jaffry_x
jaffry_x

Reputation: 179

Cannot evaluate tensor using eval() error

I am following the code written in the tutorial here. I have followed exactly each line, but I am receiving the following error. Following is the error, followed by my code.

ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use with sess.as_default() or pass an explicit session to eval(session=sess)

X = tf.placeholder(tf.float32, [None, n_timesteps, n_inputs])

basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)

outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)

init = tf.global_variables_initializer()
# #with tf.Session() as sess:
sess = tf.Session()

with sess.as_default():
    init.run()
    sess.run(tf.global_variables_initializer())
    outputs_val = outputs.eval(feed_dict={X: X_batch})

print(states.eval(feed_dict={X: X_batch}))

Upvotes: 1

Views: 891

Answers (1)

SJa
SJa

Reputation: 515

You have to have an active session for the print line. Bring it under the with sess.as_default() as shown below.

Use the following

> with sess.as_default():
>     init.run()
>     sess.run(tf.global_variables_initializer())
>     outputs_val = outputs.eval(feed_dict={X: X_batch})    
>     print(states.eval(feed_dict={X: X_batch}))

Upvotes: 2

Related Questions