线性代数都学过二维矩阵的乘法,而tf.matmul还可以处理多维矩阵,比如
import tensorflow as tf
import numpy as np
a = tf.random.uniform([2, 1, 2, 3])
b = tf.random.uniform([1, 3, 3, 2])
c = tf.matmul(a, b)
c是什么呢?
先给出结论:不管几维矩阵都是先做最后两维的矩阵的乘法,再在不同维度重复多次。
多维的 tf.matmul(a, b) 的维度有如下两个要求:
1、a的axis=-1的值(~只可意会~)和b的axis=-2的值需要相等。比如上述例子中[3, 2, 3]最后的3,和[3, 3, 2]的第二个3。
2、a和b的各维度的值(除了axis=-1和-2的值),在任意维度上,都需要“相等”或“有一个是1”。
比如,[3, 2, 3]维度的张量与[3, 3, 2]维度的张量做tf.matmul的例子:
In [84]: import tensorflow as tf
...: import numpy as np
...: a = tf.random.uniform([3, 2, 3])
...: b = tf.random.uniform([3, 3, 2])
...: c = tf.matmul(a, b)
...: c.shape
...:
...:
Out[84]: TensorShape([3, 2, 2])
In [87]: tf.matmul(a[0],b[0])
Out[87]:
<tf.Tensor: id=374, shape=(2, 2), dtype=float32, numpy=
array([[1.4506222 , 1.323427 ],
[0.28268352, 0.2917934 ]], dtype=float32)>
In [88]: tf.matmul(a[1],b[1])
Out[88]:
<tf.Tensor: id=383, shape=(2, 2), dtype=float32, numpy=
array([[1.0278544 , 0.4219831 ],
[0.865297 , 0.87740964]], dtype=float32)>
In [89]: c
Out[89]:
<tf.Tensor: id=365, shape=(3, 2, 2), dtype=float32, numpy=
array([[[1.4506222 , 1.323427 ],
[0.28268352, 0.2917934 ]],
[[1.0278544 , 0.4219831 ],
[0.865297 , 0.8774096 ]],
[[0.5752927 , 0.13066964],
[0.5343988 , 0.2741483 ]]], dtype=float32)>
可以看到,[3, 2, 3]维度的张量与[3, 3, 2]维度的张量做tf.matmul,可以理解成:
第一步,先在axis=1和2的维度上做[2, 3]维度的张量与[3, 2]维度的张量之间的二维张量的矩阵乘法,得到[2, 2]维度的结果;
第二部,然后在axis=0的维度上,分别选a的第i个和选b的第i个做上述的第一步,最终得到[3, 2,2]维度的输出。
如果,a和b的axis=0维度对不上,会bug:
In [95]: im