<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/training/training_util.py#L258-L326) |

Returns and create (if necessary) the global step tensor.

    tf.compat.v1.train.get_or_create_global_step(
        graph=None
    )

<br />

## Migrate to TF2

<br />

> [!CAUTION]
> **Caution:** This API was designed for TensorFlow v1. Continue reading for details on how to migrate from this API to a native TensorFlow v2 equivalent. See the [TensorFlow v1 to TensorFlow v2 migration guide](https://www.tensorflow.org/guide/migrate) for instructions on how to migrate the rest of your code.

With the deprecation of global graphs, TF no longer tracks variables in
collections. In other words, there are no global variables in TF2. Thus, the
global step functions have been removed (`get_or_create_global_step`,
`create_global_step`, `get_global_step`) . You have two options for migrating:

1. Create a Keras optimizer, which generates an `iterations` variable. This variable is automatically incremented when calling `apply_gradients`.
2. Manually create and increment a [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable).

Below is an example of migrating away from using a global step to using a
Keras optimizer:

Define a dummy model and loss:

    def compute_loss(x):
      v = tf.Variable(3.0)
      y = x * v
      loss = x * 5 - x * v
      return loss, [v]

Before migrating:

    g = tf.Graph()
    with g.as_default():
      x = tf.compat.v1.placeholder(tf.float32, [])
      loss, var_list = compute_loss(x)
      global_step = tf.compat.v1.train.get_or_create_global_step()
      global_init = tf.compat.v1.global_variables_initializer()
      optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.1)
      train_op = optimizer.minimize(loss, global_step, var_list)
    sess = tf.compat.v1.Session(graph=g)
    sess.run(global_init)
    print("before training:", sess.run(global_step))
    before training: 0
    sess.run(train_op, feed_dict={x: 3})
    print("after training:", sess.run(global_step))
    after training: 1

Migrating to a Keras optimizer:

    optimizer = tf.keras.optimizers.SGD(.01)
    print("before training:", optimizer.iterations.numpy())
    before training: 0
    with tf.GradientTape() as tape:
      loss, var_list = compute_loss(3)
      grads = tape.gradient(loss, var_list)
      optimizer.apply_gradients(zip(grads, var_list))
    print("after training:", optimizer.iterations.numpy())
    after training: 1

<br />

<br />

## Description

### Used in the notebooks

| Used in the guide | Used in the tutorials |
|---|---|
| - [Migrating model checkpoints](https://www.tensorflow.org/guide/migrate/migrating_checkpoints) - [Debug a TensorFlow 2 migrated training pipeline](https://www.tensorflow.org/guide/migrate/migration_debugging) | - [Multi-worker training with Estimator](https://www.tensorflow.org/tutorials/distribute/multi_worker_with_estimator) - [Checkpointer and PolicySaver](https://www.tensorflow.org/agents/tutorials/10_checkpointer_policysaver_tutorial) - [Exploring the TF-Hub CORD-19 Swivel Embeddings](https://www.tensorflow.org/hub/tutorials/cord_19_embeddings) - [Linear Mixed-Effect Regression in {TF Probability, R, Stan}](https://www.tensorflow.org/probability/examples/HLM_TFP_R_Stan) - [Graph-based Neural Structured Learning in TFX](https://www.tensorflow.org/tfx/tutorials/tfx/neural_structured_learning) |

<br />

<br />

<br />

| ## Args ||
|---|---|
| `graph` | The graph in which to create the global step tensor. If missing, use default graph. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| The global step tensor. ||

<br />