PyTorch torch.cumprod-Funktion


Pytorch torch 参考手册PyTorch torch Referenzhandbuch

torch.cumprodEs ist eine Funktion zur Berechnung des kumulativen Produkts in PyTorch. Sie gibt das kumulative Produkt entlang der angegebenen Dimension zurück, d. h. das Produkt aller Elemente vom Anfang bis zur aktuellen Position.

Funktionsdefinition

torch.cumprod(input, dim, dtype=None)

Anwendungsbeispiel

Beispiel

import torch

# Berechne das kumulative Produkt
x = torch.tensor([1, 2, 3, 4, 5])
result = torch.cumprod(x, dim=0)
print("Eingabe:", x)
print("Kumulatives Produkt:", result)
# Ausgabe: tensor([1, 2, 6, 24, 120])
# Erklärung: 1, 1*2=2, 1*2*3=6, 1*2*3*4=24, 1*2*3*4*5=120

# 2-dimensionaler Tensor
x = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32)
# Kumulativ nach Spalte
result_col = torch.cumprod(x, dim=0)
print("nKumulatives Produkt nach Spalte:")
print(result_col)
# tensor([[ 1, 2, 3],
#         [ 4, 10, 18]])

# Kumulativ nach Zeile
result_row = torch.cumprod(x, dim=1)
print("nKumulatives Produkt nach Zeile:")
print(result_row)
# tensor([[  1,   2,   6],
#         [  4,  20, 120]])

Pytorch torch 参考手册PyTorch torch Referenzhandbuch

Andere Erweiterungen