我们学习的矩阵乘法通常只能是二维matrix相乘,那么如何操作更高维度矩阵的相乘呢?
栗子
import tensorflow as tf
a = tf.constant(0, shape=[1, 2, 3, 4])
b = tf.constant(0, shape=[1, 2, 4, 7])
c = tf.matmul(a, b)
print(a,b)
print(c.shape)
结果:
c.shape = (1, 2, 3, 7)
结论
即,如果a和b的dimention大于2,那么有如下规则:
1. a和b除了最后两个维度可不一致,其他维度必须一致
2. a和b最后两维的维度要符合矩阵乘法的要求(比如a的(3,4)能和b的(4,6)进行矩阵乘法)