How do I get the weights of a layer in Keras?
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
Keras layers expose their parameters directly, so inspecting weights is usually straightforward once the layer has been built. The two things that trip people up most often are choosing the right API and remembering that unbuilt layers do not yet have initialized weight arrays.
The Simplest Way: get_weights()
For a built layer, get_weights() returns a list of NumPy arrays.
For a dense layer, the list usually contains:
- the kernel matrix
- the bias vector
The exact shapes depend on the input size and the number of units.
Make Sure the Layer Is Built First
Keras only creates weight variables once the layer knows its input shape. If the layer has not been built yet, get_weights() may return an empty list.
In a full model, calling the model once or defining an input shape usually builds everything automatically.
Access Tensor Variables Directly
If you want TensorFlow variables instead of NumPy arrays, inspect weights, trainable_weights, or non_trainable_weights.
This is useful when you want to work inside TensorFlow code rather than immediately converting to NumPy.
Modify the Weights
You can also set weights manually, as long as the shapes match exactly.
This is useful for experiments, weight transfer, or deterministic initialization during tests.
Access Weights by Index or Name
If you do not name the layer, you can still access it by index:
But named access is often better in larger models:
Layer names make inspection code more stable when the model architecture changes slightly.
Custom Layers Work the Same Way
If you define your own layer and add weights with add_weight, those variables show up through the same APIs.
So once you understand the standard layer APIs, custom layers follow the same pattern.
Common Pitfalls
The biggest mistake is calling get_weights() before the layer or model has been built. No input shape means no initialized weights yet.
Another issue is assuming every layer has both kernel and bias arrays. Some layers have different parameter structures, and some have no trainable weights at all.
Developers also mix up TensorFlow variables with NumPy arrays. layer.weights returns variables, while layer.get_weights() returns NumPy copies of their values.
Finally, if you use set_weights(...), the shapes must match exactly. Keras will not silently reshape the arrays for you.
Summary
- Use
layer.get_weights()to retrieve a layer's parameter values as NumPy arrays. - Make sure the layer is built before expecting weights to exist.
- Use
layer.weightsortrainable_weightswhen you want TensorFlow variables. - Named layers are easier to inspect than index-based access in larger models.
- When setting weights manually, the replacement arrays must match the original shapes exactly.
Related reading
- How do I initialize weights in PyTorch?
- How do I initialize weights in PyTorch?
- How do I keep track of the time the CPU is used vs the GPUs for deep learning?
- How do I know if tensorflow using cuda and cudnn or not?
- How do I go from Pandas DataFrame to Tensorflow BatchDataset for NLP?
- How do I install tensorflow_text?
- How do I install TensorFlow's tensorboard?
- How do I load a local model with torch.hub.load?
.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.