PyTorch torch.cumprod 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

torch.cumprodは、PyTorch で累積積を計算するために使用される関数です。指定した次元に沿った累積積、つまり先頭から現在位置までのすべての要素の積を返します。

関数定義

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

使用例

実例

import torch

# 累積積を計算
x = torch.tensor([1, 2, 3, 4, 5])
result = torch.cumprod(x, dim=0)
print("入力:", x)
print("累積積:", result)
# 出力: tensor([1, 2, 6, 24, 120])
# 説明: 1, 1*2=2, 1*2*3=6, 1*2*3*4=24, 1*2*3*4*5=120

# 2次元テンソル
x = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32)
# 列ごとに累積
result_col = torch.cumprod(x, dim=0)
print("n列ごとの累積積:")
print(result_col)
# tensor([[ 1,  2,  3],
#         [ 4, 10, 18]])

# 行ごとに累積
result_row = torch.cumprod(x, dim=1)
print("n行ごとの累積積:")
print(result_row)
# tensor([[  1,   2,   6],
#         [  4,  20, 120]])

Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

その他の拡張