title: TensorFlow: How to get Intermediate value of a variable in tf.while_loop()?
body: <p>I need to fetch the intermediate value of a tensor in tf.while_loop(), however, it only gives me the last returned value.</p>
  
  <p>For example, I have a variable x, which has 3 pages and its dimension is 3*2*4. Now I want to fetch each page one time and calculate the total sum, the page sum, the mean, max and min value of each page. Then I define the condition and body function and want to use tf.while_loop() to calculate the needed results. The source code is as bellow.</p>
  
  <pre><code>import tensorflow as tf    
  x = tf.constant([[[41, 8, 48,  82],
                        [9, 56, 67,  23]],
                       [[95, 89, 44,  54],
                        [11, 33, 29,  1]],
                       [[34,  9,  5,  70],
                        [14, 35, 18,  17]]], dtype=tf.int32)
  
  def cond(out, count, x):
    return count &lt; 3
  
  def body(out, count, x):
    outTemp = tf.slice(x, [count, 0, 0], [1, -1, -1])
    count += 1
    outPack = tf.unpack(out)
    outPack[0] += tf.reduce_sum(outTemp)
    outPack[1] = tf.reduce_sum(outTemp)
    outPack[2] = tf.reduce_mean(outTemp)
    outPack[3] = tf.reduce_max(outTemp)
    outPack[4] = tf.reduce_min(outTemp)
    out = tf.pack(outPack)
    return out, count, x
  
  out = tf.Variable(tf.constant([0, 0, 0, 0, 0]))  # total sum, page sum, mean, max, min
  count = tf.Variable(tf.constant(0))
  result = tf.while_loop(cond, body, [out, count, x])
  
  init = tf.initialize_all_variables()
  with tf.Session() as sess:
    sess.run(init)
    print(sess.run(x))
    print(sess.run(result)[0])
  </code></pre>
  
  <p>When I run the program, it only gives me the returned value of the last time and I can only get the results of the last page.
  So the question is, How can I get the results of each page and How can I get the intermediate value from tf.while_loop()?</p>
  
  <p>Thank you.</p>