PyTorch torch.std_mean function
PyTorch torch reference manual
torch.std_mean is a function in PyTorch used to return both the standard deviation and the mean of a tensor. This is an efficient function that computes the standard deviation and mean in a single operation.
Function definition
torch.std_mean(input, dim, unbiased, keepdim=False)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
,
4.0
,
5.0
]
)
Return both the standard deviation and the mean
std
,
mean
=
torch.
std_mean
(
x
)
(
"Standard deviation:"
,
std
)
(
"Mean:"
,
mean
)
Return both the standard deviation and the mean along dim=0
y
=
torch.
tensor
(
[
[
1.0
,
2.0
,
3.0
]
,
[
4.0
,
5.0
,
6.0
]
]
)
std0
,
mean0
=
torch.
std_mean
(
y
,
dim
=
0
)
(
"dim=0 standard deviation:"
,
std0
)
(
"dim=0 mean:"
,
mean0
)
The output is:
标准差: tensor(1.5811)
均值: tensor(3.)
dim=0 标准差: tensor([1.5000, 1.5000, 1.5000])
dim=0 均值: tensor([2.5000, 3.5000, 4.5000])