machine learning
neural networks
trainable variables
model training
AI techniques

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.

Practice ML system design

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

python
1for layer in base_model.layers:
2    layer.trainable = False
3
4model = tf.keras.Sequential([base_model, head])
5model.compile(optimizer="adam", loss="sparse_categorical_crossentropy")

Compile after changing trainable flags.

2. Unfreeze later for fine-tuning

python
1for layer in base_model.layers[-20:]:
2    layer.trainable = True
3
4model.compile(optimizer=tf.keras.optimizers.Adam(1e-5), loss="sparse_categorical_crossentropy")

Lower learning rate is common during unfreeze phase.

3. Individual variable control

You can create variables with trainable=False:

python
v = tf.Variable([1.0, 2.0], trainable=False)

For existing model parameters, manage at layer/model level unless building custom training loops.

4. Verify trainable variables

python
print(len(model.trainable_variables))
for v in model.trainable_variables[:5]:
    print(v.name)

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.

python
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))

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.

text
1release_checklist:
2  - tests cover edge cases and failure paths
3  - runtime and dependency versions documented
4  - logs/metrics confirm expected execution path
5  - retries and timeouts are bounded
6  - rollback or fallback plan is defined

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 trainable flags without recompiling compiled Keras model.
  • Assuming variable-level freeze works automatically inside prebuilt models.
  • Forgetting to verify model.trainable_variables before 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
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.