Skip to content Skip to sidebar Skip to footer

Error While Running Tensorflow A Second Time

I am trying to run the following tensorflow code and it's working fine the first time. If I try running it again, it keeps throwing an error saying ValueError: Variable layer1/wei

Solution 1:

This is a matter of how TF works. One needs to understand that TF has a "hidden" state - a graph being built. Most of the tf functions create ops in this graph (like every tf.Variable call, every arithmetic operation and so on). On the other hand actual "execution" happens in the tf.Session(). Consequently your code will usually look like this:

build_graph()

with tf.Session() as sess:
  process_something()

since all actual variables, results etc. leave in session only, if you want to "run it twice" you would do

build_graph()

with tf.Session() as sess:
  process_something()

with tf.Session() as sess:
  process_something()

Notice that I am building graph once. Graph is an abstract representation of how things look like, it does not hold any state of computations. When you try to do

build_graph()

with tf.Session() as sess:
  process_something()

build_graph()

with tf.Session() as sess:
  process_something()

you might get errors during second build_graph() due to trying to create variables with the same names (what happens in your case), graph being finalised etc. If you really need to run things this way you simply have to reset graph in between

build_graph()

with tf.Session() as sess:
  process_something()

tf.reset_default_graph()

build_graph()

with tf.Session() as sess:
  process_something()

will work fine.


Post a Comment for "Error While Running Tensorflow A Second Time"