torch.matmul()

This method allows the computation of multiplication of two vector matrices (single-dimensional matrices), 2D matrices and mixed ones also. This method also supports broadcasting and batch operations. Depending upon the input matrices dimensions, the operation to be done is decided. The general syntax is given below.

torch.matmul(Tensor_1, Tensor_2, out=None)

The table below lists the various possible dimensions of the arguments and the operations based on it.

             argument_1               

         argument_2                    

      Action taken                                                                      

1-dimensional 1-dimensional The scalar product is calculated
2-dimensional 2-dimensional General matrix multiplication is done
1-dimensional 2-dimensional The tensor-1 is pretended with a ‘1’ to match dimension of tensor-2
2-dimensional 1-dimensional Matrix-vector product is calculated
1/N-dimensional (N>2) 1/N-dimensional (N>2) Batched matrix multiplication is done

Example1: Arguments of the same dimension

Python3




import torch as t
  
# both arguments 1D
vec_1 = torch.tensor([3, 6, 2])
vec_2 = torch.tensor([4, 1, 9])
  
print("Single dimensional tensors :", torch.matmul(vec_1, vec_2))
  
# both arguments 2D
mat_1 = torch.tensor([[1, 2, 3],
                      [4, 3, 8],
                      [1, 7, 2]])
  
mat_2 = torch.tensor([[2, 4, 1],
                      [1, 3, 6],
                      [2, 6, 5]])
  
out = torch.matmul(mat_1, mat_2)
  
print("\n3x3 dimensional tensors :\n", out)


Output:

Single dimensional tensors : tensor(36)

3x3 dimensional tensors :
 tensor([[10, 28, 28],
        [27, 73, 62],
        [13, 37, 53]])

Example2: Arguments of different dimensions

Python3




import torch
  
# first argument 1D and second argument 2D
mat1_1 = torch.tensor([3, 6, 2])
  
mat1_2 = torch.tensor([[1, 2, 3],
                       [4, 3, 8],
                       [1, 7, 2]])
  
out_1 = torch.matmul(mat1_1, mat1_2)
print("\n1D-2D multiplication :\n", out_1)
  
# first argument 2D and second argument 1D
mat2_1 = torch.tensor([[2, 4, 1],
                       [1, 3, 6],
                       [2, 6, 5]])
  
mat2_2 = torch.tensor([4, 1, 9])
  
# assigning to output tensor
out_2 = torch.matmul(mat2_1, mat2_2)
  
print("\n2D-1D multiplication :\n", out_2)


Output:

1D-2D multiplication :
 tensor([29, 38, 61])

2D-1D multiplication :
 tensor([21, 61, 59])

Example3:  N-dimensional argument (N>2)

Python3




import torch
  
# creating Tensors using randn()
mat_1 = torch.randn(2, 3, 3)
mat_2 = torch.randn(3)
  
# printing the matrices
print("matrix A :\n", mat_1)
print("\nmatrix B :\n", mat_2)
  
# output
print("\nOutput :\n", torch.matmul(mat_1, mat_2))


Output:

matrix A :
 tensor([[[ 0.5433,  0.0546, -0.5301],
         [ 0.9275, -0.0420, -1.3966],
         [-1.1851, -0.2918, -0.7161]],

        [[-0.8659,  1.8350,  1.6068],
         [-1.1046,  1.0045, -0.1193],
         [ 0.9070,  0.7325, -0.4547]]])

matrix B :
 tensor([ 1.8785, -0.4231,  0.1606])

Output :
 tensor([[ 0.9124,  1.5358, -2.2177],
        [-2.1448, -2.5191,  1.3208]])

Python – Matrix multiplication using Pytorch

The matrix multiplication is an integral part of scientific computing. It becomes complicated when the size of the matrix is huge. One of the ways to easily compute the product of two matrices is to use methods provided by PyTorch.  This article covers how to perform matrix multiplication using PyTorch.

PyTorch and tensors:

It is a package that can be used for neural network-based deep learning projects. It is an open-source library developed by Facebook’s AI research team. It can replace NumPy with its power of GPU. One of the important classes provided by this library is Tensor. It is nothing but the n-dimensional arrays as provided by the NumPy package. There are so many methods in PyTorch that can be applied to Tensor, which makes computations faster and easy. The Tensor can hold only elements of the same data type.

Matrix multiplication with PyTorch:

The methods in PyTorch expect the inputs to be a Tensor and the ones available with PyTorch and Tensor for matrix multiplication are:

  1. torch.mm().
  2. torch.matmul().
  3. torch.bmm()
  4. @ operator.

Similar Reads

torch.mm():

This method computes matrix multiplication by taking an m×n Tensor and an n×p Tensor. It can deal with only two-dimensional matrices and not with single-dimensional ones. This function does not support broadcasting. Broadcasting is nothing but the way the Tensors are treated when their shapes are different. The smaller Tensor is broadcasted to suit the shape of the wider or larger Tensor for operations. The syntax of the function is given below....

torch.matmul():

...

torch.bmm():

...

@ operator:

This method allows the computation of multiplication of two vector matrices (single-dimensional matrices), 2D matrices and mixed ones also. This method also supports broadcasting and batch operations. Depending upon the input matrices dimensions, the operation to be done is decided. The general syntax is given below....