Fix TensorFlow TensorArray is Always Zero Tensor – TensorFlow Tutorial

By | June 21, 2020

When you are writing some tensors to TensorArray, you may get an zero tensor. In this tutorial, we will introduce you how to fix this error.

Look at this example below.

We have created fixed size tensorarray gen_o.

import tensorflow as tf
import numpy as np

gen_o = tf.TensorArray(dtype=tf.float32, size= 3,
                                             dynamic_size=False, clear_after_read = False,infer_shape=True)

Then we will write 3 tensors into gen_o.

init_1 = tf.Variable(np.array([1, 0, 0, 0], dtype = np.float32))
init_2 = tf.Variable(np.array([1, 1, 0, 0], dtype = np.float32))
init_3 = tf.Variable(np.array([1, 1, 1, 0], dtype = np.float32))
gen_o.write(0, init_1)
gen_o.write(1, init_2)
gen_o.write(2, init_3)

Output tensors in gen_o.

v = gen_o.stack()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(v))

Then you will find v is:

[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

Why v is zero tensor?

Because it is wrong when we write tensors to gen_o.

gen_o.write(0, init_1)

You should write as follow:

gen_o = gen_o.write(0, init_1)

Notice: When you are writing some tensors into a tensorarray, it will return a new tensorarray object.

So wo should modify our code to below:

gen_o = gen_o.write(0, init_1)
gen_o = gen_o.write(1, init_2)
gen_o = gen_o.write(2, init_3)

Run this code again, you will find v is:

[[1. 0. 0. 0.]
 [1. 1. 0. 0.]
 [1. 1. 1. 0.]]

That is right.

Leave a Reply

Your email address will not be published. Required fields are marked *