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.
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.
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.
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.
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.
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.
That error is useful. It tells you the layer and the arrays do not agree structurally.
Common Pitfalls
- Calling
set_weightsbefore the layer or model has been built. - Passing arrays with the wrong shapes or in the wrong order.
- Forgetting that
model.set_weightsexpects 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_weightson 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
- How to Setup Adaptive Learning Rate in Keras
- How to setup learning environment for Udacity Deep Learning class with TensorFlow Windows
- How to show all my images in tensorboard?
- How to slice Tensorflow network into two maintaining gradient back-propagation?
- How to show training and predicted values on Tensorboard using python
- How to shuffle two numpy datasets using TensorFlow 2.0?
- How to show loss values during training in scikit-learn?
- How to simplify Tensorboard graph with shared variables?
.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.