Keras
Deep Learning
Neural Networks
Layer Reuse
Machine Learning

Reusing a group of Keras layers

Master System Design with Codemia

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

Introduction

Reusing one Keras layer is easy, but reusing a whole group of layers requires you to decide whether you want shared weights or just repeated architecture. In Keras, those are different things: reusing the same layer or submodel instance shares weights, while creating a new copy of the same code produces a structurally similar block with independent weights.

The Three Common Reuse Patterns

When people say "reuse a group of layers," they usually mean one of these:

  • wrap the block in a function that creates new layers each time
  • wrap the block in a reusable keras.Model
  • wrap the block in a custom keras.layers.Layer

The correct choice depends on whether you want weight sharing and how much control you need.

Repeating Architecture Without Sharing Weights

If you only want the same architecture in several places, use a function that creates fresh layers on each call.

python
1import tensorflow as tf
2from tensorflow import keras
3
4
5def dense_block(x, units):
6    x = keras.layers.Dense(units, activation="relu")(x)
7    x = keras.layers.Dense(units, activation="relu")(x)
8    return x
9
10inputs = keras.Input(shape=(16,))
11x1 = dense_block(inputs, 32)
12x2 = dense_block(x1, 32)
13outputs = keras.layers.Dense(1)(x2)
14model = keras.Model(inputs, outputs)

This reuses the pattern, not the weights. Each call creates new layer instances.

Reusing the Same Block with Shared Weights

If you want the exact same block instance to be applied more than once, wrap it as a submodel or custom layer and call the same object repeatedly.

python
1import tensorflow as tf
2from tensorflow import keras
3
4block_input = keras.Input(shape=(16,))
5x = keras.layers.Dense(32, activation="relu")(block_input)
6x = keras.layers.Dense(32, activation="relu")(x)
7shared_block = keras.Model(block_input, x, name="shared_block")
8
9inputs = keras.Input(shape=(16,))
10a = shared_block(inputs)
11b = shared_block(a)
12outputs = keras.layers.Dense(1)(b)
13model = keras.Model(inputs, outputs)

Because shared_block is the same object both times, its weights are shared.

This pattern is useful in Siamese networks, tied encoders, and other architectures that deliberately reuse the same learned transformation.

Custom Layer for a Reusable Logical Unit

If the block is conceptually one reusable component, a custom layer often reads best.

python
1import tensorflow as tf
2from tensorflow import keras
3
4class DenseBlock(keras.layers.Layer):
5    def __init__(self, units):
6        super().__init__()
7        self.d1 = keras.layers.Dense(units, activation="relu")
8        self.d2 = keras.layers.Dense(units, activation="relu")
9
10    def call(self, inputs):
11        x = self.d1(inputs)
12        return self.d2(x)
13
14block = DenseBlock(32)
15inputs = keras.Input(shape=(16,))
16x = block(inputs)
17x = block(x)
18outputs = keras.layers.Dense(1)(x)
19model = keras.Model(inputs, outputs)

Again, calling the same block object twice shares weights.

Weight Sharing Versus Fresh Copies

This distinction is the central design point.

  • same instance reused: shared weights
  • new instance created from the same code: independent weights

That means a helper function and a reusable object are not interchangeable, even if the code looks similar at first glance.

When debugging reuse, inspect the model summary and the number of trainable variables. If the parameter count doubled when you expected sharing, you probably created new instances instead of reusing one.

When to Use Each Pattern

A helper function is good when you want repeated architecture but not tied weights.

A submodel or custom layer is better when the block is a conceptual module or when weight sharing is intentional.

A custom layer is often the most maintainable choice when the block has internal state, optional behavior, or configuration you want to serialize cleanly.

Common Pitfalls

Assuming that calling the same Python function means shared weights is the most common mistake. It usually creates fresh layers unless you explicitly reuse existing objects.

Reusing the same block instance accidentally can also be a bug if you intended independent weights.

Another common issue is building blocks with shapes that cannot be applied repeatedly. Shared reuse still requires compatible input and output dimensions.

Finally, when saving and loading models, custom layers need proper serialization support if you want portable model configs.

Summary

  • reusing a group of Keras layers can mean either repeated architecture or true weight sharing
  • helper functions usually create new layers and therefore new weights
  • reusing the same Model or custom Layer instance shares weights
  • choose the pattern based on whether weight sharing is intended
  • if the parameter count is not what you expected, check whether you reused one object or recreated several

Course illustration
Course illustration

All Rights Reserved.