PyTorch torch.var function
PyTorch torch reference manual
torch.var is a function in PyTorch used to return the variance of a tensor. The variance is the average of the squared differences between each value and the mean, measuring the dispersion of the data.
Function definition
torch.var(input, dim, unbiased, keepdim=False)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
,
4.0
,
5.0
]
)
Return the variance of all elements
(
"Variance:"
,
torch.
var
(
x
)
)
Variance along dim=0
y
=
torch.
tensor
(
[
[
1.0
,
2.0
,
3.0
]
,
[
4.0
,
5.0
,
6.0
]
]
)
(
"dim=0 variance:"
,
torch.
var
(
y
,
dim
=
0
)
)
(
"dim=1 variance:"
,
torch.
var
(
y
,
dim
=
1
)
)
The output is:
方差: tensor(2.5000)
dim=0 方差: tensor([2.2500, 2.2500, 2.2500])
dim=1 方差: tensor([1., 1.])