Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 40 ยท Advanced Topics

Custom Layers and Models

Write your own Keras layers and models by subclassing: build(), call(), add_weight, get_config, custom losses and metrics, plus a NumPy layer class that mirrors the pattern.

Keras ships dozens of layers, but research and special problems often need something new: a custom activation, a gated residual, a physics-inspired transformation. Keras makes this easy through subclassing. Once you understand the three-part pattern (create weights, compute forward pass, describe yourself), writing a layer is a few lines.

TensorFlow and Keras do not run in the browser here, so layer code that uses keras is read-only. The runnable blocks implement the same lifecycle in plain Python and NumPy.

The layer lifecycle

A keras.layers.Layer subclass has three jobs:

  1. __init__: store the configuration (number of units, activation). No weights yet, because the input shape is unknown.
  2. build(input_shape): called once, on the first call, when the shape is known. Create weights here with self.add_weight.
  3. call(inputs): the forward computation.
Python
import keras
from keras import layers, ops

class MyDense(layers.Layer):
    def __init__(self, units, activation=None, **kwargs):
        super().__init__(**kwargs)
        self.units = units
        self.activation = keras.activations.get(activation)

    def build(self, input_shape):
        self.w = self.add_weight(
            shape=(input_shape[-1], self.units),
            initializer="glorot_uniform",
            trainable=True,
            name="kernel",
        )
        self.b = self.add_weight(
            shape=(self.units,), initializer="zeros", trainable=True, name="bias"
        )

    def call(self, inputs):
        return self.activation(ops.matmul(inputs, self.w) + self.b)

    def get_config(self):
        config = super().get_config()
        config.update({"units": self.units,
                       "activation": keras.activations.serialize(self.activation)})
        return config

Use keras.ops (not raw tf.*) for math inside layers: the same code then runs on TensorFlow, JAX or PyTorch backends in Keras 3. get_config is what makes the layer saveable.

The same lifecycle in plain Python

This stand-in has the same three phases: configure, lazily build on the first call, then compute.

Python A layer with lazy build

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Lazy building is why you can write layers.Dense(10) without stating the input size.

A layer with a non-trainable state and a training flag

Some layers behave differently in training and inference (dropout, batch normalization). call receives a training argument. A custom layer can also own non-trainable state, such as a running average.

Python training flag and running state

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In Keras the training argument is passed automatically by fit, evaluate and predict. Pass it through to sublayers: self.drop(x, training=training).

Custom models by subclassing

Subclass keras.Model to group layers, define call, and use ordinary Python control flow.

Python
class ResidualMLP(keras.Model):
    def __init__(self, hidden=64, classes=10, **kwargs):
        super().__init__(**kwargs)
        self.inp = layers.Dense(hidden, activation="relu")
        self.mid = layers.Dense(hidden, activation="relu")
        self.drop = layers.Dropout(0.2)
        self.out = layers.Dense(classes, activation="softmax")

    def call(self, x, training=False):
        h = self.inp(x)
        h = h + self.mid(h)           # residual connection
        h = self.drop(h, training=training)
        return self.out(h)

model = ResidualMLP()
model.compile("adam", "sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=3)

Subclassed models are flexible, but model.summary() cannot show shapes until the model is built (call it on data or use model.build(input_shape)), and they are harder to save and plot than Sequential or functional models. Prefer the functional API when the graph is a fixed DAG, and subclass when you need dynamic logic.

Custom losses and metrics

A loss is just a function (y_true, y_pred) -> scalar-per-example. Write it with keras.ops, then pass it to compile.

Python
def huber_like(y_true, y_pred, delta=1.0):
    err = y_true - y_pred
    small = ops.abs(err) <= delta
    return ops.mean(ops.where(small, 0.5 * ops.square(err),
                              delta * (ops.abs(err) - 0.5 * delta)), axis=-1)

model.compile(optimizer="adam", loss=huber_like)
Python Check a custom loss by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Test a loss on a few numbers like this before training. A stateful metric subclasses keras.metrics.Metric with update_state, result and reset_state.

Handy extras

  • self.add_loss(...) inside a layer adds a regularization term to the total loss.
  • layers.Lambda wraps a function quickly, but it is hard to save safely; prefer a real layer.
  • self.add_weight(..., trainable=False) creates state (counters, running stats).
  • Give layers a name to find them later: model.get_layer("kernel_block").

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should a custom layer create its weights when the input size is not known in init?

    build() runs once with the real input shape; call() would recreate weights every time.
  2. 2. What is the training argument of call() for?

    Layers such as Dropout and BatchNormalization act differently during training and inference.
  3. 3. Why implement get_config()?

    Keras uses the returned dict to recreate the layer when loading.
  4. 4. Why use keras.ops in custom layers?

    keras.ops is the backend-agnostic math API of Keras 3.

Summary

  • A custom layer: store config in __init__, create weights in build, compute in call, describe in get_config.
  • call receives training; pass it to sublayers.
  • Subclass keras.Model for dynamic logic; use the functional API for fixed graphs.
  • Custom losses are plain functions built from keras.ops; check them on small numbers first.
  • Use keras.ops for backend-independent code.

Next: Custom Training Loops