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.
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:
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:
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.
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.
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_stepwhen 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.iterationsis 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.iterationsusually plays the same role more naturally.
Related reading
- What does that code snippet signify tf.logging.set_verbositytf.logging.INFO in tensorflow code?
- What does the function control_dependencies do?
- What does the print of DMA and tensorflow mean? And is it possible to set them?
- What does the property losses of the Bayesian layers of TensorFlow Probability represent?
- What does the Brown clustering algorithm output mean?
- What does the capital letter 'J' mean in cost function Jθ?
- What does the -U option stand for in pip install -U
- what does the __file__ variable mean/do?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.