PyTorch torch.mul function
PyTorch torch reference manual
torch.mul 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 tensor multiplication
Example
import
torch
a
=
torch.
tensor
(
[
1
,
2
,
3
]
)
b
=
torch.
tensor
(
[
4
,
5
,
6
]
)
c
=
torch.
mul
(
a
,
b
)
(
c
)
The output is:
tensor([ 4, 10, 18])
Example 2: Multiplication by a scalar
Example
import
torch
a
=
torch.
tensor
(
[
1
,
2
,
3
]
)
Multiply by 2
b
=
torch.
mul
(
a
,
2
)
(
b
)
You can also use the * operator
c
=
a *
2
(
c
)
The output is:
tensor([2, 4, 6])
tensor([2, 4, 6])