When global_variables_initializer() is actually required

Vinay picture Vinay · Jun 1, 2017 · Viewed 23.8k times · Source
import tensorflow as tf
x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')
# model = tf.global_variables_initializer()
with tf.Session() as session:
        print("x = ", session.run(x)) 
        # session.run(model)
        print("y = ", session.run(y))

I was not able to understand when global_variables_initializer() is actually required. In the above code, if we uncomment lines 4 & 7, I can execute the code and see the values. If I run as-is, I see a crash.

My question is which variables it is initializing. x is a constant which does not need initialization and y is variable which is not being initialized but is used as an arithmetic operation.

Answer

Salvador Dali picture Salvador Dali · Jun 1, 2017

tf.global_variables_initializer is a shortcut to initialize all global variables. It is not required, and you can use other ways to initialize your variables or in case of easy scripts sometimes you do not need to initialize them at all.

Everything except of variables do not require initialization (constants and placeholders). But every used variable (even if it is a constant) should be initialized. This will give you an error, although z is just 0-d tensor with only one number.

import tensorflow as tf
z = tf.Variable(4)
with tf.Session() as session:
        print(session.run(z)) 

I highlighted the word used, because if you just have variables which are not run (or non of the runs depends on them) you do not need to initialize them.


For example this code will execute without any problems, nonetheless it has 2 variables and one operation which depends on them. But the run does not require them.

import tensorflow as tf
x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')
z = tf.Variable(4)
a = y + z
with tf.Session() as session:
        print("x = ", session.run(x))