@ operator

The @ – Simon H operator, when applied on matrices performs multiplication element-wise on 1D matrices and normal matrix multiplication on 2D matrices. If both the matrices have the same dimension, then the matrix multiplication is carried out normally without any broadcasting/prepending.  If any one of the matrices is of a different dimension, then appropriate broadcasting is carried out first and then the multiplication is carried out. This operator applies to N-Dimensional matrices also.

Example:

Python3




# single dimensional matrices
oneD_1 = torch.tensor([3, 6, 2])
oneD_2 = torch.tensor([4, 1, 9])
  
  
# two dimensional matrices
twoD_1 = torch.tensor([[1, 2, 3],
                       [4, 3, 8],
                       [1, 7, 2]])
twoD_2 = torch.tensor([[2, 4, 1],
                       [1, 3, 6],
                       [2, 6, 5]])
  
# N-dimensional matrices (N>2)
  
# 2x3x3 dimensional matrix
ND_1 = torch.tensor([[[-0.0135, -0.9197, -0.3395],
                      [-1.0369, -1.32421.4799],
                      [-0.0182, -1.29170.6575]],
  
                     [[-0.3585, -0.04780.4674],
                      [-0.6688, -0.9217, -1.2612],
                      [1.6323, -0.06400.4357]]])
  
# 2x3x4 dimensional matrix
ND_2 = torch.tensor([[[0.2431, -0.1044, -0.1437, -1.4982],
                      [-1.4318, -0.25101.62470.5623],
                      [1.5265, -0.8568, -2.1125, -0.9463]],
  
                     [[0.01820.52071.2890, -1.3232],
                      [-0.2275, -0.8006, -0.6909, -1.0108],
                      [1.3881, -0.0327, -1.4890, -0.5550]]])
  
print("1D matrices output :\n", oneD_1 @ oneD_2)
print("\n2D matrices output :\n", twoD_1 @ twoD_2)
print("\nN-D matrices output :\n", ND_1 @ ND_2)
print("\n Mixed matrices output :\n", oneD_1 @ twoD_1 @ twoD_2)


Output:

1D matrices output :
 tensor(36)

2D matrices output :
 tensor([[10, 28, 28],
        [27, 73, 62],
        [13, 37, 53]])

N-D matrices output :
 tensor([[[ 0.7953,  0.5231, -0.7751, -0.1757],
         [ 3.9030, -0.8274, -5.1287, -0.5915],
         [ 2.8487, -0.2372, -3.4850, -1.3212]],

        [[ 0.6531, -0.1637, -1.1250,  0.2633],
         [-1.5532,  0.4309,  1.6526,  2.5166],
         [ 0.6491,  0.8869,  1.4995, -2.3370]]])

 Mixed matrices output:
 tensor([218, 596, 562])


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....