Is it possible to make a trainable variable not trainable?
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
In TensorFlow/Keras, you can make trainable variables non-trainable, but the correct method depends on whether you are toggling entire layers/models or individual variables. The most common pattern is setting layer.trainable = False, then recompiling the model so optimizer variable lists update. Developers often forget recompilation and assume freeze settings are active when they are not. Understanding when trainable flags are read is essential for transfer learning and staged training.
Core Sections
1. Freeze a layer or model
Compile after changing trainable flags.
2. Unfreeze later for fine-tuning
Lower learning rate is common during unfreeze phase.
3. Individual variable control
You can create variables with trainable=False:
For existing model parameters, manage at layer/model level unless building custom training loops.
4. Verify trainable variables
Always verify before training starts.
5. Custom training loop behavior
If using GradientTape, gradients are computed for watched trainable vars. Exclude frozen vars when applying gradients.
6. BatchNorm nuance
Freezing layers with BatchNorm can still involve moving statistics behavior depending on training/inference mode. Validate expected behavior explicitly in transfer-learning setups.
Validation and production readiness
A working snippet is only the first step. To make the solution dependable, validate behavior under representative inputs and operating conditions. Build a small test matrix that includes normal cases, boundary values, and malformed data so failure modes are explicit. If the topic involves time, concurrency, or networking, add at least one test that simulates delayed execution and one test that verifies timeout handling. This catches race conditions and environment-specific bugs that rarely appear in local happy-path runs.
Operational clarity matters as much as correctness. Document assumptions near the implementation: runtime version, required dependencies, expected timezone or locale rules, and platform limitations. Ambiguous assumptions are a major source of production incidents because teammates run the same logic under different defaults. Use structured logs around critical branches and external calls so debugging does not require ad hoc reproduction. Logs should include identifiers and concise context, but avoid sensitive payloads.
For recurring jobs or frequently executed code paths, add observability and guardrails. Define simple success metrics, retry boundaries, and explicit rollback or fallback behavior. Silent retries with no upper limit can hide systemic failures and increase downstream impact. Keep a lightweight pre-deploy checklist in source control so changes remain auditable and repeatable across environments.
Teams that treat these checks as part of the default implementation workflow usually spend less time on incident triage and more time shipping stable improvements.
Common Pitfalls
- Changing
trainableflags without recompiling compiled Keras model. - Assuming variable-level freeze works automatically inside prebuilt models.
- Forgetting to verify
model.trainable_variablesbefore training. - Unfreezing too many layers at high learning rate and destabilizing training.
- Ignoring BatchNorm-specific behavior during freeze/unfreeze phases.
Summary
Yes, trainable variables can be made non-trainable in TensorFlow/Keras, typically by freezing layers or models and recompiling. Use staged freeze/unfreeze workflows for transfer learning, and verify trainable variable lists before optimization. With explicit control and validation, parameter freezing becomes predictable and safe.
In collaborative teams, documenting this exact workflow and enforcing it with simple CI or runbook checks prevents repeated mistakes and keeps behavior consistent across development, staging, and production environments.
Related reading
- Is it possible to make tensorflow graph summary?
- Is it possible to merge multiple TensorFlow graphs into one?
- Is it possible to modify an existing TensorFlow computation graph?
- Is it possible to replace placeholder with a constant in an existing graph?
- is it possible to retrain a previously saved keras model?
- Is it possible to run tensorflow-gpu on a computer without a GPU or CUDA?
- Is it possible to save the class/label mapping directly inside a keras model.h5 file?
- Is it possible to see tensorboard over ssh?
.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.