TensorFlow
tf.train.get_global_step
global step
machine learning
Python

What does tf.train.get_global_step do in TensorFlow?

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.train.get_global_step() is a TensorFlow 1.x style helper for retrieving the graph's shared training-step variable. That variable usually tracks how many optimizer updates have happened. The function matters mainly in graph-based training code, where checkpointing, learning-rate schedules, and logging often depend on a consistent step counter.

What the Global Step Represents

A global step is typically a scalar integer variable that increments once per training update. It is not the epoch number and not the number of examples processed directly.

If you train with mini-batches, then:

  • one optimizer update usually increases global step by one
  • one epoch may contain many global steps
  • checkpoint names and summary events often use that counter

That is why old TensorFlow utilities treat the global step as shared training metadata.

What tf.train.get_global_step() Actually Does

The function looks for the global step variable in the graph collections and returns it if present. It does not invent a value by reading your optimizer history or scanning the graph.

A common pattern is to create the step first and then retrieve it later:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5global_step = tf.compat.v1.train.get_or_create_global_step()
6x = tf.compat.v1.Variable(3.0)
7loss = tf.square(x - 10.0)
8train_op = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(
9    loss,
10    global_step=global_step,
11)
12
13retrieved = tf.compat.v1.train.get_global_step()
14
15with tf.compat.v1.Session() as sess:
16    sess.run(tf.compat.v1.global_variables_initializer())
17    print("before:", sess.run(retrieved))
18    sess.run(train_op)
19    print("after:", sess.run(retrieved))

In this example, minimize increments the global step because you passed it through the global_step argument.

Why Old TensorFlow Code Uses It

Graph-era TensorFlow code needed a standard place to find the step counter. Several APIs expected it:

  • learning-rate decay functions
  • checkpoint managers
  • summary hooks
  • Estimator-based training utilities

For example, a decaying learning rate can be tied to the global step:

python
1learning_rate = tf.compat.v1.train.exponential_decay(
2    learning_rate=0.1,
3    global_step=global_step,
4    decay_steps=100,
5    decay_rate=0.96,
6    staircase=True,
7)

As the step count increases, the schedule changes automatically.

get_global_step Versus get_or_create_global_step

These two functions are related but not identical.

  • 'get_global_step() retrieves the existing step variable from the graph'
  • 'get_or_create_global_step() retrieves it if present or creates it if absent'

That difference matters because older code often assumes the step already exists. If it does not, get_global_step() can return None.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5step = tf.compat.v1.train.get_global_step()
6print(step)

If no global-step variable has been created, the result is not useful for training logic.

TensorFlow 2 Perspective

In TensorFlow 2, eager execution and Keras-centered training changed how most users think about training state. Instead of retrieving a graph collection entry, you usually read the optimizer's iteration counter or keep your own variable.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(1)
5])
6
7optimizer = tf.keras.optimizers.Adam()
8model.compile(optimizer=optimizer, loss="mse")
9
10print(int(optimizer.iterations))

optimizer.iterations serves the same conceptual purpose for many modern training workflows.

When You Still Encounter This API

You will most often see tf.train.get_global_step() in:

  • TensorFlow 1.x tutorials
  • legacy graph-mode codebases
  • Estimator programs
  • migration code using tf.compat.v1

If you are maintaining older TensorFlow code, understanding this function helps you reason about resume logic, checkpoint names, and learning-rate schedules.

Common Pitfalls

  • Assuming the function creates a global step automatically. It usually does not; use get_or_create_global_step when creation is required.
  • Confusing global step with epoch count. One epoch may include many optimizer steps.
  • Forgetting to pass the global step into the optimizer or training op, so the counter never increments.
  • Restoring model weights without restoring the step counter, which can break learning-rate schedules or resume behavior.
  • Trying to force graph-era APIs into ordinary TensorFlow 2 Keras code when optimizer.iterations is simpler and clearer.

Summary

  • 'tf.train.get_global_step() retrieves the graph's shared training-step variable in TensorFlow 1.x style code.'
  • The global step usually counts optimizer updates, not epochs.
  • Many older TensorFlow utilities use it for checkpointing, logging, and learning-rate schedules.
  • 'get_global_step() retrieves; get_or_create_global_step() retrieves or creates.'
  • In TensorFlow 2, optimizer.iterations usually plays the same role more naturally.

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.