Understand TensorFLow Global Variables and Local Variables – TensorFlow Tutorial

By | August 26, 2021

In this tutorial, we will introduce the difference between global variables and local variables in tensorflow. You can learn how to use them correctly.

TensorFlow Global Variables

TensorFlow global variables are shared across machines in a distributed environment. We can use tf.Variable() and tf.get_variable() to create a global variable.

tensorflow create a global variable

In order to list all global variables, we can do as follows:

print([x for x in tf.global_variables()])

In order to initialize global variables, we can use sess.run(tf.global_variables_initializer()). Here is the tutorial:

An Explain to sess.run(tf.global_variables_initializer()) for Beginners – TensorFlow Tutorial

TensorFlow Local Variables

TensorFlow local variables only can be used in local environment, they are not shared in a distributed environment. We can use tf.local_variable() to create a local variable.

tensorflow create a local variable

In order to list all local variables, we can do as follows:

print([x for x in tf.local_variables()])

To initialize local varaibles, we can do:

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

Leave a Reply