评价此页

torch.cumsum#

torch.cumsum(input, dim, *, dtype=None, out=None) Tensor#

返回 input 在维度 dim 上元素的累积和。

例如,如果 input 是一个大小为 N 的向量,则结果也将是一个大小为 N 的向量,其元素为:

yi=x1+x2+x3++xiy_i = x_1 + x_2 + x_3 + \dots + x_i
参数
  • input (Tensor) – 输入张量。

  • dim (int) – 要进行操作的维度

关键字参数
  • dtype (torch.dtype, 可选) – 返回张量所需的数据类型。如果指定,输入张量在执行操作前会被转换为 dtype。这对于防止数据类型溢出很有用。默认值:None。

  • out (Tensor, optional) – 输出张量。

示例

>>> a = torch.randint(1, 20, (10,))
>>> a
tensor([13,  7,  3, 10, 13,  3, 15, 10,  9, 10])
>>> torch.cumsum(a, dim=0)
tensor([13, 20, 23, 33, 46, 49, 64, 74, 83, 93])