我正在寫一個示例程序來學習4個節點上的分佈式tensorflow。我的程序包含賦值給一個簡單的numpy數組。然後我將它們轉換成張量流數組來做一個基本的算術加法操作。我將輸出存儲在另一個tensorflowarray中。最後將這個數組轉換成張量,將其打印在輸出中。Tensorarray初始化
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)
我想覆蓋tensorarrays u1,u2,u3,u4。上面的書面程序給出了以下錯誤: -
InvalidArgumentError (see above for traceback): TensorArray TensorArray_1_21: Could not write to TensorArray index 0 because it has already been read.
是否有第一個for循環後初始化使用tensorarrays?請幫忙。