Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Module

model.set_weights

Model.set_weights assigns a layer or model's weights from a list of NumPy arrays. The arrays must match the target's weight count, shapes, and creation order.

How it works

Call the model or layer first so its weights exist, then pass a list of NumPy arrays to set_weights. Keras assigns those arrays according to the order in which the layer created its weights; the list length and every array shape must match the target layer's current weight specification.

Syntax

Model.set_weights(weights)

Parameters

NameTypeDescription
weightslist of NumPy arraysA list of NumPy arrays whose count and shapes match the target layer's weights.

Returns

None

Key points

  • The method replaces a layer or model's current weight values using NumPy arrays.
  • Weights are part of the layer's state, so changing them changes the state used for computation.
  • Its input is an array collection rather than a serialized weights file or state-tree mapping.
  • Preserve Keras's weight-creation order when constructing the input list.
  • A compatible list can be obtained from another compatible object with get_weights, enabling direct transfer.

Common mistakes

  • The supplied list must have the required number of arrays, and each array must have the corresponding shape; otherwise Keras raises ValueError.
  • Build or call the layer before setting weights; an unbuilt layer does not yet have the weight variables required for validation and assignment.

Example: Copy weights between models

import numpy as np
import keras

source = keras.Sequential([keras.layers.Dense(2)])
target = keras.Sequential([keras.layers.Dense(2)])
source(np.zeros((1, 3)))
target(np.zeros((1, 3)))
target.set_weights(source.get_weights())

Build both layers before transferring the source layer's NumPy weight arrays to the target.

When to use it

Transfer weights in memory

Initialize one compatible model or layer with another's learned parameters.

Assign prepared parameters

Restore or apply explicitly prepared NumPy weight values to a built object.

Synchronize model state

Validate that two compatible objects share the same weight structure before copying parameters.

Interview questions

QWhat does Model.set_weights do?

set_weights accepts a list of NumPy arrays and assigns them to the layer's weights. The arrays must be ordered the same way the layer created its weights.

Follow-up: What happens if the list is incompatible? Keras raises ValueError when its number of arrays or shapes do not match the layer specification.

QWhat precondition must hold before calling set_weights?

The target layer must already have instantiated weights, typically by calling or building it. The input list must match the target's weight structure and shapes.

Follow-up: Why can two apparently similar models fail to exchange weights? Their weight creation order, count, or shapes can differ, so the list no longer matches the target specification.

QHow can weights be copied between two Keras objects?

A common transfer pattern is target.set_weights(source.get_weights()). get_weights produces NumPy arrays, and set_weights consumes them in the corresponding weight order.

Follow-up: How would you diagnose a ValueError? Compare the source arrays' count and shapes with the target layer's get_weights output and verify both objects are built.

  • Model.get_weights()
  • Layer.get_weights()
  • Layer.set_weights()
  • Model.save_weights()
  • Model.load_weights()

Source: Keras documentation: Weights-only saving & loading. Checked 2026-10-15.