How to print weights 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
Inspecting model weights in TensorFlow helps verify that layers are connected as expected and training is actually updating parameters. Whether you use Keras APIs or custom training loops, the core approach is to access layer variables and print selected values or summaries. This is useful for debugging initialization, drift, and convergence issues.
Print Weights from Keras Layers
In TensorFlow 2, most users work with Keras models. Each trainable layer exposes weights through get_weights and variable objects.
To print actual numeric arrays:
get_weights returns NumPy arrays, which are easy to inspect or save.
Print Trainable and Non-Trainable Variables
Some layers contain both trainable and non-trainable variables. Batch normalization is a common example.
This distinction matters when you freeze layers during transfer learning.
Inspect Weights During Training
Logging weights every step is expensive, but periodic snapshots can reveal training behavior.
Printing summary statistics such as mean, std, min, and max is usually more practical than printing full matrices.
Print Weights in Custom Training Loops
With tf.GradientTape, you access the same variables directly.
This is useful when debugging gradient flow in advanced training logic.
Save Weight Snapshots for Offline Analysis
Instead of printing everything to console, save arrays to files for comparison across epochs.
Later you can load and compare:
This avoids noisy logs and helps version model state cleanly.
Inspect Weights by Layer Name Quickly
For larger models, filtering by layer name is easier than printing all variables.
You can also compare snapshots before and after one optimization step to confirm updates are happening.
For convolutional models, inspecting per-channel statistics can reveal dead filters early in training. A simple min and max check per kernel often surfaces initialization or learning-rate issues before accuracy metrics diverge.
Common Pitfalls
A common mistake is trying to read layer weights before the model is built. Call the model once with sample input or specify input shape so variables exist.
Another issue is printing huge matrices every iteration, which slows training and makes logs unusable. Print sampled values or summary statistics instead.
Developers also assume layer.weights are always trainable. Use trainable_variables and non_trainable_variables to avoid confusion, especially with normalization layers.
Summary
- Access TensorFlow weights through layer variables or
get_weights. - Build the model before inspecting weights.
- Distinguish trainable and non-trainable variables.
- Log concise weight summaries during training for practical debugging.
- Save snapshot files when detailed offline analysis is needed.
Related reading
- How to profile networks in TensorFlow v2
- how to programmatically determine available GPU memory with tensorflow?
- How to Properly Combine TensorFlow's Dataset API and Keras?
- How to properly feed specific tensor to keras model
- How to profile TensorFlow networks?
- How to properly manage memory and batch size with TensorFlow
- How to proceed with NLP task for recognizing intent and slots
- how to produce a tensor containing the indices of nonzero elements?
.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.