此处带有变量创建代码的Tensorflow While循环:
x = tf.Variable(100)
c = tf.Constant(2)
n = 100
loops = 50
l1 = tf.Variable(np.random.random(n))
c1 = tf.Variable(np.random.random(n))
x = tf.multiply(c1,tf.exp(-(x-l1)/c))
l2 = tf.Variable(np.random.random(n))
c2 = tf.Variable(np.random.random(n))
x = tf.multiply(c2,tf.exp(-(x-l2)/c))
l3 = tf.Variable(np.random.random(n))
c3 = tf.Variable(np.random.random(n))
x = tf.multiply(c3,tf.exp(-(x-l3)/c))
.....
.....
l50 = tf.Variable(np.random.random(n))
c50 = tf.Variable(np.random.random(n))
x = tf.multiply(c50,tf.exp(-(x-l50)/c))
所以,我想在张量流的while循环中这样做:
while loop(i from 1 to 50):
l[i] = tf.Variable(np.random.random(n))
c[i] = tf.Variable(np.random.random(n))
x = tf.multiply(c[i],tf.exp(-(x-l[i])/c))
如何在tensorflow中实现这一目标。
谢谢你
答案 0 :(得分:1)
你能这样喂吗?
l = tf.placeholder(tf.float32, shape=[None, ])
c = tf.placeholder(tf.float32, shape=[None, ])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
x = tf.multiply(l,c) #Assume a formula
for i in range(50) :
arr = np.random.random_sample((i,))
print (arr)
sess.run(x,feed_dict={l :arr,c :arr})
根据您的评论,我只是尝试过。这不是确切的答案。
results = tf.TensorArray(dtype=tf.float32, size=100)
lvalues = tf.TensorArray(dtype=tf.float32, size=50)
cvalues = tf.TensorArray(dtype=tf.float32, size=50)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
def my_func(x):
a = np.random.random_sample((x,))
return a
input = tf.placeholder(tf.int32)
y = tf.py_func(my_func, [input], tf.float64)
for i in range(50) :
r = sess.run(y,feed_dict={input : i})
lvalues.write(i,y)
cvalues.write(i,y)
print(y)
print(r)