Blizzard
Blizzard

Reputation: 1127

Tensorflow: AttributeError: 'tuple' object has no attribute 'eval'

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

I am trying to follow this https://github.com/kvfrans/twitch/blob/master/sample.py#L45 example to decode and run a trained tensorflow model, but it seems like the tensorflow code used was an older version. I have managed to fix most calls to v 1.0.0, but I am stuck where the code line above gives me the following error:

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'

Any ideas on how I should fix the .eval() and state? It is used later in:

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

Upvotes: 4

Views: 10613

Answers (3)

mrry
mrry

Reputation: 126184

The .eval() method is only implemented on tf.Tensor, but as others have observed, the cell.zero_state() method returns a tuple object.

The tf.Session.run() method understands how to unpack tuples, and tf.Tensor.eval() is just a convenient wrapper for calling tf.Session.run() on a single tensor in the "default" session. Using this observation, you can switch this line:

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

...with the following:

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

Upvotes: 3

bhaskarc
bhaskarc

Reputation: 9521

You cannot run eval on Python objects - tuple in this case.

One option could be to convert the Python Object to tensor first:

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

to:

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

Once it is a tensor, you eval it with:

state.eval()

Upvotes: 2

Franck Dernoncourt
Franck Dernoncourt

Reputation: 83347

From 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.

Which explains the error message you get (you cannot call .eval() on a tuple).

Upvotes: 1

Related Questions