How can I get biases from a trained model 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
In Keras, biases are stored as part of a layer's weights. For layers such as Dense and Conv2D, you can inspect them after training by reading the layer's weights or by accessing the bias variable directly. The two important questions are whether the layer actually has a bias term and whether you want one layer's bias or every bias vector in the model.
Use get_weights() for the Basic Case
For a standard dense layer, get_weights() usually returns two arrays: the kernel matrix and the bias vector.
For a dense layer with four units, the bias vector has shape (4,).
Access the Bias Variable Directly
If you only want the bias and not the kernel weights, reading the bias variable directly is often clearer.
This is especially convenient in notebooks and debugging sessions where you want to inspect one parameter without unpacking the whole weight list.
Iterate Through the Whole Model
If you need every bias vector in the model, iterate through the layers and check whether each one exposes a bias.
This pattern is safer than assuming every layer returns two arrays from get_weights().
Remember That Not Every Layer Has Biases
Bias terms are common, but they are not universal.
Examples:
- '
Denseusually has a bias unlessuse_bias=False' - '
Conv2Dusually has a bias unlessuse_bias=False' - '
Dropouthas no trainable weights' - some layers expose trainable parameters that are not ordinary bias vectors
Here is a simple example with use_bias=False:
This is why "just take the second value" is not a reliable rule across all layer types.
Biases Are Available After Reloading Too
You do not need special training-time code to inspect biases later. Once the model is loaded, the same layer access works.
That makes post-training analysis and debugging straightforward.
Use Layer Names for Stable Access
Small scripts often use model.layers[0], but named access is safer in real projects.
If you later insert another layer near the front of the model, named access still finds the intended layer. Index-based access can quietly inspect the wrong weights.
Common Pitfalls
The most common pitfall is assuming every layer has a bias vector. Some layers do not, and some are built with use_bias=False.
Another issue is unpacking get_weights() as though it always returns exactly two arrays. That happens for many common layers, but not for all of them.
Teams also often rely on numeric layer indexes in models that change over time. That makes it easy to read the wrong bias values after a refactor.
Finally, do not confuse bias vectors with other trainable parameters such as normalization statistics or recurrent-state weights.
Summary
- In Keras, biases are part of a layer's weights and can be read after training.
- Use
layer.get_weights()when you want the full weight arrays. - Use
layer.bias.numpy()when you want the bias directly. - Check that the layer actually has a bias term before assuming it exists.
- Prefer named layer access over index-based access in evolving models.
Related reading
- How can I implement a custom `RNN` specifically an ESN in Tensorflow?
- How can I implement a weighted cross entropy loss in tensorflow using sparse_softmax_cross_entropy_with_logits
- How can I implement dilated convolution in keras?
- How can I know whether a tensorflow tensor is in cuda or cpu?
- How can I get the relative importance of features of a logistic regression for a particular prediction?
- How can I implement incremental training for xgboost?
- How can I list all Tensorflow variables a node depends on?
- How can I make a trainable parameter in keras?
.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.