1)如果是兩個1維的,就向量內積;
2)如果兩個都是2維的,就矩陣相乘
3)如果第一個是1維,第二個是2維;填充第一個使得能夠和第二個參數相乘;如果第一個是2維,第二個是1維,就是矩陣和向量相乘;
例:
a = torch.zeros(7,)
b= torch.zeros(7,8)
>>> torch.matmul(a,b)
tensor([0., 0., 0., 0., 0., 0., 0., 0.])
如果a是5,8維,報錯。
例子2:
a = torch.zeros(8,)
b= torch.zeros(7,8)
>>> torch.matmul(b,a)
tensor([0., 0., 0., 0., 0., 0., 0.])
如果a是5,7維,報錯。
4) 高維情況
i)其中一個1維,另一個N維(N>2): 類似3),即需要靠近的那個維數相同,比如(7,)和(7,8,11),又比如(7,8,11)和(11,)
ii)都高於2維,那么除掉最后兩個維度之外(兩個數的維數滿足矩陣乘法,即,(m,p)和(p,n)),剩下的緯度滿足pytorch的廣播機制。比如:
# can line up trailing dimensions
>>> x=torch.empty(5,3,4,1) >>> y=torch.empty( 3,1,1) # x and y are broadcastable. # 1st trailing dimension: both have size 1 # 2nd trailing dimension: y has size 1 # 3rd trailing dimension: x size == y size # 4th trailing dimension: y dimension doesn't exist
https://pytorch.org/docs/stable/notes/broadcasting.html#broadcasting-semantics
https://pytorch.org/docs/stable/torch.html?highlight=matmul#torch.matmul
torch.
matmul
(tensor1, tensor2, out=None) → Tensor:
# can line up trailing dimensions
>>> x=torch.empty(5,3,4,1) >>> y=torch.empty( 3,1,1) # x and y are broadcastable. # 1st trailing dimension: both have size 1 # 2nd trailing dimension: y has size 1 # 3rd trailing dimension: x size == y size # 4th trailing dimension: y dimension doesn't exist
###########################################################################需要除掉兩個數的后兩個維度,然后再看是否滿足廣播機制
torch.
matmul
(tensor1, tensor2, out=None) → Tensor
Matrix product of two tensors.
The behavior depends on the dimensionality of the tensors as follows:
-
If both tensors are 1-dimensional, the dot product (scalar) is returned.
-
If both arguments are 2-dimensional, the matrix-matrix product is returned.
-
If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.
-
If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned.
-
If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiple and removed after. The non-matrix (i.e. batch) dimensions are broadcasted (and thus must be broadcastable). For example, if
tensor1
is a (j \times 1 \times n \times m)(j×1×n×m)tensor andtensor2
is a (k \times m \times p)(k×m×p) tensor,out
will be an (j \times k \times n \times p)(j×k×n×p) tensor.