TensorFlow
Checkpoint
Machine Learning
Deep Learning
Weights

How to read weights saved in tensorflow checkpoint file?

Master System Design with Codemia

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

Introduction

A TensorFlow checkpoint stores model variable values, not a friendly report of your network. If you want to inspect saved weights, the usual approaches are to list variable names directly from the checkpoint or restore those variables into a compatible model and then read them through the framework API.

The right choice depends on what you are debugging. Listing variables is best for inspection and migration work, while full restore is best when you want to run the model or verify that the checkpoint matches your architecture.

Understand What a Checkpoint Contains

A TensorFlow checkpoint usually comes as multiple files that share the same prefix, such as model.ckpt-10.index and model.ckpt-10.data-00000-of-00001. The prefix, not the individual filename, is what you pass to the TensorFlow loading APIs.

For example, if your directory contains these files:

text
checkpoints/model-10.index
checkpoints/model-10.data-00000-of-00001

then the checkpoint path is checkpoints/model-10.

List Variables Without Restoring a Model

If you only need to see what tensors were saved, tf.train.list_variables is the fastest way.

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

Typical output includes variable names and shapes, for example dense/kernel/.ATTRIBUTES/VARIABLE_VALUE [4, 8]. This is useful when you are comparing checkpoints, confirming layer names, or diagnosing why a restore operation does not match.

Read a Specific Tensor Directly

Once you know the variable name, load the checkpoint reader and pull out the actual tensor values.

python
1import tensorflow as tf
2
3checkpoint_path = "checkpoints/model-10"
4reader = tf.train.load_checkpoint(checkpoint_path)
5
6kernel = reader.get_tensor("dense/kernel/.ATTRIBUTES/VARIABLE_VALUE")
7print(kernel.shape)
8print(kernel)

This does not require rebuilding the model, which makes it ideal for one-off inspection scripts.

Restore Weights into a Keras Model

When you want to do more than inspect raw arrays, restore the checkpoint into a model with matching variable names and shapes.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu", name="dense"),
6    tf.keras.layers.Dense(1, name="output")
7])
8
9checkpoint = tf.train.Checkpoint(model=model)
10status = checkpoint.restore("checkpoints/model-10")
11status.expect_partial()
12
13weights, bias = model.get_layer("dense").get_weights()
14print(weights.shape)
15print(bias.shape)

The architecture must match the checkpoint. If the saved variables came from different layer names, a different nesting structure, or different tensor shapes, TensorFlow will not restore them cleanly.

TensorFlow 1 and TensorFlow 2 Differences

Older TensorFlow 1 code often used NewCheckpointReader from internal APIs or relied on graph-based variable names. In current TensorFlow 2 code, prefer the public APIs shown above.

If you are reading a legacy checkpoint, variable names may look different from what modern Keras code produces. That is another reason to list variables first before trying to restore them.

Use Inspection to Verify Migration Work

Checkpoint inspection is especially valuable when migrating a model between training codebases. You can confirm whether the new model still expects the same kernel and bias shapes, whether renamed layers changed checkpoint keys, and whether optimizer state is present.

A short verification script can save a lot of time:

python
1import tensorflow as tf
2
3expected = {
4    "dense/kernel/.ATTRIBUTES/VARIABLE_VALUE": (4, 8),
5    "dense/bias/.ATTRIBUTES/VARIABLE_VALUE": (8,),
6}
7
8for name, shape in tf.train.list_variables("checkpoints/model-10"):
9    if name in expected:
10        print(name, tuple(shape), expected[name])

That kind of check is simple, but it quickly tells you whether a checkpoint is compatible with the model you intend to restore.

Common Pitfalls

  • Passing the .index filename instead of the shared checkpoint prefix.
  • Expecting a checkpoint to contain full model architecture metadata like a SavedModel export.
  • Trying to restore into a model whose layer names or shapes no longer match the saved variables.
  • Hardcoding tensor names before listing what the checkpoint actually contains.
  • Ignoring restore warnings, which can hide partially loaded weights.

Summary

  • Use the checkpoint prefix, not the individual .index or .data filename.
  • Start with tf.train.list_variables to inspect saved tensors and shapes.
  • Use tf.train.load_checkpoint when you want to read one tensor directly.
  • Restore into a matching model when you need the weights in Keras or TensorFlow objects.
  • Inspect variable names first whenever you are debugging restores or migrating checkpoints.

Course illustration
Course illustration

All Rights Reserved.