title: How to assign values in a TensorFlow while_loop
body: <p>The goal is to implement a recurrent function in TensorFlow to filter a signal over time.</p>
  
  <p>The <code>input</code> is later presented as a 5-D tensor of form <code>[batch, in_depth, in_height, in_width, in_channels]</code>. I want to use <a href="https://www.tensorflow.org/api_docs/python/tf/while_loop" rel="nofollow noreferrer"><code>tf.while_loop</code></a> to iterate over <code>in_depth</code> and reassign values depended on values of previous time steps. However, I fail to reassign variable values within the loop.</p>
  
  <p>In order to simplify the problem, I have create a 1-D version of the problem:</p>
  
  <pre><code>def condition(i, signal):
      return tf.less(i, signal.shape[0])
  
  def operation(i, signal):
      signal = tf.get_variable("signal")
      signal = signal[i].assign(signal[i-1]*2)
      i = tf.add(i, 1)
      return (i, signal)
  
  with tf.variable_scope("scope"):
      i = tf.constant(1)
      init = tf.constant_initializer(0)
      signal = tf.get_variable("scope", [4], tf.float32, init, trainable = False)
      signal = tf.assign(signal[0], 1.2)
  
  with tf.variable_scope("scope", reuse = True):
      loops_vars = [i, signal]
      i, signal = tf.while_loop(condition, operation, loop_vars, back_prop = False)
  
  with tf.Session() as session:
      session.run(tf.global_variables_initializer())
      i, signal = session.run([i, signal])
  </code></pre>
  
  <p><a href="https://www.tensorflow.org/api_docs/python/tf/assign" rel="nofollow noreferrer"><code>tf.assign</code></a> returns an operation, which has to be run in a session in order to be evaluated (<a href="https://stackoverflow.com/questions/34220532/how-to-assign-value-to-a-tensorflow-variable">see here for further details</a>).</p>
  
  <p>I expected, that TensorFlow would chain the operations within the loop and hence execute the assignments once I run a session and request <code>signal</code>. However, when I execute the given code and print the result, <code>signal</code> contatins <code>[1.2, 0, 0, 0]</code> and <code>i</code> contains (as expected) <code>4</code>.</p>
  
  <p>What is my misconception here and how can I change the code such that the values of <code>signal</code> are reassigned?</p>