keras.layers.Lambda(function, output_shape=None, mask=None, arguments=None, **kwargs)
Wraps arbitrary expressions as a
Lambda layer exists so that arbitrary expressions can be used
Layer when constructing Sequential
and Functional API models.
Lambda layers are best suited for simple
operations or quick experimentation. For more advanced use cases,
prefer writing new subclasses of
Lambda layers have (de)serialization limitations!
The main reason to subclass
Layer instead of using a
Lambda layer is saving and inspecting a model.
are saved by serializing the Python bytecode, which is fundamentally
non-portable and potentially unsafe.
They should only be loaded in the same environment where
they were saved. Subclassed layers can be saved in a more portable way
by overriding their
get_config() method. Models that rely on
subclassed Layers are also often easier to visualize and reason about.
# add a x -> x^2 layer model.add(Lambda(lambda x: x ** 2))
output_shape = (input_shape, ) + output_shapeor, the input is
Noneand the sample dimension is also
output_shape = (None, ) + output_shape. If a function, it specifies the entire shape as a function of the input shape:
output_shape = f(input_shape).
compute_masklayer method, or a tensor that will be returned as output mask regardless of what the input is.