I roughly understand the relations between TensorArray and Tensor in Tensorflow 0.80, then I would write my memos and codes. TensorArray is a special types of tensor that can modify its value in while_loop, and we cannot apply Session.run to an instance of TensorArray. This means that we cannot see the values in TensorArray. Thus we need to convert TensorArray to Tensor that Session.run can be applied to. The conversion is enable by TensorArray.pack(). A simple example of using TensorArray code is here.
import tensorflow as tf
import numpy as np
from tensorflow.python.ops import tensor_array_ops
#Simple example of seeing values in TensorArray
# just adding [2.4, 3.5] to TensorArray three times
def body(time,output_ta_l):
output_ta_l = output_ta_l.write(time, [2.4,3.5])
return time+1, output_ta_l
def condition(time,output_ta_l):
return tf.less(time, 3)
time = tf.constant(0)
output_ta = tensor_array_ops.TensorArray(tf.float32, size=1, dynamic_size=True)
result = tf.while_loop(condition, body, [time,output_ta])
ltime, lout = result
final_out = lout.pack()
with tf.Session():
tf.initialize_all_variables().run()
print(ltime.eval())
print("final_out==",final_out.eval())
You can get the result below.
3
final_out== [[ 2.4000001 3.5 ]
[ 2.4000001 3.5 ]
[ 2.4000001 3.5 ]]
So if you convert TensorArray to Tensor with Tensor.Array.pack(), you can see the values with print.
Reference:
http://stackoverflow.com/questions/40339510/tensorarray-initialization/40349551
http://stackoverflow.com/questions/37441140/how-to-use-tf-while-loop-in-tensorflow