MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / Lambda

Class Lambda

tensorflow/python/keras/layers/core.py:658–919  ·  view source on GitHub ↗

Wraps arbitrary expressions as a `Layer` object. The `Lambda` layer exists so that arbitrary TensorFlow functions can be used when constructing `Sequential` and Functional API models. `Lambda` layers are best suited for simple operations or quick experimentation. For more advanced use cases

Source from the content-addressed store, hash-verified

656
657@keras_export('keras.layers.Lambda')
658class Lambda(Layer):
659 """Wraps arbitrary expressions as a `Layer` object.
660
661 The `Lambda` layer exists so that arbitrary TensorFlow functions
662 can be used when constructing `Sequential` and Functional API
663 models. `Lambda` layers are best suited for simple operations or
664 quick experimentation. For more advanced use cases, subclassing
665 `keras.layers.Layer` is preferred. One reason for this is that
666 when saving a Model, `Lambda` layers are saved by serializing the
667 Python bytecode, whereas subclassed Layers are saved via overriding
668 their `get_config` method and are thus more portable. Models that rely
669 on subclassed Layers are also often easier to visualize and reason
670 about.
671
672 Examples:
673
674 ```python
675 # add a x -> x^2 layer
676 model.add(Lambda(lambda x: x ** 2))
677 ```
678 ```python
679 # add a layer that returns the concatenation
680 # of the positive part of the input and
681 # the opposite of the negative part
682
683 def antirectifier(x):
684 x -= K.mean(x, axis=1, keepdims=True)
685 x = K.l2_normalize(x, axis=1)
686 pos = K.relu(x)
687 neg = K.relu(-x)
688 return K.concatenate([pos, neg], axis=1)
689
690 model.add(Lambda(antirectifier))
691 ```
692
693 Variables can be created within a `Lambda` layer. Like with
694 other layers, these variables will be created only once and reused
695 if the `Lambda` layer is called on new inputs. If creating more
696 than one variable in a given `Lambda` instance, be sure to use
697 a different name for each variable. Note that calling sublayers
698 from within a `Lambda` is not supported.
699
700 Example of variable creation:
701
702 ```python
703 def linear_transform(x):
704 v1 = tf.Variable(1., name='multiplier')
705 v2 = tf.Variable(0., name='bias')
706 return x*v1 + v2
707
708 linear_layer = Lambda(linear_transform)
709 model.add(linear_layer)
710 model.add(keras.layers.Dense(10, activation='relu'))
711 model.add(linear_layer) # Reuses existing Variables
712 ```
713
714 Note that creating two instances of `Lambda` using the same function
715 will *not* share Variables between the two instances. Each instance of

Callers 1

multi_gpu_modelFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected