2017-03-08 59 views
3
state = cell.zero_state(batchsize, tf.float32).eval() 

我想按照这个https://github.com/kvfrans/twitch/blob/master/sample.py#L45例如解码和运行一个训练有素的tensorflow模型,但它似乎是使用了旧版本的tensorflow代码。我已成功地修复大多数呼叫到v 1.0.0,但我被困在那里上面的代码行给了我以下错误:Tensorflow:AttributeError的:“元组”对象有没有属性“的eval”

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

.eval() method仅在tf.Tensor上实施,但其他人有o保留,cell.zero_state()方法返回一个tuple对象。

tf.Session.run()方法了解如何解压元组,并tf.Tensor.eval()仅仅是一个方便的包装上在"default" session一个叫张tf.Session.run()。使用这种观察,你可以切换这一行:

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

...有以下几点:

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

这工作,非常感谢你! – Blizzard

1

TensorFlow Release 1.0.0 notes

LSTMCell , BasicLSTMCell , and MultiRNNCell constructors now default to state_is_tuple=True. For a quick fix while transitioning to the new default, simply pass the argument state_is_tuple=False .

,说明您收到错误信息(你不能叫一个tuple.eval())。

+0

我不能把'state_is_tuple = FALSE',你会怎么推荐修复它? – Blizzard

+0

@Blizzard选择你感兴趣的元组元素并在其上运行eval。 –

+0

这很有道理,谢谢! – Blizzard

2

在这种情况下,您无法对Python对象 - 元组运行eval。

一种选择可能是Python的对象转换先张:

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

到:

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

一旦它是一个张量,你eval它:

state.eval() 
+0

它以此返回:'ValueError:无法拼合字典。钥匙有4个元素,但值有1个元素。 Key:[],value:[]。' – Blizzard