Calling a Keras model on a TensorFlow tensor but keep weights
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
Calling a Keras model on a new TensorFlow tensor does not reset or lose weights by default. In most workflows, you can treat a model like a layer and reuse it inside a bigger model while keeping the same parameters. The important part is understanding weight sharing versus cloning so you control training behavior intentionally.
Calling a Model on a Tensor Shares the Same Weights
A compiled or loaded Keras model is callable. When you pass a tensor through it, Keras executes the existing layer graph and reuses the same variables.
No new independent weights are created just because you called base(x).
To verify sharing, compare variable object identity when reusing in another graph.
This prints True, confirming shared variables.
Freeze or Fine Tune Reused Weights
When using a pretrained model, you often need one of two modes:
- freeze base layers and train only new head,
- unfreeze some layers for fine tuning.
Freeze mode:
Fine-tune mode:
Low learning rates are usually safer when unfreezing pretrained layers.
Keep Training and Inference Behavior Correct
Some layers behave differently in training and inference, such as BatchNormalization and Dropout. When freezing a reused model, pass training=False during forward calls if you need stable inference behavior in that block.
If you omit this and train end-to-end, stateful layers may update moving statistics even when you intended full freeze semantics.
For custom loops with tf.GradientTape, only optimize intended variables:
This ensures frozen weights stay unchanged.
Clone When You Need Separate Weights
If you want the same architecture but independent parameters, clone the model.
Now clone starts with copied values but future updates do not affect base.
Use cloning for teacher-student models, ensembling, or experiments that require parameter divergence.
Verify Weight Preservation Programmatically
When debugging reuse behavior, compare weight snapshots before and after wrapping.
This confirms that calling on tensors does not mutate parameters unless training updates are applied.
Common Pitfalls
A common mistake is assuming repeated calls create new weight sets. They do not. Repeated calls reuse the same variables unless you explicitly clone.
Another issue is freezing layers after compiling and expecting optimizer behavior to update automatically. After changing trainable flags, recompile the model.
Developers also forget to manage training mode on reused submodels, especially with BatchNormalization, which can silently update internal statistics.
Summary
- Calling a Keras model on a tensor reuses existing weights by default.
- Use
trainable=Falseandtraining=Falsewhen you want frozen feature extraction. - Recompile after changing trainable flags.
- Clone models only when you need independent parameter sets.
- Validate trainable variable lists to confirm optimization scope.
Related reading
- Calling fit_generator multiple times in Keras
- Calling fit multiple times in Keras
- Can a Tensorflow variable be trained using the Tensorflow Keras functional API model? Can a Tensorflow operation be used in the functional API Model?
- can anyone give a tiny example to explain the params of tf.random.categorical?
- Calling a stateful LSTM as a functional model?
- Can a model be created on Spark batch and use it in Spark streaming?
- Can cond support TF ops with side effects?
- Can Dataset.map somehow parallelize over tf.py_func calls?
.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.