How to perform element-wise multiplication on tensors in PyTorch?
Last Updated : 02 Mar, 2022
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.]])
Similar Reads
How to perform element-wise addition on tensors in PyTorch?
In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function. This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in di
3 min read
How to perform element-wise division on tensors in PyTorch?
In this article, we will understand how to perform element-wise division of two tensors in PyTorch. To perform the element-wise division of tensors, we can apply the torch.div() method. It takes two tensors (dividend and divisor) as the inputs and returns a new tensor with the element-wise division
3 min read
How to perform element-wise subtraction on tensors in PyTorch?
In this article, we are going to understand how to perform element-wise subtraction on tensors in PyTorch in Python. We can perform element-wise subtraction using torch.sub() method. torch.sub() method allows us to perform subtraction on the same or different dimensions of tensors. It takes two tens
3 min read
Performing Batch Multiplication in PyTorch Without Using torch.bmm
Batch multiplication is a fundamental operation in deep learning and scientific computing, especially when working with large datasets and models. PyTorch, a popular deep learning framework, provides several methods for matrix multiplication, including torch.bmm for batch matrix multiplication. Howe
5 min read
How to compute element-wise entropy of an input tensor in PyTorch
In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input a
2 min read
How to compute element-wise remainder of given input tensor in PyTorch?
In this article, we are going to see how to compute the element-wise remainder in PyTorch. we have two methods to compute element-wise reminders one is torch.remainder() and the other one is torch.fmod() let's go discuss both of them one by one. torch.remainder() method The PyTorch remainder() meth
3 min read
How to compute the element-wise angle of given input tensor in PyTorch?
In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch. torch.angle() method Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angl
3 min read
NumPy ndarray.__mul__() Method | Element Wise Multiplication of Array
The ndarray.__mul__() method does an element-wise multiplication of NumPy ndarray to a particular value that is provided as the parameter. Example C/C++ Code import numpy as np gfg = np.array([1, 2.5, 3, 4.8, 5]) print(gfg.__mul__(5)) Output[ 5. 12.5 15. 24. 25. ] SyntaxSyntax: ndarray.__mul__($self
1 min read
How To Sort The Elements of a Tensor in PyTorch?
In this article, we are going to see how to sort the elements of a PyTorch Tensor in Python. To sort the elements of a PyTorch tensor, we use torch.sort() method. Â We can sort the elements along with columns or rows when the tensor is 2-dimensional. Syntax: torch.sort(input, dim=- 1, descending=Fals
3 min read
How to Perform in-place Operations in PyTorch?
In this article, we will see different in-place operations performed on tensors in PyTorch. Inplace operations are used to directly alter the values of a tensor. The data collected from the user will not be copied. The fundamental benefit of adopting these procedures is that they reduce memory stora
3 min read