TensorFlow
init_from_checkpoint
tf.Variable
machine learning
neural networks

tf.train.init_from_checkpoint does not initialize variables created with tf.Variable

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.init_from_checkpoint is a TensorFlow 1 style utility for restoring variables by checkpoint key during graph construction. When it seems not to initialize variables created with tf.Variable, the real issue is usually not just the class name. It is usually a mismatch in naming, execution mode, or initialization order.

What init_from_checkpoint Actually Does

This API does not load a checkpoint the same way a Saver.restore call or a TensorFlow 2 checkpoint restore does. Instead, it rewrites the initializer for matching graph variables so that when the variable is initialized, the value comes from the checkpoint.

That leads to three important consequences:

  • it is designed for TensorFlow 1 graph mode
  • it depends on checkpoint keys matching current variable names or an assignment map
  • it must be called after variables are created and before initialization runs

If any of those assumptions are broken, the variable keeps its normal initializer and looks as if the checkpoint logic never happened.

Use It in Graph Mode With Predictable Variable Names

The most reliable pattern is to use tf.compat.v1.get_variable inside named scopes, then map checkpoint names explicitly.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5with tf.compat.v1.variable_scope("new_scope"):
6    weights = tf.compat.v1.get_variable("weights", shape=[2, 2])
7    bias = tf.compat.v1.get_variable("bias", shape=[2])
8
9tf.compat.v1.train.init_from_checkpoint(
10    "/tmp/model.ckpt",
11    {
12        "old_scope/weights": "new_scope/weights",
13        "old_scope/bias": "new_scope/bias",
14    },
15)
16
17with tf.compat.v1.Session() as sess:
18    sess.run(tf.compat.v1.global_variables_initializer())
19    print(sess.run(weights))
20    print(sess.run(bias))

This works because the new graph variables have stable names and the assignment map tells TensorFlow exactly which checkpoint tensor should initialize each one.

Why Plain tf.Variable Often Causes Confusion

A standalone tf.Variable is not automatically wrong, but it is easier to misconfigure. In older TensorFlow code, tf.Variable often appears without the naming discipline provided by variable_scope and get_variable, which makes checkpoint matching fragile.

For example:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5weights = tf.Variable(tf.zeros([2, 2]), name="weights")
6
7tf.compat.v1.train.init_from_checkpoint(
8    "/tmp/model.ckpt",
9    {"old_scope/weights": "weights"},
10)

This can work only if the checkpoint key and the current graph variable line up exactly. If the real variable name ends up as weights_1, or lives under a different scope, or if eager execution is enabled, the mapping no longer matches what you think it does.

So the more precise statement is:

  • 'init_from_checkpoint works best with TF1 graph variables that have stable checkpoint-compatible names'
  • 'tf.Variable often makes that setup harder to reason about'

Check the Checkpoint Keys First

Before blaming the variable type, inspect the checkpoint contents:

python
1import tensorflow as tf
2
3for name, shape in tf.train.list_variables("/tmp/model.ckpt"):
4    print(name, shape)

If the checkpoint contains old_scope/weights but your graph variable is named model/weights, nothing will be initialized unless you map it correctly.

This is also where shape mismatches show up. Even with the right name, a variable cannot be initialized from a checkpoint tensor of the wrong shape.

Prefer Modern Restore APIs in TensorFlow 2

If you are writing new TensorFlow 2 code, use object-based checkpoint restore instead of init_from_checkpoint.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential(
4    [
5        tf.keras.layers.Dense(4),
6        tf.keras.layers.Dense(1),
7    ]
8)
9
10checkpoint = tf.train.Checkpoint(model=model)
11checkpoint.restore("/tmp/ckpt-1").expect_partial()

TensorFlow 2 checkpoints track objects rather than relying purely on graph variable names, which makes restoration behavior easier to understand in modern code.

Common Pitfalls

  • Using init_from_checkpoint in eager mode and expecting TensorFlow 1 graph behavior.
  • Calling it after variable initialization has already run.
  • Assuming tf.Variable is the whole problem when the real issue is a checkpoint key mismatch.
  • Forgetting to inspect checkpoint tensor names before writing the assignment map.
  • Using this TF1 utility in new TF2 code where tf.train.Checkpoint is the better tool.

Summary

  • 'tf.train.init_from_checkpoint is a TensorFlow 1 graph-mode initializer rewrite utility, not a general-purpose restore call.'
  • It works only when variable creation, naming, and initialization order line up correctly.
  • 'tf.Variable can be used, but it often makes naming and mapping less predictable than tf.compat.v1.get_variable.'
  • Inspect checkpoint keys and use an explicit assignment map when names differ.
  • For modern TensorFlow 2 code, prefer tf.train.Checkpoint or Keras model checkpoint restore APIs.

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.