Keras
machine learning
model interpretation
neural networks
biases

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.

Practice ML system design

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.

python
1import numpy as np
2from tensorflow import keras
3
4model = keras.Sequential(
5    [
6        keras.layers.Input(shape=(3,)),
7        keras.layers.Dense(4, activation="relu", name="hidden"),
8        keras.layers.Dense(1, name="output"),
9    ]
10)
11
12model.compile(optimizer="adam", loss="mse")
13
14x = np.random.rand(20, 3)
15y = np.random.rand(20, 1)
16model.fit(x, y, epochs=2, verbose=0)
17
18weights, biases = model.get_layer("hidden").get_weights()
19
20print(weights.shape)
21print(biases.shape)
22print(biases)

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.

python
hidden_bias = model.get_layer("hidden").bias
print(hidden_bias.numpy())

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.

python
for layer in model.layers:
    if hasattr(layer, "bias") and layer.bias is not None:
        print(layer.name, layer.bias.numpy())

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:

  • 'Dense usually has a bias unless use_bias=False'
  • 'Conv2D usually has a bias unless use_bias=False'
  • 'Dropout has no trainable weights'
  • some layers expose trainable parameters that are not ordinary bias vectors

Here is a simple example with use_bias=False:

python
1layer = keras.layers.Dense(4, use_bias=False)
2layer.build((None, 3))
3
4print(layer.get_weights())  # only the kernel
5print(layer.bias)           # None

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.

python
1model.save("demo_model.keras")
2loaded = keras.models.load_model("demo_model.keras")
3
4print(loaded.get_layer("output").bias.numpy())

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.

python
output_bias = model.get_layer("output").bias.numpy()
print(output_bias)

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
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.