Issue 1:
Hi,

I am using Tensorflow v0.11 and I have tried on Mac OS X and Centos 6

I am running into an error when running the following code:

W = tf.get_variable('W', (5, 3))

x = tf.placeholder(tf.float32, shape=(None, 5))

h = tf.matmul(x, W)

grads = tf.map_fn(lambda x: tf.gradients(x, W)[0], h)

I basically want to have the following but without a fixed batch size:

grads = [tf.gradients(h[t], W)[0] for t in range(batch_size)]

My error is:

Invalid argument: TensorArray map/TensorArray_1@map/while/gradients: Could not write to TensorArray index 3 because it has already been read.
[...]
tensorflow.python.framework.errors.InvalidArgumentError: TensorArray map/TensorArray_1@map/while/gradients: Could not write to TensorArray index 3 because it has already been read.
[[Node: map/while/gradients/map/while/TensorArrayRead_grad/TensorArrayWrite = TensorArrayWrite[T=DT_FLOAT, _class=["loc:@map/TensorArray"], _device="/job:localhost/replica:0/task:0/cpu:0"](map/while/gradients/map/while/TensorArrayRead_grad/TensorArrayGrad/TensorArrayGrad, map/while/Identity, map/while/gradients/Fill, map/while/gradients/map/while/TensorArrayRead_grad/TensorArrayGrad/gradient_flow)]]

I have tried the following workaround using scan instead of map_fn with a zero initializer but to no avail:

initializer = np.zeros((5, 3)).astype('float32')
grads = tf.scan(
lambda a, x: tf.gradients(x, W)[0],
h,
initializer)

Is this a know issue?

Issue 2:
GitHub issues are for bugs / installation problems / feature requests.

For general support from the community, see StackOverflow.

To make bugs and feature requests more easy to find and organize, we close issues that are deemed

out of scope for GitHub Issues and point people to StackOverflow.

For bugs or installation issues, please provide the following information.

The more information you provide, the more easily we will be able to offer

help and advice.

Environment info

Operating System:

Mac OS 10.10

Installed version of CUDA and cuDNN:

(please attach the output of ls -l /path/to/cuda/lib/libcud*):

None

If installed from binary pip package, provide:

Which pip package you installed.

The output from python -c "import tensorflow; print(tensorflow.__version__)".

0.10.0rc0

If installed from source, provide

The commit hash (git rev-parse HEAD)

The output of bazel version

Steps to reproduce

I want to evaluate the diagonal of Hessian with tf.map_fn, which takes a function that maps each dimension (scalar) to its Hessian

import tensorflow as tf

def hessian_factory(f):
def hessian1(x):
g = tf.gradients(f, x)[0]
h = tf.gradients(g, x)[0]
return h

return hessian1

sess = tf.Session()
x = tf.Variable(1.0)
sess.run(tf.initialize_all_variables())

f = 1. / (1 + tf.exp(-x))
func = hessian_factory(f)
h = tf.map_fn(func, x)

print(sess.run(h))

However, it seems tf.gradients(f, x) produces None.

Even the following statements gives None

import tensorflow as tf

sess = tf.Session()
x = tf.Variable(1.0)
sess.run(tf.initialize_all_variables())

f = 1. / (1 + tf.exp(-x))
h = tf.map_fn(lambda x: tf.gradients(f, x)[0], x)

What have you tried?

Logs or other output that would be helpful

(If logs are large, please upload as attachment).