Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.Lambda

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.

How it works

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.

Syntax

keras.layers.Lambda(function, output_shape=None, mask=None, arguments=None, **kwargs)

Parameters

NameTypeDescription
functioncallableCallable evaluated by the layer; it receives the input tensor first.
output_shapetuple or callable, optionalOptional output shape, given as a tuple or a function of the input shape.
maskNone, callable, or tensor, optionalMask behavior: None, a mask-computing callable, or an output-mask tensor.
argumentsdict, optionalKeyword arguments forwarded to the callable.
**kwargsoptional keyword argumentsAdditional Keras layer keyword arguments.

Returns

A Keras Layer that evaluates the supplied function.

Key points

  • Lambda turns an arbitrary callable expression into a Keras Layer.
  • It lets custom tensor expressions participate in Sequential and Functional model construction.
  • The wrapped function must accept the input tensor as its first parameter.
  • Keras can infer the output shape when output_shape is not supplied.
  • Use arguments to pass named extra values into the callable.
  • The mask option can disable masking, compute it with a callable, or provide a fixed mask tensor.

Common mistakes

  • Prefer Lambda for simple transformations or prototypes; use a subclassed Layer for advanced behavior.
  • Lambda serialization has limitations, so avoid it when models must be safely portable or independently reloadable.

Example: Square inputs with Lambda

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.

When to use it

Simple tensor expressions

Apply a compact tensor transformation, such as squaring or reshaping values, inside a model graph.

Rapid experimentation

Quickly test a model idea without creating a separate layer class.

Parameterized operations

Pass fixed named values into a transformation using the callable's keyword parameters.

Interview questions

QHow does Lambda determine its output shape?

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.

QWhat does the mask argument control?

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.

QWhen should Lambda be replaced by a custom Layer?

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.Layer
  • keras.Sequential
  • keras.Model
  • keras.Input

Source: Lambda layer. Checked 2026-10-15.