Reputation: 5444
I am struggling to understand the way that the Lambda layers are working in keras. For example, I have the following example:
add_layer = Lambda(lambda x:x[0]+x[1], output_shape=lambda x:x[0])
Given my input x what exactly I am expecting from the output layer of the Labmda
?
Upvotes: 2
Views: 2064
Reputation: 11333
Lambda
layer works similar to Python lambda function. But instead of a function, it returns a Layer
object that performs whatever passed in that lambda function.
You generally use the lambda layer as follows. Which gives out as the result of performing whatever passed in as the Lambda function.
out = add_layer(<some_input>)
For example in the function above you need to pass in a list of two items (denoted by x[0]
and x[1]
. And the lambda layer would perform an element-wise add operation on those two tensors.
You can use this lambda layer in a model as follows. Note that the two inputs inp1
and inp2
need to be broadcastable (does not necessarily need to have the same shape - e.g. (10, 5) and (10, 1) would still work).
from tensorflow.keras import layers, models
inp1 = layers.Input(shape=(5))
inp2 = layers.Input(shape=(1))
add_layer = layers.Lambda(lambda x:x[0]+x[1], output_shape=lambda x:x[0])
out = add_layer([inp1, inp2])
model = models.Model(inputs=[inp1, inp2], outputs=out)
model.summary()
Then you would use this layer as follows.
x1 = np.ones(shape=(10, 5))*1.0
x2 = np.ones(shape=(10, 1))*2.0
print(model.predict([x1, x2]))
Which will return
[[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]]
PS: I would leave the output_shape
argument out because Keras will automatically infer the shape in this case (it's a simple element wise add). Because otherwise what would happen is that, if you pass
inp1 = layers.Input(shape=(1))
inp2 = layers.Input(shape=(5))
The output would have the wrong shape. Because it is always getting the shape of the first tensor as the shape. However in this case the shape would be the shape of the second tensor.
Upvotes: 3
Reputation: 2316
Here your lambda layer is doing the addition of 2 inputs. You would typically use it as:
add_layer = Lambda(lambda x:x[0]+x[1])
input_1 = Input(input_1_shape)
input_2 = Input(input_2_shape)
output = add_layer([input_1, input_2])
This will compute the addition between the 2 tensors.
Of course this is a toy example, and if you really want the addition between 2 tensors, you can use the Add
layer.
Another thing, you don't need to specify the output shape in most cases, except if you work with complex or integer tensors.
Upvotes: 1