Tensorflow:属性错误:'tuple'对象没有属性'eval'

4
state = cell.zero_state(batchsize, tf.float32).eval()

我正在尝试跟随这个示例 https://github.com/kvfrans/twitch/blob/master/sample.py#L45 解码和运行一个经过训练的TensorFlow模型,但似乎使用的TensorFlow代码是旧版本。我已经设法修复了大多数对v 1.0.0的调用,但是当上述代码行给出以下错误时,我卡住了:
Traceback (most recent call last):
  File "server.py", line 1, in <module>
    from sample import *
  File "/home/user/twitch/sample.py", line 75, in <module>
    print predict("this game is")
  File "/home/user/twitch/sample.py", line 46, in predict
    state = initialstate.eval()
AttributeError: 'tuple' object has no attribute 'eval'

有没有想法,我应该如何修复 .eval()state?它们后面被用于:

guessed_logits, state = sess.run([logits, final_state], feed_dict={input_data: primer, initialstate: state})
3个回答

3
.eval()方法只适用于tf.Tensor,但是其他人已经注意到,cell.zero_state()方法返回一个tuple对象。

tf.Session.run()方法知道如何解包元组,并且tf.Tensor.eval()只是在“默认”会话中对单个张量调用tf.Session.run()的方便包装器。使用这个观察结果,你可以改变这一行:

state = cell.zero_state(batchsize, tf.float32).eval()

以下是需要使用的内容:

state = tf.get_default_session().run(cell.zero_state(batchsize, tf.float32))

2

您不能在Python对象 - 在这种情况下是元组上运行eval。

一种选择是首先将Python对象转换为张量:

state = cell.zero_state(batchsize, tf.float32).eval()

to:

state = tf.convert_to_tensor(cell.zero_state(batchsize, tf.float32))

一旦它是一个张量,您可以使用以下方式进行 eval

state.eval()

它返回了这个:ValueError: 无法展开字典。键有4个元素,但值只有1个元素。键:[<tf.Tensor 'zeros:0' shape=(1, 200) dtype=float32>, <tf.Tensor 'zeros_1:0' shape=(1, 200) dtype=float32>, <tf.Tensor 'zeros_2:0' shape=(1, 200) dtype=float32>, <tf.Tensor 'zeros_3:0' shape=(1, 200) dtype=float32>],值:[<tf.Tensor 'packed:0' shape=(2, 2, 1, 200) dtype=float32>]。 - Blizzard

1

来自 TensorFlow Release 1.0.0 notes:

LSTMCellBasicLSTMCellMultiRNNCell构造函数现在默认为state_is_tuple=True。在过渡到新默认值时进行快速修复,只需传递参数state_is_tuple=False

这解释了您收到的错误消息(您不能在元组上调用.eval())。


我无法将 state_is_tuple=False 转换,你有什么建议修复它? - Blizzard
@Blizzard 选择你感兴趣的元组元素并在其上运行 eval。 - Franck Dernoncourt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接