Simplicity
Simplicity

Reputation: 48966

TensorFlow - NameError: name 'session' is not defined

I'm having an error in the last line of the following code portion:

confusionMatrix = tf.confusion_matrix(labels=y_true_cls,predictions=y_pred_cls)
x_batch, y_batch, _, cls_batch = data.valid.next_batch(batch_size_validation)
confusionMatrix = session.run(confusionMatrix, feed_dict={x: x_batch, y_true: y_batch})

The error states the following:

NameError: name 'session' is not defined

At the end of my code (after the above code portion), I have the following:

with tf.Session() as session:
    init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    session.run(init)
    train( num_iteration=1000)

How can I solve this issue?

Thanks.

Upvotes: 0

Views: 10214

Answers (2)

Simplicity
Simplicity

Reputation: 48966

I simply included my confusion matrix in a function called evaluate(), and issued a call to evaluate() under train(num_iteration=1000) in the with tf.Session() as session: block:

with tf.Session() as session:
    init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    session.run(init)
    train(num_iteration=10000)
    evaluate()

Upvotes: 1

Sadjad Anzabi Zadeh
Sadjad Anzabi Zadeh

Reputation: 444

You haven't defined session prior to session.run() . Simply define it (for example session=tf.Session()) and it should work.

Upvotes: 2

Related Questions