PyTorch torch.mv Funktion
PyTorch torch Referenzhandbuch
torch.mvist eine Funktion in PyTorch, die zur Matrix-Vektor-Multiplikation verwendet wird. Sie berechnet das Produkt einer Matrix und eines Vektors.
Funktionsdefinition
torch.mv(input, vec, out=None)
Parameters:
input(Tensor): Eingabematrix, Form (n, m).vec(Tensor): Eingabevektor, Form (m,) oder (m, 1).out(Tensor, optional): Ausgabe-Tensor.
Rückgabewert:
torch.Tensor: Gibt das Matrix-Vektor-Produkt zurück, Form (n,).
Anwendungsbeispiel
Beispiel
import torch
# Matrix und Vektor erstellen
mat = torch.randn(3, 4)
vec = torch.randn(4)
# Matrix-Vektor-Multiplikation
result = torch.mv(mat, vec)
print("Form der Matrix:", mat.shape)
print("Form des Vektors:", vec.shape)
print("Form des Ergebnisses:", result.shape)
print("Ergebnis:", result)
# Matrix und Vektor erstellen
mat = torch.randn(3, 4)
vec = torch.randn(4)
# Matrix-Vektor-Multiplikation
result = torch.mv(mat, vec)
print("Form der Matrix:", mat.shape)
print("Form des Vektors:", vec.shape)
print("Form des Ergebnisses:", result.shape)
print("Ergebnis:", result)
Das Ausgabeergebnis ist:
矩阵形状: torch.Size([3, 4]) 向量形状: torch.Size([4]) 结果形状: torch.Size([3]) tensor([-0.1861, 0.2482, -0.4375])
Beispiel - Berechnung einer neuronalen Netzwerkschicht
import torch
# Simuliere eine vollständig verbundene Schicht eines neuronalen Netzwerks
W = torch.randn(512, 256) # Gewichtsmatrix
x = torch.randn(256) # Eingabevektor
# Berechne y = W @ x
y = torch.mv(W, x)
print("Form der Gewichtsmatrix:", W.shape)
print("Form des Eingabevektors:", x.shape)
print("Form des Ausgabevektors:", y.shape)
# Simuliere eine vollständig verbundene Schicht eines neuronalen Netzwerks
W = torch.randn(512, 256) # Gewichtsmatrix
x = torch.randn(256) # Eingabevektor
# Berechne y = W @ x
y = torch.mv(W, x)
print("Form der Gewichtsmatrix:", W.shape)
print("Form des Eingabevektors:", x.shape)
print("Form des Ausgabevektors:", y.shape)
Andere Erweiterungen