title: index a list inside the while_loop TensorFlow function
body: <p>Hello.</p>
  
  <p>I have an issue. I actually have a list (<strong>real python list</strong>) of placeholders.
  My list is of length <strong>n ( = T in the below code)</strong> and is as follow:</p>
  
  <p><code>
  my_list = [[D0, K], [D1, K], ... [Dn, K]]
  </code></p>
  
  <p>Where the Di are not necessary of the same size. That is why I used a list (because I cannot convert this list to a tensor without padding)</p>
  
  <p>What I want to do is that:</p>
  
  <pre class="lang-py prettyprint-override"><code>temp = []
  for step in range(T):
      temp.append(tf.reduce_sum(x[step], axis=0))
  
  sum_vn_t = tf.stack(temp)
  </code></pre>
  
  <p>Where <strong>x = my_list</strong> of length <strong>n</strong> defined previously.
  This piece of code will just transform my inputs list <strong>x</strong> that looks like:</p>
  
  <pre class="lang-py prettyprint-override"><code>[[D0, K], [D1, K], ... [Dn, K]]
  </code></pre>
  
  <p>into</p>
  
  <pre class="lang-py prettyprint-override"><code>[n, K]
  </code></pre>
  
  <p>Where I actually sum over each Di rows so that <strong>each jth line</strong> of my new tensor of size [n, K] contains: <strong>sum([Dj, K], axis=0)</strong></p>
  
  <p>The problem is that if I use a python <strong>for ... loop</strong> I am not sure the backpropagation will actually works (I'm quite new to TensorFlow but I think that if I don't use the <strong>while_loop</strong> function my operations won't be added to the Graph and so doing a native python for loop does not make sense ?).</p>
  
  <p>So I've just tried to recode this piece of code using the tensorflow while_loop.
  The code is as follow:</p>
  
  <pre class="lang-py prettyprint-override"><code>def reduce_it(i, outputs):
      i_row = tf.reduce_sum(x[i], axis=0) # x[i] throw an error as i is a Tensor
      outputs = outputs.write(i, i_row)
  
      return i+1, outputs
  
  temp = tf.TensorArray(dtype=tf.float32, infer_shape=False, size=1,
                    dynamic_size=True)
  _, temp = tf.while_loop(lambda i, *args: tf.less(i, T),
                       reduce_it, [0, temp])
  temp = temp.stack()
  </code></pre>
  
  <p>I've already seen someone asking this but nobody was able to give him a workaround. I tried to convert the Tensor <strong>i</strong> into an integer by passing a numpy array on which I add element during the while loop to get the shape of this array:</p>
  
  <pre class="lang-py prettyprint-override"><code>def reduce_it(arr, outputs):
      idx = arr.shape[0] - 1 # use shape[0] of array as i
      i_row = tf.reduce_sum(x[idx], axis=0)
      outputs = outputs.write(tf.constant([idx]), i_row)
      arr = np.append(arr, 0)
      return arr, outputs
  
  temp = tf.TensorArray(dtype=tf.float32, infer_shape=False, size=1,
                    dynamic_size=True)
  _, temp = tf.while_loop(lambda arr, *args: tf.less(arr.shape[0], T),
                       reduce_it, [np.array([0]), temp])
  temp = temp.stack()
  </code></pre>
  
  <p>but it doesn't work because the shape of my array <strong>arr</strong> change during the loop so I might need to use the shape_invariants option of while_loop but I didn't manage to have a working code...</p>
  
  <p>Also I have converted my list to a Tensor by adding a padding such that my tensor is of size: <strong>[T, max(Di), K]</strong> but I still need to know on which Dimension Di i'm working at each iteration of my loop that means I need to create a tensor (1d-array) of size n having Di as number on position i:</p>
  
  <pre class="lang-py prettyprint-override"><code>my_tensor = [D1, D2, ..., Dn]
  </code></pre>
  
  <p>then I need to gather Di in my while loop but if I simply do:</p>
  
  <pre class="lang-py prettyprint-override"><code>my_dim = tf.gather(my_tensor, i)
  </code></pre>
  
  <p>I will only gather a tensor and I need a integer.</p>
  
  <p>I don't think I can define a session and recover my_dim.eval() as this code is part of my module which is then called during training (and I create a session at this moment).</p>
  
  <p>Some experts of TF can think of a workaround or a hack?</p>
  
  <p>Thank you in advance</p>
  
  <p><strong>Note</strong>: Also padding is a solution but actually later in my code I need to get each one of my initial matrix of size [Di, K] and so if I pad my [Di, K] so that I can build a Tensor of shape:</p>
  
  <pre class="lang-py prettyprint-override"><code>[n, max(Dn), K]
  </code></pre>
  
  <p>then, I still need to recover each [Di, K] to be able to use tf.matmul() (operations for example) with the correct dimensions. So padding is actually not a solution for me.</p>
  
  <p>I hope my post is clear enough.</p>