PyTorch torch.cov-Funktion


Pytorch torch 参考手册PyTorch torch-Referenzhandbuch

torch.covist eine Funktion in PyTorch, die zur Berechnung der Kovarianzmatrix verwendet wird. Sie berechnet die Kovarianzmatrix des Eingabe-Tensors, um die Kovarianz zwischen Variablen zu messen.

Funktionsdefinition

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

Anwendungsbeispiel

Beispiel

import torch

# Kovarianzmatrix berechnen
x = torch.tensor([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]], dtype=torch.float32)
print("Eingabematrix:")
print(x)

# Kovarianzmatrix berechnen (Bessel-Korrektur standardmäßig verwendet)
cov = torch.cov(x)
print("Kovarianzmatrix:")
print(cov)

# Ohne Korrektur (correction=0)
cov_no_correction = torch.cov(x, correction=0)
print("Kovarianzmatrix ohne Korrektur:")
print(cov_no_correction)

# Zwei Variablen
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("Kovarianzmatrix von a und b:")
print(cov2)

Pytorch torch 参考手册PyTorch torch-Referenzhandbuch

Andere Erweiterungen