torch.matmul

J·2021년 6월 15일
0

pytorch

목록 보기
13/23

공식설명은 아래와 같다.

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에 따라 정해진다.

    1. 두 tensor가 모두 1 dimensional일 때, dot product 수행
    1. 두 tensor가 모두 2 dimensional일 때, matrix product를 수행
    1. 첫번째 tensor가 1 dimensional이고 두번째 tensor가 2 dimensional일 때, matrix multiply를 하기 위해 첫번째 dimension의 앞 dimension에 1이 더해진다. matrix multiply를 한 후에 더해진 차원은 사라진다.
  • example
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
    1. 첫번째 tensor가 2dimensional이고, 두번째 tensor가 1 dimensional이면 matrix-vector product를 수행한다.
    1. 두 tensor 모두 1 dimensional이상이고 하나의 tensor가 3이상의 N dimensional이면, batched matrix multiply가 수행된다. 첫번째 tensor가 1 dimensional이면 1차원이 앞에 추가되어 batched matrix multipy를 수행하고, 수행후에 추가된 dimension은 사라진다. non-matrix dimension (batch)는 broadcast된다. 예를 들면 첫번째 tensor가 (j x 1 x n x n) tensor이고, 두번째 tensor가 (k x n x n) tensor일 때, output은 (j x k x n x n) tensor가 된다. broad casting logic은 input이 broadcast 가능할 때에 batch dimension에만 해당된다. 예를 들면 첫번째 tensor가 (j x 1 x n x m) tensor이고, 두번째 tensor가 (k x m x p) tensor일 때 matrix dimension이 달라도 broadcasting이 유효하다. output은 (j x k x n x p) tensor가 된다.

Reference

  1. https://pytorch.org/docs/stable/generated/torch.matmul.html
profile
I'm interested in processing video&images with deeplearning and solving problem in our lives.

0개의 댓글