Simple tensor expressions
Apply a compact tensor transformation, such as squaring or reshaping values, inside a model graph.
Keras · Class
keras.layers.Lambda wraps an arbitrary callable as a Keras layer for use in Sequential or Functional models. It is intended mainly for simple operations and rapid experimentation.
Lambda wraps a callable as a Keras layer. The callable receives the input tensor first, while optional keyword arguments can supply extra values; Keras can infer the output shape when it is omitted. Use it for small transformations, and define a custom Layer for substantial or reusable behavior.
keras.layers.Lambda(function, output_shape=None, mask=None, arguments=None, **kwargs)| Name | Type | Description |
|---|---|---|
function | callable | Callable evaluated by the layer; it receives the input tensor first. |
output_shape | tuple or callable, optional | Optional output shape, given as a tuple or a function of the input shape. |
mask | None, callable, or tensor, optional | Mask behavior: None, a mask-computing callable, or an output-mask tensor. |
arguments | dict, optional | Keyword arguments forwarded to the callable. |
**kwargs | optional keyword arguments | Additional Keras layer keyword arguments. |
A Keras Layer that evaluates the supplied function.
output_shape is not supplied.arguments to pass named extra values into the callable.mask option can disable masking, compute it with a callable, or provide a fixed mask tensor.import keras
from keras import layers
inputs = keras.Input(shape=(4,))
outputs = layers.Lambda(lambda x: x ** 2)(inputs)
model = keras.Model(inputs, outputs)
print(model(keras.ops.array([[1., 2., 3., 4.]])))Builds a model whose Lambda layer squares every input element.
Apply a compact tensor transformation, such as squaring or reshaping values, inside a model graph.
Quickly test a model idea without creating a separate layer class.
Pass fixed named values into a transformation using the callable's keyword parameters.
The callable is evaluated as a layer operation and receives the input tensor as its first argument. output_shape may be omitted because Keras can infer it, or it can be supplied as a tuple or a function.
Follow-up: How would you pass an additional constant? Supply it through the arguments dictionary, whose entries become keyword arguments to the callable.
mask can be None, a callable compatible with a layer's mask computation, or a tensor that is returned as the output mask. This lets the layer preserve or define masking behavior.
Follow-up: What should be used for complex masking or reusable logic? Implement a custom Layer and define the appropriate layer methods instead of concentrating behavior in a Lambda callable.
Lambda is intended to expose arbitrary expressions as layers in Sequential and Functional models. It is most appropriate for simple operations and quick experimentation, while more advanced cases belong in a subclassed Layer.
Follow-up: Why is a custom Layer preferable for production components? It gives the behavior a structured implementation and avoids Lambda's portability and serialization limitations.
keras.layers.Layerkeras.Sequentialkeras.Modelkeras.Input