Skip to content
Related Articles

Related Articles

How to perform element-wise multiplication on tensors in PyTorch?

View Discussion
Improve Article
Save Article
  • Last Updated : 02 Mar, 2022
View Discussion
Improve Article
Save Article

In this article, we are going to see how to perform element-wise multiplication on tensors in PyTorch in Python. We can perform element-wise addition using torch.mul() method. 

This function also allows us to perform multiplication on the same or different dimensions of tensors. If tensors are different in dimensions so it will return the higher dimension tensor. we can also multiply a scalar quantity with a tensor using torch.mul() function.  

Syntax: torch.mul(input, other, *, out=None)

Parameters:

  • input: This is input tensor.
  • other: The value or tensor that is to be multiply to every element of tensor.
  • out: it is the output tensor, This is optional parameter.

Return: returns a new modified tensor..

Example 1: 

The following program is to perform multiplication on two single dimension tensors.

Python3




# import torch library
import torch
 
# define two tensors
tens_1 = torch.Tensor([1, 2, 3, 4, 5])
tens_2 = torch.Tensor([10, 20, 30, 40, 50])
 
# display tensors
print(" First Tensor: ", tens_1)
print(" Second Tensor: ", tens_2)
 
# multiply tensors
tens = torch.mul(tens_1, tens_2)
 
# display result after perform element wise multiplication
print(" After Element-wise multiplication: ", tens)

Output:

 First Tensor:  tensor([1., 2., 3., 4., 5.])

 Second Tensor:  tensor([10., 20., 30., 40., 50.])

 After Element-wise multiplication:  tensor([ 10.,  40.,  90., 160., 250.])

Example 2: 

The following program is to know how to multiply a scalar quantity to a tensor.

Python3




# import torch library
import torch
 
# define a tensors
tens_1 = torch.Tensor([100, 200, 300, 400, 500])
 
# display tensor
print(" First Tensor: ", tens_1)
 
# multiply a scalar tensors
tens = torch.mul(tens_1, 2)
 
# display result after perform element wise multiplication
print(" After multiply 2 in tensor: ", tens)

Output:

 First Tensor:  tensor([100., 200., 300., 400., 500.])

 After multiply 2 in tensor:  tensor([ 200.,  400.,  600.,  800., 1000.])

Example 3: 

The following program is to perform elements-wise multiplication on 2D tensors.

Python3




# import torch
import torch
 
# Define two 2D tensors
tens_1 = torch.Tensor([[10, 20], [30, 40]])
tens_2 = torch.Tensor([[1, 2], [3, 4]])
 
# display tensors
print(" First tensor:  ", tens_1)
print(" Second tensor:  ", tens_2)
 
# Multiply above two 2-D tensors
tens = torch.mul(tens_1, tens_2)
print(" After multiply 2D tensors: ", tens)

Output:

First tensor:   tensor([[10., 20.],[30., 40.]])
 Second tensor:   tensor([[1., 2.],[3., 4.]])
 After multiply 2D tensors:  tensor([[ 10.,  40.],[ 90., 160.]])

Example 4:

The following program is to shows how to perform elements-wise multiplication on two different dimension tensors.

Python3




# import torch
import torch
 
# Define two 2D tensors
tens_1 = torch.Tensor([[10, 20], [30, 40]])
tens_2 = torch.Tensor([2, 4])
 
# display tensors
print(" 2D tensor: ", tens_1)
print(" 1D tensor:  ", tens_2)
 
# Multiply above two 2-D tensors
tens = torch.mul(tens_1, tens_2)
print(" After multiply tensors: ", tens)

Output:

 2D tensor:  tensor([[10., 20.],
        [30., 40.]])
 1D tensor:   tensor([2., 4.])
 After multiply tensors:  tensor([[ 20.,  80.],
        [ 60., 160.]])

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!