TensorFlow
initialize_all_variables
deep learning
machine learning
neural networks

About tensorflow.initialize_all_variables

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

tf.initialize_all_variables() was a TensorFlow 1.x function that initialized all global variables in the computation graph before running any operations. It was deprecated in TensorFlow 0.12 and replaced by tf.global_variables_initializer(). In TensorFlow 2.x, variable initialization is automatic with eager execution — variables are initialized when created, and neither function is needed. This article covers the evolution from manual initialization to automatic eager execution.

TensorFlow 1.x: Why Initialization Was Needed

python
1import tensorflow as tf
2
3# TF 1.x: Variables are not initialized when created
4W = tf.Variable(tf.zeros([3, 1]), name="weights")
5b = tf.Variable(tf.zeros([1]), name="bias")
6
7# At this point, W and b have no values — they are graph nodes only
8
9with tf.Session() as sess:
10    # Must initialize before using variables
11    sess.run(tf.global_variables_initializer())  # Recommended
12    # OR the deprecated version:
13    # sess.run(tf.initialize_all_variables())
14
15    print(sess.run(W))  # [[0.], [0.], [0.]]
16    print(sess.run(b))  # [0.]

In TF 1.x, tf.Variable(...) only defined a graph node. The variable had no value until explicitly initialized within a session. Without initialization, accessing a variable raised FailedPreconditionError: Attempting to use uninitialized value.

The Deprecation

python
1# DEPRECATED (TF 0.x - 0.11):
2init = tf.initialize_all_variables()
3
4# REPLACEMENT (TF 0.12+):
5init = tf.global_variables_initializer()
6
7# Both do exactly the same thing — initialize all global variables
8# The rename was for consistency with tf.local_variables_initializer()

The function was renamed to tf.global_variables_initializer() to distinguish it from tf.local_variables_initializer(), which initializes only local (non-saved) variables like epoch counters and metrics accumulators.

Global vs Local Variables

python
1# Global variables: saved in checkpoints, persist across sessions
2W = tf.Variable(tf.zeros([3, 1]), name="weights")  # Global by default
3
4# Local variables: not saved, typically used for metrics
5total = tf.Variable(0.0, trainable=False, collections=[tf.GraphKeys.LOCAL_VARIABLES])
6count = tf.Variable(0, trainable=False, collections=[tf.GraphKeys.LOCAL_VARIABLES])
7
8with tf.Session() as sess:
9    # Initialize global variables (weights, biases)
10    sess.run(tf.global_variables_initializer())
11
12    # Initialize local variables (metrics counters)
13    sess.run(tf.local_variables_initializer())
14
15    # Or initialize everything at once
16    # sess.run(tf.group(
17    #     tf.global_variables_initializer(),
18    #     tf.local_variables_initializer()
19    # ))

Selective Initialization

python
1# Initialize specific variables only
2W = tf.Variable(tf.random_normal([3, 1]), name="weights")
3b = tf.Variable(tf.zeros([1]), name="bias")
4
5with tf.Session() as sess:
6    # Initialize only specific variables
7    sess.run(tf.variables_initializer([W]))
8
9    # W is initialized, b is not
10    print(sess.run(W))  # Works
11    # print(sess.run(b))  # ERROR: Uninitialized
12
13    # Initialize remaining variables
14    sess.run(tf.variables_initializer([b]))
15    print(sess.run(b))  # Now works
16
17    # Check which variables are uninitialized
18    uninitialized = sess.run(tf.report_uninitialized_variables())
19    print(uninitialized)  # Lists names of uninitialized variables

TensorFlow 2.x: Automatic Initialization

python
1import tensorflow as tf
2
3# TF 2.x: Eager execution is on by default
4# Variables are initialized immediately when created
5W = tf.Variable(tf.zeros([3, 1]), name="weights")
6b = tf.Variable(tf.zeros([1]), name="bias")
7
8# No session, no initialization call needed
9print(W.numpy())  # [[0.], [0.], [0.]]
10print(b.numpy())  # [0.]
11
12# Variables work directly in computations
13x = tf.constant([[1.0, 2.0, 3.0]])
14result = tf.matmul(x, W) + b
15print(result.numpy())  # [[0.]]
python
1# TF 2.x with Keras (most common pattern)
2model = tf.keras.Sequential([
3    tf.keras.layers.Dense(64, activation='relu', input_shape=(10,)),
4    tf.keras.layers.Dense(1)
5])
6
7# Variables are created and initialized automatically
8model.compile(optimizer='adam', loss='mse')
9model.summary()  # Shows parameters — all already initialized

In TF 2.x, eager execution creates and initializes variables immediately. There is no separate graph-building and execution phase.

Running TF 1.x Code in TF 2.x

python
1import tensorflow as tf
2
3# Compatibility mode for legacy code
4tf.compat.v1.disable_eager_execution()
5
6W = tf.compat.v1.Variable(tf.zeros([3, 1]))
7
8with tf.compat.v1.Session() as sess:
9    sess.run(tf.compat.v1.global_variables_initializer())
10    print(sess.run(W))

Use tf.compat.v1 namespace to run legacy TF 1.x code in TF 2.x environments.

Common Pitfalls

  • Using the deprecated function name: tf.initialize_all_variables() raises a deprecation warning in TF 1.x and does not exist in TF 2.x. Use tf.global_variables_initializer() for TF 1.x or remove initialization calls entirely for TF 2.x.
  • Forgetting to initialize in TF 1.x: Accessing a variable before calling the initializer raises FailedPreconditionError: Attempting to use uninitialized value. Always run the initializer before any computation.
  • Initializing before all variables are defined: tf.global_variables_initializer() captures a snapshot of variables at the time it is called. Variables created after the initializer is defined will not be initialized. Place the initializer call after all variable definitions.
  • Not distinguishing global from local variables: Metrics and streaming operations use local variables. tf.global_variables_initializer() does not initialize them. Call tf.local_variables_initializer() separately or combine both.
  • Calling initialization in TF 2.x: In TF 2.x with eager execution, calling tf.global_variables_initializer() is unnecessary and may cause confusion. Variables are initialized at creation time.

Summary

  • tf.initialize_all_variables() was deprecated in TF 0.12 — use tf.global_variables_initializer() in TF 1.x
  • In TF 2.x (eager execution), variable initialization is automatic — no initializer needed
  • Use tf.local_variables_initializer() for metrics and streaming variables
  • Use tf.variables_initializer([var_list]) for selective initialization
  • Use tf.compat.v1 namespace to run legacy TF 1.x code in TF 2.x
  • Keras handles all variable initialization internally — no manual calls required

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design