TensorFlow
global_variables_initializer
deep learning
machine learning
neural networks

What does tf.global_variables_initializer do under the hood?

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

In TensorFlow 1 graph mode, defining a variable does not immediately create its runtime value. tf.global_variables_initializer builds one operation that initializes all global variables currently registered in the graph. Understanding this behavior is important when maintaining legacy TensorFlow code or mixed TensorFlow 1 compatibility paths.

Variable Creation in Graph Mode

In graph execution, variable creation adds symbolic nodes and initializer ops, but values are not materialized until a session runs those initializers.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5w = tf.compat.v1.get_variable("w", shape=[2], initializer=tf.ones_initializer())
6b = tf.compat.v1.get_variable("b", shape=[1], initializer=tf.zeros_initializer())
7
8print("w init op:", w.initializer)
9print("b init op:", b.initializer)

Each variable has its own init operation. Running all manually is possible but error-prone.

What global_variables_initializer Builds

tf.compat.v1.global_variables_initializer() collects global variables from the graph collection and returns a grouped initialization op.

python
init_op = tf.compat.v1.global_variables_initializer()
print(init_op)

When run in a session, this grouped op executes variable initializer assignments.

python
with tf.compat.v1.Session() as sess:
    sess.run(init_op)
    print(sess.run([w, b]))

Without running init or restore first, variable reads fail with uninitialized-variable errors.

Snapshot Timing Is Important

The initializer op captures variables that exist when the op is created. Variables added later are not automatically included.

python
init_op = tf.compat.v1.global_variables_initializer()
late = tf.compat.v1.get_variable("late", shape=[1], initializer=tf.ones_initializer())

In this pattern, late is excluded from init_op. Solutions:

  • recreate global_variables_initializer after creating all variables
  • or run late.initializer separately

This is a common source of bugs in modular graph-construction code.

Relationship with Checkpoint Restore

Initialization and restore are separate operations. Typical flow:

  1. build graph
  2. initialize variables
  3. restore checkpoint values where available
python
1saver = tf.compat.v1.train.Saver()
2
3with tf.compat.v1.Session() as sess:
4    sess.run(tf.compat.v1.global_variables_initializer())
5    # saver.restore(sess, "model.ckpt")

Restore overwrites matching variables with checkpoint data, while unmatched variables keep initialized defaults.

Debugging Uninitialized Variables

TensorFlow provides helper ops to inspect initialization status.

python
1report = tf.compat.v1.report_uninitialized_variables()
2
3with tf.compat.v1.Session() as sess:
4    print(sess.run(report))

This is useful when graph assembly spans multiple modules and initialization order is uncertain.

Variable Collections and Scope Behavior

global_variables_initializer relies on global variable collections. Variables outside expected collections or created through custom flows may require explicit handling.

When building reusable model components, keep variable creation conventions consistent and avoid hidden side effects in helper functions.

TensorFlow 2 Context

In TensorFlow 2 eager execution, variables initialize when constructed, so this function is usually unnecessary unless using compatibility graph APIs.

python
1import tensorflow as tf
2
3v = tf.Variable([1.0, 2.0])
4print(v.numpy())

In mixed codebases, isolate graph-mode modules clearly so initialization responsibilities remain explicit.

Minimal End-to-End Graph Example

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, shape=[None, 1])
6w = tf.compat.v1.get_variable("w2", shape=[1, 1], initializer=tf.ones_initializer())
7y = tf.matmul(x, w)
8
9with tf.compat.v1.Session() as sess:
10    sess.run(tf.compat.v1.global_variables_initializer())
11    out = sess.run(y, feed_dict={x: [[2.0], [3.0]]})
12    print(out)

If you remove the initializer call, execution fails because w has no runtime value.

Migration Notes

When migrating legacy TensorFlow 1 code:

  • define variables before building grouped init ops
  • centralize initialization in one startup path
  • prefer checkpoint restore for trained models
  • gradually move modules to eager style where possible

A phased migration avoids hard-to-debug execution-mode mismatches.

Common Pitfalls

A common pitfall is assuming variable definition implies automatic runtime initialization in graph mode.

Another pitfall is creating variables after building init op and forgetting to reinitialize them.

A third pitfall is confusing checkpoint restore with initializer semantics and order.

Teams also mix eager and graph assumptions in the same module, causing inconsistent behavior.

Summary

  • global_variables_initializer builds a grouped init op for current global variables in graph mode.
  • Variables need explicit initialization or restore before use in TensorFlow 1 sessions.
  • Init op captures a snapshot of variables at creation time.
  • Restore and initialization are separate steps with different responsibilities.
  • Clear initialization order is key for reliable legacy TensorFlow execution.

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

All Rights Reserved.