Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When global_variables_initializer() is actually required

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.

like image 823
Vinay Avatar asked Jun 01 '17 06:06

Vinay


People also ask

What is TF global_variables_initializer ()?

Variable() function in order to create a Variable and define what value it will be initialized with. We then have to explicitly perform an initialization operation by running the session with the tf. global_variables_initializer() method, which allocates the memory for the Variable and sets its initial values.

Do global variables need to be initialized?

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.

Why is a variable initializer important?

This refers to the process wherein a variable is assigned an initial value before it is used in the program. Without initialization, a variable would have an unknown value, which can lead to unpredictable outputs when used in computations or other operations.

What is TF Variable_scope?

Variable scope allows you to create new variables and to share already created ones while providing checks to not create or share by accident. For details, see the Variable Scope How To, here we present only a few basic examples. The Variable Scope works as expected when the Eager Execution is Disabled.


2 Answers

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))  
like image 52
Salvador Dali Avatar answered Sep 19 '22 23:09

Salvador Dali


From the docs (emphasis mine):

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.

Later,

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.

In short, global_variables_initializer is never required, Variable initialization is. Whenever you have Variables in your code, you must initialize them first. The global_variables_initializer helper initializes all Variables that have been previously declared, and is therefore just a very convenient way to do it.

like image 27
P-Gn Avatar answered Sep 18 '22 23:09

P-Gn