View source on GitHub |
Wraps arbitrary expressions as a Layer
object.
Inherits From: Layer
, Operation
tf.keras.layers.Lambda(
function, output_shape=None, mask=None, arguments=None, **kwargs
)
Used in the notebooks
Used in the guide | Used in the tutorials |
---|---|
The Lambda
layer exists so that arbitrary expressions can be used
as a 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 Layer
.
The main reason to subclass Layer
instead of using a
Lambda
layer is saving and inspecting a model. Lambda
layers
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.
Example:
# add a x -> x^2 layer
model.add(Lambda(lambda x: x ** 2))
Methods
from_config
@classmethod
from_config( config, custom_objects=None, safe_mode=None )
Creates a layer from its config.
This method is the reverse of get_config
,
capable of instantiating the same layer from the config
dictionary. It does not handle layer connectivity
(handled by Network), nor weights (handled by set_weights
).
Args | |
---|---|
config
|
A Python dictionary, typically the output of get_config. |
Returns | |
---|---|
A layer instance. |
symbolic_call
symbolic_call(
*args, **kwargs
)