<p dir="auto">Hi,</p>
<p dir="auto">I am using Tensorflow v0.11 and I have tried on Mac OS X and Centos 6</p>
<p dir="auto">I am running into an error when running the following code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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)"><pre class="notranslate"><code class="notranslate">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)
</code></pre></div>
<p dir="auto">I basically want to have the following but without a fixed batch size:<br>
<code class="notranslate">grads = [tf.gradients(h[t], W)[0] for t in range(batch_size)]</code></p>
<p dir="auto">My error is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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=[&quot;loc:@map/TensorArray&quot;], _device=&quot;/job:localhost/replica:0/task:0/cpu:0&quot;](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)]]"><pre class="notranslate"><code class="notranslate">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)]]
</code></pre></div>
<p dir="auto">I have tried the following workaround using <code class="notranslate">scan</code> instead of <code class="notranslate">map_fn</code> with a zero initializer but to no avail:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="initializer = np.zeros((5, 3)).astype('float32')
grads = tf.scan(
	lambda a, x: tf.gradients(x, W)[0],
	h,
	initializer)"><pre class="notranslate"><code class="notranslate">initializer = np.zeros((5, 3)).astype('float32')
grads = tf.scan(
	lambda a, x: tf.gradients(x, W)[0],
	h,
	initializer)
</code></pre></div>
<p dir="auto">Is this a know issue?</p>
<p dir="auto">GitHub issues are for bugs / installation problems / feature requests.<br>
For general support from the community, see <a href="https://stackoverflow.com/questions/tagged/tensorflow" rel="nofollow">StackOverflow</a>.<br>
To make bugs and feature requests more easy to find and organize, we close issues that are deemed<br>
out of scope for GitHub Issues and point people to StackOverflow.</p>
<p dir="auto">For bugs or installation issues, please provide the following information.<br>
The more information you provide, the more easily we will be able to offer<br>
help and advice.</p>
<h3 dir="auto">Environment info</h3>
<p dir="auto">Operating System:<br>
Mac OS 10.10</p>
<p dir="auto">Installed version of CUDA and cuDNN:<br>
(please attach the output of <code class="notranslate">ls -l /path/to/cuda/lib/libcud*</code>):<br>
None</p>
<p dir="auto">If installed from binary pip package, provide:</p>
<ol dir="auto">
<li>Which pip package you installed.</li>
<li>The output from <code class="notranslate">python -c "import tensorflow; print(tensorflow.__version__)"</code>.<br>
0.10.0rc0</li>
</ol>
<p dir="auto">If installed from source, provide</p>
<ol dir="auto">
<li>The commit hash (<code class="notranslate">git rev-parse HEAD</code>)</li>
<li>The output of <code class="notranslate">bazel version</code></li>
</ol>
<h3 dir="auto">Steps to reproduce</h3>
<p dir="auto">I want to evaluate the diagonal of Hessian with <code class="notranslate">tf.map_fn</code>, which takes a function that maps each dimension (scalar) to its Hessian</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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))"><pre class="notranslate"><code class="notranslate">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))
</code></pre></div>
<p dir="auto">However, it seems <code class="notranslate">tf.gradients(f, x)</code> produces <code class="notranslate">None</code>.</p>
<p dir="auto">Even the following statements gives <code class="notranslate">None</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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)"><pre class="notranslate"><code class="notranslate">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)
</code></pre></div>
<h3 dir="auto">What have you tried?</h3>
<ol dir="auto">
<li></li>
</ol>
<h3 dir="auto">Logs or other output that would be helpful</h3>
<p dir="auto">(If logs are large, please upload as attachment).</p>