공식설명은 아래와 같다.
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 input is a (j \times 1 \times n \times n)(j×1×n×n) tensor and other is a (k \times n \times n)(k×n×n) tensor, out will be a (j \times k \times n \times n)(j×k×n×n) tensor.
Note that the broadcasting logic only looks at the batch dimensions when determining if the inputs are broadcastable, and not the matrix dimensions. For example, if input is a (j \times 1 \times n \times m)(j×1×n×m) tensor and other is a (k \times m \times p)(k×m×p) tensor, these inputs are valid for broadcasting even though the final two dimensions (i.e. the matrix dimensions) are different. out will be a (j \times k \times n \times p)(j×k×n×p) tensor.
This operator supports TensorFloat32.
두 tensor의 행렬 연산을 수행한다. 이 함수의 behavior는 tensor의 dimension에 따라 정해진다.
import torch
>>>tensor1 = torch.randn(3)
>>>tensor2 = torch.randn(3, 4)
>>>tensor3 = torch.randn(4)
>>>result1 = torch.matmul(tensor1, tensor2)
>>>print(result1.shape)
torch.Size([4])
>>>print(result1)
tensor([-5.6995, -4.0084, 1.0690, 0.8973])
>>>result2 = torch.matmul(tensor3, tensor2)
RuntimeError: size mismatch, m1: [1 x 4], m2: [3 x 4] at C:\w\1\s\windows\pytorch\aten\src\TH/generic/THTensorMath.cpp:136