Reputation: 1409
I am writing an example program to learn distributed tensorflow on 4 nodes. My program consists of assigning values to a simple numpy array. Then I convert them into tensorflow arrays to do a basic arithmetic addition operation. I store the outputs in another tensorflowarray. Finally convert this array into tensor to print it in the output.
import tensorflow as tf
import numpy as np
import datetime
u1 = tf.TensorArray(dtype=tf.float64,size=101)
u2 = tf.TensorArray(dtype=tf.float64,size=101)
u3 = tf.TensorArray(dtype=tf.float64,size=101)
u4 = tf.TensorArray(dtype=tf.float64,size=101)
u5 = tf.Variable(np.zeros(101))
u = np.zeros(101)
a = tf.Variable(0, dtype=tf.float64)
L = 100.0
x = tf.constant(2, dtype=tf.float64)
t1 = datetime.datetime.now()
with tf.device("/job:local/task:3"):
for i in range(2):
for j in range(101):
# u[j] = (np.sin(2*np.pi*j/L))
u[j] = j
u1 = u1.write(j,u[j])
u2 = u2.write(j,u[j])
u3 = u3.write(j,u1.read(j)+u2.read(j))
u4 = u3.pack()
assign = tf.assign(u5, u3(100))
model = tf.initialize_all_variables()
with tf.Session("grpc://localhost:2222") as sess:
sess.run(model)
print sess.run(u4)
print sess.run(assign)
# print (u4.eval())
t2 = datetime.datetime.now()
print('\n')
print "Multi node computation time: " + str(t2-t1)
I want to overwrite the tensorarrays u1,u2,u3,u4. The above written program gives the following error:-
InvalidArgumentError (see above for traceback): TensorArray TensorArray_1_21: Could not write to TensorArray index 0 because it has already been read.
Is there anyway to initialize the used tensorarrays after the first for loop? Please help.
Upvotes: 1
Views: 3226
Reputation: 5808
TensorArrays are a "write-once" data structure: after they've been read from (e.g. with pack()) they can't be written to. They're mainly useful in combination with tf.while_loop (see the TensorArray documentation).
It looks like TensorArrays aren't a great fit here. You could use Python lists of Tensors, which will avoid the TensorArray errors, although this will lead to a large graph when looping over many values. If that becomes an issue, you can switch the Python for loops over to a while_loop or one of the higher-level looping constructs. It's not clear exactly what you're trying to do, but you might look at scan() in particular if you want to save intermediate values.
Upvotes: 2