PyTorch torch.cov function
PyTorch torch reference manual
torch.cov is a PyTorch function for computing the covariance matrix. It computes the covariance matrix of the input tensor, used to measure the covariance between variables.
Function definition
torch.cov(input, correction=1, fweights=None, aweights=None)
Usage examples
Example
import
torch
Compute the covariance matrix
x
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
2
,
3
,
4
]
,
[
3
,
4
,
5
]
,
[
4
,
5
,
6
]
]
,
dtype
=
torch.
float32
)
(
"Input matrix:"
)
(
x
)
Compute the covariance matrix (using Bessel's correction by default)
cov
=
torch.
cov
(
x
)
(
"Covariance matrix:"
)
(
cov
)
Without correction (correction=0)
cov_no_correction
=
torch.
cov
(
x
,
correction
=
0
)
(
"Uncorrected covariance matrix:"
)
(
cov_no_correction
)
Two variables
a
=
torch.
tensor
(
[
1
,
2
,
3
,
4
,
5
]
,
dtype
=
torch.
float32
)
b
=
torch.
tensor
(
[
2
,
4
,
6
,
8
,
10
]
,
dtype
=
torch.
float32
)
data
=
torch.
stack
(
[
a
,
b
]
)
cov2
=
torch.
cov
(
data
)
(
"Covariance matrix of a and b:"
)
(
cov2
)