Keras Use the same layer in different models share weights
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, Keras can share weights across different models, but the rule is very specific: you must reuse the same layer instance. If you create two separate layers with the same configuration, they do not share weights even if they look identical.
What weight sharing actually means
A Keras layer object owns its variables. Reusing that exact object in multiple graphs or models means all those models refer to the same variables.
That is how architectures such as Siamese networks, multi-task networks, and shared encoders are built.
Reusing the same layer instance
Here is a simple example with one shared dense layer used in two models:
That prints True because both models reference the same layer object.
If you train model_a, the shared layer weights change. When you later run model_b, it sees those updated weights too.
A more realistic shared-encoder example
Weight sharing is most useful when two inputs should be processed by the same feature extractor.
Here the whole encoder model is reused, which means all layers inside it are shared as one unit.
Separate model objects, shared variables
A useful mental model is:
- model topology can differ
- the shared layer object stays the same
- gradients update the same underlying variables
That means separate models can expose different inputs and outputs while still learning through shared parameters.
This is especially helpful when you want one training model and one inference model built around the same learned representation.
What does not share weights
This does not share weights:
Even though both layers have the same type and shape, they are separate objects with separate variables.
If you want shared weights, reuse layer1; do not create layer2.
Training implications
When shared layers appear in one combined training graph, Keras correctly accumulates gradient contributions from every path that uses those variables.
If you instead train separate models at different times, updates from one model affect the shared layer state seen by the others. That is valid, but you should be intentional about it because one task can influence another.
Common Pitfalls
The biggest mistake is assuming that matching layer configuration implies shared weights. Keras shares by object identity, not by architecture similarity.
Another issue is building multiple models before the shared layer has been called with compatible input shapes. With dynamic construction, shape mismatches can be confusing if the same layer is reused incorrectly.
It is also easy to forget that training one model changes the shared variables for all models using that layer. That is the point of sharing, but it can surprise you during debugging.
Finally, if you save and reload models separately, think carefully about whether you want to preserve a shared architecture or just the resulting weights in each exported model artifact.
Summary
- Keras shares weights when you reuse the same layer or model instance.
- Two separately constructed layers do not share weights, even if they are configured identically.
- Shared layers are useful for Siamese networks, shared encoders, and multi-task architectures.
- Training one model updates the shared variables seen by other models using the same layer.
- The key rule is simple: reuse the same object if you want shared parameters.

