Matmul
Source:R/gen-namespace-docs.R
, R/gen-namespace-examples.R
, R/gen-namespace.R
torch_matmul.Rd
Matmul
Arguments
- self
(Tensor) the first tensor to be multiplied
- other
(Tensor) the second tensor to be multiplied
matmul(input, other, out=NULL) -> 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
input
is a \((j \times 1 \times n \times m)\) tensor andother
is a \((k \times m \times p)\) tensor,out
will be an \((j \times k \times n \times p)\) tensor.
Examples
if (torch_is_installed()) {
# vector x vector
tensor1 = torch_randn(c(3))
tensor2 = torch_randn(c(3))
torch_matmul(tensor1, tensor2)
# matrix x vector
tensor1 = torch_randn(c(3, 4))
tensor2 = torch_randn(c(4))
torch_matmul(tensor1, tensor2)
# batched matrix x broadcasted vector
tensor1 = torch_randn(c(10, 3, 4))
tensor2 = torch_randn(c(4))
torch_matmul(tensor1, tensor2)
# batched matrix x batched matrix
tensor1 = torch_randn(c(10, 3, 4))
tensor2 = torch_randn(c(10, 4, 5))
torch_matmul(tensor1, tensor2)
# batched matrix x broadcasted matrix
tensor1 = torch_randn(c(10, 3, 4))
tensor2 = torch_randn(c(4, 5))
torch_matmul(tensor1, tensor2)
}
#> torch_tensor
#> (1,.,.) =
#> 6.1269 1.3612 0.6866 -3.8543 -1.1654
#> 1.5528 0.8489 -1.4175 1.1915 0.1632
#> -1.2197 -0.3985 -0.0122 0.6675 0.5687
#>
#> (2,.,.) =
#> 4.5127 0.7491 -1.7496 0.9552 3.8570
#> -5.5157 -1.1494 0.6715 1.3456 -1.3611
#> 0.9765 0.3039 1.3610 -2.6397 -2.5517
#>
#> (3,.,.) =
#> -3.0810 -0.7864 0.0398 1.3468 0.3584
#> -4.0864 -1.0721 0.8442 0.5483 -0.6800
#> 1.9305 0.8333 0.1393 -1.3895 -1.9398
#>
#> (4,.,.) =
#> -3.8142 -0.6156 -0.2180 1.9256 -0.5659
#> -6.0375 -1.0019 0.3333 1.8409 -1.9684
#> -1.6487 -0.2011 -1.1990 2.5860 1.2929
#>
#> (5,.,.) =
#> 1.4946 0.4902 -0.4693 0.0062 0.1112
#> -4.9825 -1.5976 -0.2041 2.9227 2.4289
#> -2.6160 -0.0570 -1.3083 2.8375 -0.0867
#>
#> (6,.,.) =
#> 1.4341 0.8723 -0.6583 -0.0735 -1.3161
#> 2.5963 1.1872 -0.6945 -0.5008 -1.4558
#> -2.1505 -0.4424 -0.1460 1.1587 0.0968
#>
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{10,3,5} ]