TensorFlow
variables
re-initialization
machine learning
programming

Re-initialize variables 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.

Practice ML system design

Introduction

Re-initializing variables in TensorFlow means resetting model state back to a known starting value. The exact method depends heavily on whether you are working in TensorFlow 2.x eager mode, Keras-based workflows, or older TensorFlow 1.x graph-and-session code.

The important point is that there is no single universal “reset everything” call that fits every style equally well. In modern TensorFlow, the most common answer is to recreate the model or reassign variables explicitly.

Reassign a tf.Variable Directly in TensorFlow 2.x

For a plain variable, the simplest reset is assign.

python
1import tensorflow as tf
2
3v = tf.Variable([1.0, 2.0, 3.0])
4print(v.numpy())
5
6v.assign([0.0, 0.0, 0.0])
7print(v.numpy())

This is enough when you know the exact value you want to restore.

If you want to reuse the original initializer logic:

python
1initializer = tf.keras.initializers.GlorotUniform()
2weights = tf.Variable(initializer(shape=(2, 3)))
3
4weights.assign(initializer(shape=(2, 3)))

That re-runs the initializer and assigns a fresh random value.

Resetting a Keras Model

If the variables belong to a Keras model, the cleanest answer is often to create a new model instance. That resets both the layer weights and the optimizer state if you also recreate the optimizer.

python
1import tensorflow as tf
2
3def build_model():
4    return tf.keras.Sequential([
5        tf.keras.layers.Dense(8, activation="relu"),
6        tf.keras.layers.Dense(1)
7    ])
8
9model = build_model()
10optimizer = tf.keras.optimizers.Adam()

To reset training from scratch:

python
model = build_model()
optimizer = tf.keras.optimizers.Adam()

This is often simpler and safer than trying to manually reset every layer variable and every optimizer slot variable.

Reset Model Weights Without Rebuilding Everything

If rebuilding the model object is inconvenient, you can store the initial weights and restore them later.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(8, activation="relu"),
5    tf.keras.layers.Dense(1)
6])
7
8model.build((None, 4))
9initial_weights = model.get_weights()
10
11# ... training happens here ...
12
13model.set_weights(initial_weights)

This is useful in experiments where you want to run several training loops from the same initialization.

However, note that this resets the model weights, not necessarily optimizer state. If the optimizer matters, recreate it too.

TensorFlow 1.x Style Reinitialization

In TensorFlow 1.x, variables lived in a graph and had to be initialized inside a session. Reinitialization often looked like this:

python
1import tensorflow.compat.v1 as tf
2tf.disable_eager_execution()
3
4v = tf.Variable(5.0)
5
6with tf.Session() as sess:
7    sess.run(tf.global_variables_initializer())
8    print(sess.run(v))
9
10    sess.run(v.assign(99.0))
11    print(sess.run(v))
12
13    sess.run(v.initializer)
14    print(sess.run(v))

For all variables:

python
sess.run(tf.global_variables_initializer())

For one variable:

python
sess.run(v.initializer)

That old style still matters if you maintain legacy code, but it is not the normal TensorFlow 2.x pattern.

Resetting Only Part of a Model

Sometimes you do not want to reset everything. For example, you may want to keep a frozen feature extractor and reset only the classification head.

That can be done by assigning fresh initial values only to selected variables:

python
for var in model.layers[-1].trainable_variables:
    initializer = tf.keras.initializers.GlorotUniform()
    var.assign(initializer(shape=var.shape, dtype=var.dtype))

This is useful in transfer learning or repeated fine-tuning experiments.

Reinitialization and Reproducibility

If you want the reset to be reproducible, set random seeds before creating or reinitializing variables:

python
import tensorflow as tf

tf.random.set_seed(1234)

Without that, a “reinitialized” model may start from a different random point every time, which might be exactly what you want, or exactly what you do not want.

Common Pitfalls

One common mistake is resetting model weights but forgetting the optimizer state. Optimizers such as Adam keep internal moments, so a “fresh” training run may not actually be fresh unless the optimizer is reset too.

Another mistake is assuming TensorFlow 1.x session-based initializer code is the right pattern in TensorFlow 2.x. In modern code, explicit assign, set_weights, or model recreation is usually clearer.

It is also easy to reinitialize only some variables accidentally when the real goal was a full experiment reset. Be explicit about the scope of the reset.

Finally, if you rely on initial random weights for fair experiment comparison, capture or control the initialization process carefully instead of assuming it will repeat by accident.

Summary

  • In TensorFlow 2.x, reset plain variables with assign.
  • For Keras models, recreating the model and optimizer is often the cleanest full reset.
  • 'get_weights() and set_weights() are useful when you want to restore a saved initial state.'
  • In TensorFlow 1.x, reinitialization usually happened through variable initializers inside a session.
  • Always think about optimizer state and reproducibility when resetting variables.

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.