Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.cov function

PyTorch torch.cov function


PyTorch torch reference manual 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

)

print

(

"Input matrix:"

)

print

(

x

)

Compute the covariance matrix (using Bessel's correction by default)

cov

=

torch.

cov

(

x

)

print

(

"Covariance matrix:"

)

print

(

cov

)

Without correction (correction=0)

cov_no_correction

=

torch.

cov

(

x

,

correction

=

0

)

print

(

"Uncorrected covariance matrix:"

)

print

(

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

)

print

(

"Covariance matrix of a and b:"

)

print

(

cov2

)


PyTorch torch reference manual PyTorch torch reference manual