I’m using TensorFlow Keras backend and I have two tensors a
, b
of the same shape: (None, 4, 7)
, where None
represents the batch dimension.
I want to do matrix multiplication, and I’m expecting a result of (None, 4, 4)
.
i.e. For each batch, do one matmul: (4,7)·(7,4) = (4,4)
Here’s my code —
JavaScript
x
2
1
K.dot(a, K.reshape(b, (-1, 7, 4)))
2
This code gives a tensor of shape (None, 4, None, 4)
I’d like to know how does high-dimension matrix multiplication work? What’s the right way to do this?
Advertisement
Answer
IIUC, you can either use tf.matmul
directly as part of your model and transpose b
or explicitly wrap the operation in a Lambda
layer:
JavaScript
1
8
1
import tensorflow as tf
2
3
a = tf.keras.layers.Input((4, 7))
4
b = tf.keras.layers.Input((4, 7))
5
output = tf.matmul(a, b, transpose_b=True)
6
model = tf.keras.Model([a, b], output)
7
model.summary()
8
JavaScript
1
17
17
1
Model: "model_1"
2
__________________________________________________________________________________________________
3
Layer (type) Output Shape Param # Connected to
4
==================================================================================================
5
input_15 (InputLayer) [(None, 4, 7)] 0 []
6
7
input_16 (InputLayer) [(None, 4, 7)] 0 []
8
9
tf.linalg.matmul_2 (TFOpLambda (None, 4, 4) 0 ['input_15[0][0]',
10
) 'input_16[0][0]']
11
12
==================================================================================================
13
Total params: 0
14
Trainable params: 0
15
Non-trainable params: 0
16
__________________________________________________________________________________________________
17
Or
JavaScript
1
8
1
import tensorflow as tf
2
3
a = tf.keras.layers.Input((4, 7))
4
b = tf.keras.layers.Input((4, 7))
5
output = tf.keras.layers.Lambda(lambda x: tf.matmul(x[0], x[1], transpose_b=True))([a, b])
6
model = tf.keras.Model([a, b], output)
7
model.summary()
8