Looping over a tensor : <p>I am trying to process a tensor of variable size, in a python way that would be something like:</p>

<pre><code># X is of shape [m, n]
for x in X:
    process(x)
</code></pre>

<p>I have tried to use <em>tf.scan</em>, the thing is that I want to process every sub-tensor, so I have tried to use a nested <em>scan</em>, but I was enable to do it, because <em>tf.scan</em> work with the accumulator, if not found it will take the first entry of the <em>elems</em> as initializer, which I don't want to do.
As an example, suppose I want to add one to every element of my tensor (this is just an example), and I want to process it element by element. If I run the code bellow, I will only have one added to a sub-tensor, because <em>scan</em> consider the first tensor as initializer, along with the first element of every sub-tensor.</p>

<pre><code>import numpy as np
import tensorflow as tf

batch_x = np.random.randint(0, 10, size=(5, 10))
x = tf.placeholder(tf.float32, shape=[None, 10])

def inner_loop(x_in):
    return tf.scan(lambda _, x_: x_ + 1, x_in)

outer_loop = tf.scan(lambda _, input_: inner_loop(input_), x, back_prop=True)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    rs = sess.run(outer_loop, feed_dict={x: batch_x})
</code></pre>

<p>Any suggestions ?</p>
