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.
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.
This is enough when you know the exact value you want to restore.
If you want to reuse the original initializer logic:
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.
To reset training from scratch:
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.
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:
For all variables:
For one variable:
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:
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:
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 withassign. - For Keras models, recreating the model and optimizer is often the cleanest full reset.
- '
get_weights()andset_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
- Re-train a frozen .pb model in TensorFlow
- Read big train/validation/test datasets in tensorflow
- Read in Large CSV File and feed into TensorFlow
- Read mixed data types from CSV row via tf.TextLineReader and tf.decode_csv
- Re-train model with new classes
- Read csv files in a MLFlow pipeline
- Read mnist images into Tensorflow
- Read only mode in keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.