0

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?

ashraful16
  • 2,742
  • 3
  • 11
  • 32
Bhuvan S
  • 213
  • 1
  • 4
  • 10
  • Is the first dimension the batch dimension? In which case this is just the outer product of the two length 4096 vectors, yes? – Aaron Keesing Sep 20 '20 at 11:02
  • Yes, that is correct. the first dimension is batch which is None. encoders e_1 & e_2 are technically 1D array of 4096 lengths, the output of flattened layers. So multiplication b/w two inputs with a dimension of (None, 4096) will give output of (None, 4096, 4096). 'None' represents batch. – Bhuvan S Sep 21 '20 at 10:17
  • by the way, it's name is Bilinear Tensor product not kronecker product – Bhuvan S Sep 21 '20 at 10:21

1 Answers1

0

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).

Aaron Keesing
  • 1,277
  • 10
  • 18
  • Thank you, it worked. I have one question though. Is this Bilinear tensor product, encoder_1 ⊗ encoder_2? If not then how to implement Keras layer to perform Bilinear tensor product? – Bhuvan S Sep 21 '20 at 14:19
  • This is the outer product between the two vectors broadcasted across the batch dimension. The outer product is the specific case of the tensor product for vectors. – Aaron Keesing Sep 21 '20 at 15:50