PyTorch torch.prod function
PyTorch torch reference manual
torch.prod is a function in PyTorch used to return the product of all elements in a tensor.
Function definition
torch.prod(input, dim, keepdim=False)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
1
,
2
,
3
,
4
]
)
Return the product of all elements
(
"Product of all elements:"
,
torch.
prod
(
x
)
)
Product along dim=0
y
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
)
(
"dim=0 product:"
,
torch.
prod
(
y
,
dim
=
0
)
)
(
"dim=1 product:"
,
torch.
prod
(
y
,
dim
=
1
)
)
The output is:
所有元素乘积: tensor(24)
dim=0 乘积: tensor([4, 10, 18])
dim=1 乘积: tensor([ 6, 120])