python - Tensorflow:AttributeError: 'tuple' 对象没有属性 'eval'

标签 python machine-learning tensorflow

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})

最佳答案

.eval() method 仅在 tf.Tensor 上实现,但正如其他人所观察到的,cell.zero_state() 方法返回一个 tuple 对象。

tf.Session.run() 方法了解如何解包元组,而 tf.Tensor.eval() 只是一个方便的包装器,用于调用 tf.Session.run() "default" session 中的单个张量。使用此观察,您可以切换此行:

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

...具有以下内容:

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

关于python - Tensorflow:AttributeError: 'tuple' 对象没有属性 'eval',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677650/

相关文章:

Python TLearn - 损失太高

python-2.7 - scikit-learn SGD 中 class_weight 参数的作用是什么

python - 在 tensorflow 中将 bool 张量转换为二进制

python - 使用系列索引作为列的 Pandas 系列到数据框

python - 从元组列表创建 Spark rdd 并使用 groupByKey

python - Apache Spark Python 与 DataFrames 的余弦相似度

python - Matplotlib set_color_cycle 与 set_prop_cycle

python - 即使使用相同的数据,Keras 训练和验证指标值也不同(逻辑回归)

python - 从 SparseTensorValue 访问值

python - 无法打印模型的混淆矩阵