Fonction torch.cov de PyTorch


Pytorch torch 参考手册Manuel de référence PyTorch torch

torch.covest une fonction de PyTorch utilisée pour calculer la matrice de covariance. Elle calcule la matrice de covariance du tenseur d'entrée pour mesurer la covariance entre les variables.

Définition de fonction

torch.cov(input, correction=1, fweights=None, aweights=None)

Exemple d'utilisation

Exemple

import torch

# Calculer la matrice de covariance
x = torch.tensor([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]], dtype=torch.float32)
print("Matrice d'entrée :")
print(x)

# Calculer la matrice de covariance (correction de Bessel utilisée par défaut)
cov = torch.cov(x)
print("Matrice de covariance :")
print(cov)

# Sans correction (correction=0)
cov_no_correction = torch.cov(x, correction=0)
print("Matrice de covariance sans correction :")
print(cov_no_correction)

# Deux 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("Matrice de covariance de a et b :")
print(cov2)

Pytorch torch 参考手册Manuel de référence PyTorch torch

Autres extensions