PyTorch torch.mul Function
PyTorch torch Reference Manual
torch.mulIt is a function in PyTorch used to perform element-wise multiplication. It multiplies two tensors or a tensor and a scalar.
This is one of the most fundamental mathematical operations in deep learning.
Function Definition
torch.mul(input, other, out=None)
Parameters:
input(Tensor): The first input tensor.other(Tensor or float): The second input tensor or scalar.out(Tensor, optional): The output tensor.
Return Value:
torch.Tensor: Returns the multiplied tensor.
Usage Examples
Example 1: Element-wise Multiplication of Tensors
Example
import torch
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = torch.mul(a, b)
print(c)
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = torch.mul(a, b)
print(c)
The output result is:
tensor([ 4, 10, 18])
Example 2: Multiplying by a Scalar
Example
import torch
a = torch.tensor([1, 2, 3])
# Multiply by 2
b = torch.mul(a, 2)
print(b)
# You can also use the * operator
c = a * 2
print(c)
a = torch.tensor([1, 2, 3])
# Multiply by 2
b = torch.mul(a, 2)
print(b)
# You can also use the * operator
c = a * 2
print(c)
The output result is:
tensor([2, 4, 6]) tensor([2, 4, 6])
Other Extensions