pytorch

How to Perform Element-wise Multiplication with the “torch.mul()” Function in PyTorch?

Tensors form the backbone of the PyTorch framework for developing machine learning models for all kinds of objectives. All types of data are stored within tensors for easier manipulation and smooth processing during recurring runs of training and testing. The numerous features of PyTorch set it apart from other frameworks because it is geared towards improving the user experience and providing advanced data handling capabilities. The “torch.mul()” function is one such feature meant for manipulating tensors.

This blog will discuss how to use the “torch.mul()” function for element-wise multiplication in PyTorch.

What is the “torch.mul()” Function and What are Its Uses?

The “torch.mul()” function contains essential features to improve the compatibility of different tensors for multiplication and other mathematical operations. These tensors contain numerical data values that are processed during the training loop of machine learning models within the PyTorch framework.

Key uses of the “torch.mul()” function are listed below:

  • Element-wise multiplication” allows every element of a particular tensor to individually interact with the corresponding element of any other tensor.
  • The “broadcasting” of tensors is also carried out by this function. It brings forth tensors of varying shapes and dimensions to be able to be multiplied together by implicit expansion.
  • It helps in the “preprocessing” of data before running the training loop.
  • Users can normalize and scale input data according to project requirements using this function.

How to Compute Element-wise Multiplication with the “torch.mul()” Function in PyTorch?

Element-wise multiplication for two similar tensors is the multiplication of each element of the first tensor with the similar element of the following tensor. The resultant product is a tensor of the same dimensionality. Also, each element of this new tensor is the product of two elements of the previous two tensors. This is different from regular tensor multiplication which depends on matching the number of columns of the first tensor with the number of rows of the second tensor.

Follow the steps given below to learn how to compute the multiplication of two tensors element-wise using the “torch.mul()” function in PyTorch.

Step 1: Launch Google Colab

The first step is to launch the IDE. Colaboratory by Google is a good choice for both programmers and data scientists because of the integrated GPUs. Go to the Colab website and click on the “New Notebook” option to begin:

Step 2: Install and Import the Torch Library

The fundamental step before working on a PyTorch project is to install and import the essential “torch” library:

!pip install torch

import torch

The above code works as follows:

  • Python installation package “!pip” is used to install the torch library into the Colab notebook.
  • Then, the “import” command helps to import the torch library:

Step 3: Define the Sample Tensors for Element-wise Multiplication

In this step, the “torch.tensor()” method will be used to define two tensors that have the same size. These tensors will be used to demonstrate element-wise multiplication in the next step:

sample_tensor_1 = torch.tensor([47, 93, 28])
sample_tensor_2 = torch.tensor([19, 21, 71])

The above code works as follows:

  • The “torch.tensor()” function is used to input the elements in both the sample tensors.
  • Then, each tensor is assigned to its own variable using the “=” sign:

Step 4: Define the Element-wise Multiplication using the “torch.mul()” Function

Next, use the “torch.mul()” function to define the element-wise multiplication between the two tensors defined in the previous step:

element_wise_multiplication = torch.mul(sample_tensor_1, sample_tensor_2)

The above line of code works as follows:

  • The “torch.mul()” function takes the two tensor variables to be multiplied element-wise as its argument.
  • The function is then assigned to the “element_wise_multiplication” variable:

Step 5: Print the Output

The last step is to print the output of the “torch.mul()” function using the “print()” method to check the element-wise multiplication of the two tensors:

print('Element-Wise Multiplication:\n', element_wise_multiplication)

The result of the element-wise multiplication of the two previously defined tensors is shown below:

Note: You can access our Colab Notebook at this link.

Pro-Tip

The “torch.mul()” function can also be used to implement broadcasting. This is a major feature in PyTorch and it is used to change the sizes of any tensor to match the rest to allow multiplication. Tensors are filled with dummy variables that are removed after the required results are achieved.

Success! We have just shown you how to use the “torch.mul()” function to perform element-wise multiplication between two tensors in PyTorch.

Conclusion

To perform element-wise multiplication with the “torch.mul()” function in PyTorch, first define the two tensors of the same size and then use this function. Other mainstream uses of this function can be implemented as per the needs of the user. In this blog, we have successfully showcased the usage of the “torch.mul()” function to compute the multiplication of two tensors in an element-wise manner.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.