C.W.K.
Stream
Lesson 05 of 07 · published

Shared Layers and Nested Models

~13 min · functional

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Shared layers: one set of weights, many call sites

Shared layers apply the same weights to different inputs. You get this for free from the callable-layer idea (lesson 02): construct a layer once, hold the instance, and call it on more than one tensor — every call reuses the same weights, and gradients from all call sites accumulate into that single weight set. The first Code block builds a Siamese network this way: one shared_embedding encodes both inputs, so the model learns a single embedding space in which 'similar' is a measurable distance. That weight tying is the whole point — if each input had its own encoder, there'd be no shared space to compare in.

Nested models: a model IS a layer

Any Keras Model can be called like a layer inside another model (second Code block). This is more than a convenience: it's the mechanism behind transfer learning. A pretrained backbone is just a Model you call on your input before attaching a fresh head — and because the nested model carries its own weights, loading pretrained weights and (optionally) freezing them with backbone.trainable = False works exactly as you'd expect. The same nesting is how encoder/decoder pairs compose into an autoencoder. Treat 'reuse a whole subgraph' and 'reuse a single layer' as the same move at different scales.

Code

Shared embedding — a Siamese similarity network·python
# Shared embedding for a Siamese network
shared_embedding = layers.Dense(64, activation="relu", name="shared_embed")

input_a = keras.Input(shape=(128,))
input_b = keras.Input(shape=(128,))

# Same weights used for both inputs
encoded_a = shared_embedding(input_a)
encoded_b = shared_embedding(input_b)

# Compute distance
distance = layers.Lambda(
    lambda x: keras.ops.abs(x[0] - x[1])
)([encoded_a, encoded_b])
output = layers.Dense(1, activation="sigmoid")(distance)

model = keras.Model(inputs=[input_a, input_b], outputs=output)
Nesting models — encoder + decoder into an autoencoder·python
# Use an existing model as a layer
encoder = keras.Model(encoder_inputs, encoded, name="encoder")
decoder = keras.Model(decoder_inputs, decoded, name="decoder")

# Nest them
inputs = keras.Input(shape=(784,))
z = encoder(inputs)       # Model called like a layer
outputs = decoder(z)
autoencoder = keras.Model(inputs, outputs)

External links

Exercise

Build a siamese network for image similarity: shared CNN encoder applied to two inputs, then L2 distance + sigmoid. Use the Functional API. Train on synthetic pairs.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.