Keras
weights
numpy
machine learning
deep learning

How to set weights in Keras with a numpy array?

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, you set weights with NumPy arrays by calling set_weights on a layer or on the whole model. The important part is not the method name. It is making sure the shapes, order, and data types of your arrays match the variables that the layer has already created.

Inspect the Existing Weight Shapes First

Before setting anything, ask the layer or model what shapes it expects.

python
1import keras
2import numpy as np
3from keras import layers
4
5layer = layers.Dense(3, input_shape=(2,))
6layer.build((None, 2))
7
8for array in layer.get_weights():
9    print(array.shape)

For a dense layer with input size 2 and output size 3, you will usually see:

  • a kernel of shape (2, 3)
  • a bias of shape (3,)

That existing structure is the template your NumPy arrays must match.

Set Weights on a Single Layer

Once the layer is built, you can pass a list of NumPy arrays to set_weights.

python
1kernel = np.array([
2    [0.1, 0.2, 0.3],
3    [0.4, 0.5, 0.6],
4], dtype="float32")
5
6bias = np.array([0.01, 0.02, 0.03], dtype="float32")
7
8layer.set_weights([kernel, bias])
9print(layer.get_weights())

The arrays must match both the expected order and the expected shapes. Keras does not guess which array belongs to which variable.

Build the Model Before Setting Weights

A common mistake is trying to call set_weights before the model or layer has created its variables. In Keras, weights do not exist until the layer is built.

python
1model = keras.Sequential([
2    layers.Input(shape=(2,)),
3    layers.Dense(3),
4])
5
6# The model is built because an input shape was provided.
7model.layers[0].set_weights([kernel, bias])

If the model was created without enough shape information, call it once on sample input or build it explicitly before setting weights.

Set Weights on the Whole Model

You can also use model.set_weights(...), but that requires one flat list containing every weight array in the model in exactly the right order.

python
1model = keras.Sequential([
2    layers.Input(shape=(2,)),
3    layers.Dense(3),
4    layers.Dense(1),
5])
6
7for array in model.get_weights():
8    print(array.shape)

For most manual experiments, setting weights layer by layer is easier to reason about because the mapping from array to variable stays obvious.

Use This for Initialization, Transfer, and Experiments

Direct weight setting is useful when:

  • copying parameters from another model
  • loading custom weights from external code
  • testing a layer with known values
  • reproducing an example exactly

It is less useful as a substitute for normal training. If you are simply loading a saved Keras model, the model-loading APIs are usually cleaner than rebuilding the architecture and calling set_weights manually.

Watch Shape and Dtype Errors Carefully

Most set_weights failures come from shape mismatches or from trying to load arrays into a layer that has a different configuration from the one that produced them.

python
1wrong_kernel = np.array([[1.0, 2.0]], dtype="float32")
2
3try:
4    layer.set_weights([wrong_kernel, bias])
5except ValueError as exc:
6    print(exc)

That error is useful. It tells you the layer and the arrays do not agree structurally.

Common Pitfalls

  • Calling set_weights before the layer or model has been built.
  • Passing arrays with the wrong shapes or in the wrong order.
  • Forgetting that model.set_weights expects one flat list for the entire model.
  • Assuming NumPy arrays will be reshaped automatically to fit the layer.
  • Rebuilding a model with a slightly different architecture and trying to reuse incompatible weight arrays.

Summary

  • Use set_weights on a built layer or model.
  • Inspect get_weights() first so you know the required shapes and order.
  • Layer-level weight setting is usually easier to reason about than whole-model weight setting.
  • NumPy arrays must match the expected structure exactly.
  • Most errors come from setting weights before build time or from shape mismatches.

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.