Reputation: 33
In the case of dot(), it takes the dot product, mathematically defined as: a.b = sum(a_i * b_i), but how to write a lambda function in keras for a*b=product (a_i * b_i) and forwarding this input to next layer
Upvotes: 0
Views: 368
Reputation: 11333
You can do the following. Essentially we are doing the dot product with multiplication for a (None, 10)
sized input and a (10,20)
sized input. This results in a (None, 20)
sized output.
import numpy as np
import tensorflow as tf
import tensorflow.keras.backend as K
inp1 = tf.keras.layers.Input(shape=(10,))
inp2 = tf.keras.layers.Input(batch_shape=(10,20))
prod_out = tf.keras.layers.Lambda(lambda x: K.dot(K.prod(x[0],axis=1, keepdims=True), K.prod(x[1],axis=0, keepdims=True)))([inp1, inp2])
model = tf.keras.models.Model([inp1,inp2], prod_out)
model.summary()
Upvotes: 1