【发布时间】:2016-10-28 11:05:55
【问题描述】:
重现步骤
我正在使用TensorFlow实现一个需要使用tf.while_loop()的网络
import tensorflow as tf
import numpy as np
class model(object):
def __init__(self):
self.argmax_ep_gate_array = [ tf.placeholder(tf.int32, [None]) for _ in range(10)]
argmax_ep_gate_array_concat = tf.concat(0, self.argmax_ep_gate_array)
story_len = tf.constant(7)
starter = tf.constant(0)
z = []
def body(hops):
hops = tf.add(hops,1)
z.append(hops)
return hops
def condition(hops):
return tf.logical_and(tf.less(tf.gather(argmax_ep_gate_array_concat, hops),story_len),tf.less(hops,tf.constant(20)))
self.gate_index = tf.while_loop(condition,body,[starter])
self.z=tf.concat(0,z)
def step(self, sess):
feed={}
for i in range(10):
feed[self.argmax_ep_gate_array[i].name]=[i]
print (sess.run([self.gate_index,self.z],feed))
with tf.Session() as sess:
while_loop = model()
sess.run(tf.initialize_all_variables())
while_loop.step(sess)
你有什么尝试?
我发现如果我想 sess.run() 任何未返回的 body() 变量,tensorflow 将陷入无限循环。
上面的例子是微不足道的,但它揭示了一些东西。在实际情况下,我使用 tf.while_loop() 运行一个包含 y= wx+b 类似的 RNN,但在 while 循环之后不会返回 w 和 b。在前向网络中,它工作正常。但是,如果我运行反向传播,程序将陷入无限循环。我想上面的代码重现了我的问题,因为反向传播确实需要修改 w 和 b。或者有什么办法可以处理这个问题?
【问题讨论】:
标签: python tensorflow