Abhi
Abhi

Reputation: 1255

Variable initialization error in Tensorflow

I am following a TensorFlow tutorial and running the below code but running into Variable initialization error:

num_points = 1000
vectors_set = []
for i in range(num_points):
         x1= np.random.normal(0.0, 0.55)
         y1= x1 * 0.1 + 0.3 + np.random.normal(0.0, 0.03)
         vectors_set.append([x1, y1])

x_data = [v[0] for v in vectors_set]
y_data = [v[1] for v in vectors_set]
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

And the error message generated is:

FailedPreconditionError: Attempting to use uninitialized value Variable_3
     [[Node: Variable_3/read = Identity[T=DT_INT64, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]
Caused by op 'Variable_3/read', defined at:
  File "/Users/ayada/anaconda/envs/tensorflow/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/ayada/anaconda/envs/tensorflow/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/ayada/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/__main__.py", line 3, in <module>
    app.launch_new_instance()
  File "/Users/ayada/anaconda/envs/tensorflow/lib/python3.5/site-packages/traitlets/config/application.py", line 653, in launch_instance
    app.start()
  File "/Users/ayada/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 474, in start
    ioloop.IOLoop.instance().start()
  File "/Users/ayada/anaconda/envs/tensorflow/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 162, in start
    super(ZMQIOLoop, self).start()

Can someone advise me how to rectify this?

Upvotes: 3

Views: 1913

Answers (3)

aiSolutions
aiSolutions

Reputation: 220

The problem is with this line tf.initialize_all_variables(). This method had been deprecated and was removed after 2017-03-02.

Because it has been deprecated, it means your variables are not actually initializing.

Try this instead


init = tf.global_variables_initializer()
sess.run(init)

Initializing variables is only required for TensorFlow v1 (TF1). Consider using TF2 as variables are initialized immediately when they are created.

Upvotes: 0

fabmilo
fabmilo

Reputation: 48310

You are running inside a ipython notebook, is better to always scope the construction of the graph and the session instantiation like this:

g = tf.Graph()
with g.as_default():
  ... build your graph ..

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

This will prevent to add the same variables to the default graph, and guarantees that you always have the same graph.

To understand further the problem:

if your run a cell multiple time something simple like

a = tf.Variable()

It will create in the tf.default_graph() each time a NEW variable.

The scoping will prevent that by creating a new graph each time.

Upvotes: 2

Prune
Prune

Reputation: 77827

Lovely ... a crash confession, rather than an error message. I suspect that something in your set-up left one of your formal TF variables hanging, probably one of the one-letter names. To debug, I suggest that you insert a simple print statement after each initialization to report the value computed, or at least the variable type descriptor. For instance:

x_data = [v[0] for v in vectors_set]
y_data = [v[1] for v in vectors_set]
print (x_data, y_data)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
print (W)
b = tf.Variable(tf.zeros([1]))
print(b)
...

Not only will this track data values, but when the program crashes again, you'll have a trace of where it died -- more information that you didn't get from the stack trace you got above.

Yes, this is dirty and low-tech ... but it will get you to a solution as fast as anything else I know, unless you have your debugger already launched on this program.

Upvotes: 2

Related Questions