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

3 Answers 3

3

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))
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

It returns with this: ValueError: Could not flatten dictionary. Key had 4 elements, but value had 1 elements. Key: [<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>], value: [<tf.Tensor 'packed:0' shape=(2, 2, 1, 200) dtype=float32>].
1

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

2 Comments

I can't turn the state_is_tuple=False, how would you recommend fixing it?
@Blizzard choose the element of the tuple that interests to you and run eval on it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.