Search code examples
pythontensorflowkerasmultimodal

How to define Kronecker product layer of 2 keras layers of shape (None, 4096) is performed?


Let's say there are 2 different/separate keras layers, encoder_1 & encoder_2 with both having output shape of (None, 4096). Now how to define keras multiply layer which gives (None, 4096, 4096) as it's output shape. Is this same as Kronecker product? If not the same please show how to implement Kronecker product of 2 layers named, encoder_1 & encoder_2?


Solution

  • So you should be able to achieve this simply using either the Dot layer or dot method of Keras, after inserting dimensions of length 1:

    import tensorflow as tf
    from tensorflow.keras.layers import dot
    
    encoder_1 = tf.expand_dims(encoder_1, axis=2)
    encoder_2 = tf.expand_dims(encoder_2, axis=1)
    outer = dot([encoder_1, encoder_2], axes=(2, 1))
    

    outer should be a tensor of shape (None, 4096, 4096).