使用 while_loop 时 TensorFlow 梯度没有响应

TensorFlow gradient does not respond when using while_loop

我正在使用 TensorFlow 构建深度学习模型。 (整个模型非常复杂。)在模型中,我需要使用 while_loop 根据我输入的句子数量动态控制计算流程。以前,我使用 for 循环而不是 while_loop。我切换到while_loop后,渐变就没用了

梯度不起作用我的意思是如果我向前执行,它工作正常(产生一些输出)。但是如果我为训练启用梯度计算,当我 运行 我的代码时它不会产生任何响应,只是挂在那里。在top中显示为S(暂停)。

有人知道发生了什么事吗?

下面是我如何以非常标准的方式使用 while_loop

def body(argmax_ep_gate, h, mem_state_previous, dummy):
  '''doing some computation'''
  return tf.to_int32(argmax_ep_gate), h, mem_state_current, mem_state_previous

def condition(argmax_ep_gate, h, mem_state_previous, dummy):
  '''return some condition in bool'''

argmax_g, h, _, state = tf.while_loop(
  condition, body, [initial_argmax_g, initial_h, self.state, self.state])

参考。请注意,如果主体包含可训练变量,则需要使用变量范围