Matmul
Source:R/gen-namespace-docs.R
, R/gen-namespace-examples.R
, R/gen-namespace.R
torch_matmul.Rd
Matmul
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 tensor andother
is a tensor,out
will be an 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,.,.) =
#> -3.8287 -1.6401 2.7681 2.3792 0.2675
#> 3.0191 0.1886 2.8494 -2.8350 0.3874
#> -0.9092 -0.5810 0.5582 1.0235 0.2518
#>
#> (2,.,.) =
#> -4.3422 0.5163 -2.5975 1.0504 -0.1999
#> -0.0228 1.2363 -3.3343 -0.3416 -1.0374
#> 9.2242 1.1177 -2.0860 -1.1601 -3.0752
#>
#> (3,.,.) =
#> -0.7372 -0.7448 0.9900 1.3846 -0.5252
#> 2.0256 0.2312 -0.1447 -0.5936 -0.2234
#> 1.9218 -0.4748 1.9894 0.0181 -1.1935
#>
#> (4,.,.) =
#> 2.6770 1.6137 1.6500 -5.1558 -0.3090
#> -0.3202 0.4507 3.0137 -3.3752 0.8325
#> -0.1189 -0.8678 1.1918 1.6684 -1.0595
#>
#> (5,.,.) =
#> 4.5889 -0.1922 0.3740 0.3436 -1.7510
#> -3.1980 -0.8469 4.6970 -0.9635 0.4744
#> 1.4599 -0.9769 3.3400 -0.2807 0.5983
#>
#> (6,.,.) =
#> 0.7696 -0.4145 3.6545 -1.9645 1.1509
#> -4.6367 1.1093 -2.7054 -0.5513 0.5958
#> 1.5427 0.2866 0.1432 -0.9288 -0.0557
#>
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{10,3,5} ]