What is the purpose of tf.global_variables_initializer?

Toke Faurby picture Toke Faurby · Jun 8, 2017 · Viewed 30.7k times · Source

I would like to understand what tf.global_variables_initializer does in a bit more detail. A sparse description is given here:

Returns an Op that initializes global variables.

But that doesn't really help me. I know that the op is necessary to initialize the graph, but what does that actually mean? Is this the step where the graph is complied?

Answer

Florentin Hennecker picture Florentin Hennecker · Jun 8, 2017

A more complete description is given here.

Only after running tf.global_variables_initializer() in a session will your variables hold the values you told them to hold when you declare them (tf.Variable(tf.zeros(...)), tf.Variable(tf.random_normal(...)),...).

From the TF doc :

Calling tf.Variable() adds several ops to the graph:

  • A variable op that holds the variable value.
  • An initializer op that sets the variable to its initial value. This is actually a tf.assign op.
  • The ops for the initial value, such as the zeros op for the biases variable in the example are also added to the graph.

And also:

Variable initializers must be run explicitly before other ops in your model can be run. The easiest way to do that is to add an op that runs all the variable initializers, and run that op before using the model.