Search code examples
machine-learningkeraskeras-layerkeras-2

how to do custom keras layer matrix multiplication


Layers:

  • Input shape (None,75)
  • Hidden layer 1 - shape is (75,3)
  • Hidden layer 2 - shape is (3,1)

For the last layer, the output must be calculated as ( (H21*w1)*(H22*w2)*(H23*w3)), where H21,H22,H23 will be the outcome of Hidden layer 2, and w1,w2,w3 will be constant weight which are not trainable. So how to write a lambda function for the above outcome

def product(X):
    return X[0]*X[1]

keras_model = Sequential()
keras_model.add(Dense(75, 
input_dim=75,activation='tanh',name="layer1" ))
keras_model.add(Dense(3 ,activation='tanh',name="layer2" ))
keras_model.add(Dense(1,name="layer3"))
cross1=keras_model.add(Lambda(lambda x:product,output_shape=(1,1)))([layer2,layer3])
print(cross1)        

NameError: name 'layer2' is not defined


Solution

  • Use the functional API model

    inputs = Input((75,))                                         #shape (batch, 75)
    output1 = Dense(75, activation='tanh',name="layer1" )(inputs) #shape (batch, 75)
    output2 = Dense(3 ,activation='tanh',name="layer2" )(output1) #shape (batch, 3)
    output3 = Dense(1,name="layer3")(output2)                     #shape (batch, 1)
    
    cross1 = Lambda(lambda x: x[0] * x[1])([output2, output3])    #shape (batch, 3)
    
    model = Model(inputs, cross1)
    

    Please notice that the shapes are totally different from what you expect.