Pytorch torch.cumprod()的简单理解与用法

官方文档:https://pytorch.org/docs/stable/generated/torch.cumprod.html?highlight=cumprod#torch.cumprod

cumprod为cumulative product的意思,即累积乘法
给定一个长度为 N N N的向量 { x i } i = 1 N \{x_i\}^{N}_{i=1} { xi}i=1N,累乘后得到一个新向量 { y i } i = 1 N \{y_i\}^{N}_{i=1} { yi}i=1N,有: y i = x 1 × x 2 × . . . × x i y_i = x_1 × x_2 × ... × x_i yi=x1×x2×...×xi 例子如下:

import torch
x = torch.Tensor([1, 2, 3, 4, 5])
y = torch.cumprod(x, dim = 0)
print(y)

输出如下:

tensor([ 1., 2., 6., 24., 120.])

即分别为1,1×2,1×2×3,1×2×3×4,1×2×3×4×5。

形式化定义如下:

res = torch.cumprod(input, dim, *, dtype=None, out=None)
  • input:输入向量
  • dim:在哪一维度上进行累乘
  • dtype:输出结果的元素类别,默认应该是按float判断
  • out:输出结果
  • res:输出结果

猜你喜欢

转载自blog.csdn.net/qq_40714949/article/details/126667931
今日推荐