0

I am trying out Tensorflow example on Variables, however I am getting an error while I try to print using tf.Session

ValueError: Variable my_int_variable already exists, disallowed. Did 
you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

I am pretty sure I have not defined anywhere else and Removed all variables as well on Spyder

import tensorflow as tf

my_int_variable = tf.get_variable("my_int_variable", [1, 2, 3], dtype=tf.int32,
  initializer=tf.zeros_initializer)

with tf.Session() as session:
    session.run(print(my_int_variable))

The screen shot of my error enter image description here

3
  • 1
    You probably meant print(session.run(my_int_variable)), and should initialize variables in the session before trying to retrieve their values. Otherwise, the snippet that you included will not produce the error that you mention, so it is hard to tell what is happening in your actual code. Commented Apr 18, 2018 at 9:15
  • @jdehesa I've attached the screen-shot Commented Apr 18, 2018 at 9:42
  • Yes, what I mean is that if you are getting that error only with that code, there must be something else that you ran before, or maybe you just run the same code twice. If you run the snippet in a new interpreter (e.g. close and reopen Spyder) and run that code once you should not see the same error. Commented Apr 18, 2018 at 10:04

1 Answer 1

1

You do not initialize the variable before using it. Try this:

import tensorflow as tf

my_int_variable = tf.get_variable("my_int_variable", [1, 2, 3], dtype=tf.int32,
  initializer=tf.zeros_initializer)

with tf.Session() as session:
  session.run(tf.global_variables_initializer())
  print(session.run(my_int_variable))

It produces the zero-initialized array on my machine. Also, it is good practice to evaluate the variables within the print function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.