checkpoint
variable names
variable values
data retrieval
machine learning

How do I find the variable names and values that are saved in a checkpoint?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In TensorFlow, the easiest way to inspect a checkpoint is to list the variable names first and then read individual tensors by name. The main tools are tf.train.list_variables() for names and shapes, and tf.train.load_checkpoint() for fetching the stored values.

Start by Listing What Is Inside the Checkpoint

If you already know the checkpoint path, list the variables before trying to read values. That tells you the exact names TensorFlow saved.

python
1import tensorflow as tf
2
3checkpoint_path = tf.train.latest_checkpoint("./checkpoints")
4print("Checkpoint:", checkpoint_path)
5
6for name, shape in tf.train.list_variables(checkpoint_path):
7    print(name, shape)

This step is important because TensorFlow variable names inside checkpoints are often more detailed than the names you expect from model code, especially in object-based checkpoints.

Read Individual Tensor Values

Once you know the variable name, use a checkpoint reader to fetch the actual tensor value.

python
1import tensorflow as tf
2
3checkpoint_path = tf.train.latest_checkpoint("./checkpoints")
4reader = tf.train.load_checkpoint(checkpoint_path)
5
6for name, shape in tf.train.list_variables(checkpoint_path):
7    value = reader.get_tensor(name)
8    print("NAME:", name)
9    print("SHAPE:", shape)
10    print("VALUE:", value)

This is the most direct inspection workflow in TensorFlow 2.

A Small End-to-End Example

It helps to see where these names come from. In object-based checkpoints, the names often reflect object attributes.

python
1import tensorflow as tf
2
3class MyModel(tf.Module):
4    def __init__(self):
5        super().__init__()
6        self.weight = tf.Variable([[1.0, 2.0], [3.0, 4.0]], name="weight")
7        self.bias = tf.Variable([0.5, -0.5], name="bias")
8
9model = MyModel()
10ckpt = tf.train.Checkpoint(model=model)
11path = ckpt.save("./demo_ckpt/ckpt")
12
13for name, shape in tf.train.list_variables(path):
14    print(name, shape)
15
16reader = tf.train.load_checkpoint(path)
17for name, _ in tf.train.list_variables(path):
18    print(name, reader.get_tensor(name))

When you run this, the names will come from the checkpoint object graph, not just from the short weight and bias labels you see in Python source.

Why the Names Sometimes Look Strange

TensorFlow 2 checkpoints are object-based, which means paths are derived from tracked object attributes. That is why names may look more like:

  • object paths
  • layer attribute names
  • internal variable keys

than like the plain variable names you remember from model code.

This often surprises people migrating from older TensorFlow 1 workflows, where variable naming conventions felt more graph-centric.

Use the Exact Name Returned by list_variables

If you try to guess a name instead of reading it from the checkpoint metadata first, you will often get a key error or the wrong tensor. The safest sequence is:

  1. call list_variables
  2. copy the exact name
  3. call get_tensor(name)

That works for both model weights and optimizer state stored in the checkpoint.

Checkpoint Directory Versus Specific File

TensorFlow APIs can work with either:

  • a checkpoint directory that has checkpoint metadata
  • a fully resolved checkpoint prefix

Using tf.train.latest_checkpoint() is often the safest option because it resolves the newest checkpoint prefix correctly.

python
checkpoint_path = tf.train.latest_checkpoint("./checkpoints")

If this returns None, the issue is not variable inspection. It means TensorFlow did not find a valid checkpoint there.

SavedModel Is a Different Question

People sometimes confuse checkpoints with SavedModel exports. They are related but not identical:

  • checkpoints store variable state
  • SavedModel stores an exportable model artifact with signatures and weights

If your goal is “inspect raw variables,” the checkpoint APIs are the right place to start.

Common Pitfalls

  • Guessing variable names instead of listing them from the checkpoint first.
  • Pointing list_variables at the wrong directory and then assuming the checkpoint is empty.
  • Forgetting that TensorFlow 2 uses object-based checkpoint naming, which can look different from layer variable names in source code.
  • Expecting SavedModel inspection tools to answer raw checkpoint questions directly.
  • Printing every tensor value from a huge checkpoint without filtering, which can produce an overwhelming amount of output.

Summary

  • Use tf.train.list_variables() to discover the names and shapes saved in a checkpoint.
  • Use tf.train.load_checkpoint() and get_tensor() to inspect actual values.
  • Let tf.train.latest_checkpoint() resolve the current checkpoint prefix when possible.
  • Expect TensorFlow 2 checkpoint names to reflect object paths, not just short variable names.
  • List first, then read values by exact name.

Course illustration
Course illustration

All Rights Reserved.