Keras
machine learning
model weights
deep learning
tutorial

How to load only specific weights on Keras

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Loading only part of a Keras weight file is a common need when you are reusing a backbone, skipping a classification head, or transplanting a few layers into a new model. The important detail is that Keras loads weights by tensor shape and layer identity, so partial loading works only when you are explicit about which layers should receive which arrays.

The Safest General Strategy

The most portable approach is not to ask Keras to magically "load some weights." Instead, build a source model that matches the saved checkpoint, load that checkpoint fully into the source model, and then copy only the layers you want into the target model.

python
1import tensorflow as tf
2
3
4def build_source_model():
5    inputs = tf.keras.Input(shape=(32,))
6    x = tf.keras.layers.Dense(64, activation="relu", name="shared_dense")(inputs)
7    outputs = tf.keras.layers.Dense(10, activation="softmax", name="old_head")(x)
8    return tf.keras.Model(inputs, outputs)
9
10
11def build_target_model():
12    inputs = tf.keras.Input(shape=(32,))
13    x = tf.keras.layers.Dense(64, activation="relu", name="shared_dense")(inputs)
14    outputs = tf.keras.layers.Dense(3, activation="softmax", name="new_head")(x)
15    return tf.keras.Model(inputs, outputs)
16
17
18source_model = build_source_model()
19target_model = build_target_model()
20
21# Pretend these weights were saved earlier.
22source_model.save_weights("source.weights.h5")
23
24source_model.load_weights("source.weights.h5")
25
26target_model.get_layer("shared_dense").set_weights(
27    source_model.get_layer("shared_dense").get_weights()
28)

This pattern is easy to reason about because you choose exactly which layer gets copied.

Why Manual Layer Transfer Is Useful

Manual transfer avoids version-specific behavior and makes failure cases obvious. If the source and target layer shapes do not match, set_weights raises an error immediately instead of silently doing the wrong thing.

It is also a good fit when:

  • the target model has a different output head
  • only the encoder or backbone should be reused
  • you renamed some layers and need explicit mapping

For transfer learning, that explicitness is usually a good trade.

Loading by Name When Available

Some Keras workflows also support name-based weight loading from compatible weight files. When that option is available in your setup, it can be convenient for skipping unmatched layers. Conceptually, the idea is:

python
model.load_weights("source.weights.h5")

or, in some compatibility-oriented workflows, load by matching layer names and skip mismatched shapes. The exact API details depend on the Keras format and version, which is why manual layer copying is often the more robust explanation.

The underlying rule does not change: matching names are not enough if the stored tensors and target tensors have incompatible shapes.

Freezing the Transferred Layers

Once selected weights are loaded, you often want to freeze those layers for an initial training phase.

python
1target_model.get_layer("shared_dense").trainable = False
2
3target_model.compile(
4    optimizer="adam",
5    loss="sparse_categorical_crossentropy",
6    metrics=["accuracy"],
7)

That prevents the transferred representation from changing immediately while the new head learns the downstream task.

Later, you can unfreeze part of the reused stack and fine-tune it with a smaller learning rate.

Verify What Actually Loaded

After partial loading, verify that the intended layers changed and the others stayed random or newly initialized.

python
for layer in target_model.layers:
    weights = layer.get_weights()
    print(layer.name, [w.shape for w in weights])

For important experiments, it is also worth printing or comparing small weight slices before and after transfer. Partial loading mistakes are easy to make and can quietly invalidate training results.

Common Pitfalls

The biggest pitfall is assuming Keras can infer your intent from a checkpoint file. It cannot know whether you wanted only the backbone, only the embeddings, or everything except the final classifier. You need to define that mapping.

Another mistake is ignoring shape mismatches. A layer with the same name but a different number of units cannot safely receive the old weights.

Developers also forget that optimizer state is separate from layer weights. Loading selected model weights does not restore the training state of an optimizer unless you are using a full model checkpoint designed for that purpose.

Finally, partial transfer only helps if preprocessing and input semantics still match. Reusing image-model weights on incompatible input channels or text-model weights with a different vocabulary can produce misleadingly poor results.

Summary

  • The safest way to load specific Keras weights is to load a matching source model, then copy chosen layers into the target model.
  • Manual get_weights and set_weights transfer makes the mapping explicit.
  • Matching layer names alone are not enough when tensor shapes differ.
  • Freeze reused layers initially when doing transfer learning, then fine-tune later if needed.
  • Always verify which layers actually received transferred weights before trusting the training run.

Course illustration
Course illustration

All Rights Reserved.