快捷方式

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, optional) – 返回張量所需的資料類型。 如果指定,則在執行操作之前,輸入張量會轉換為 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])

文件

存取 PyTorch 的完整開發人員文件

檢視文件

教學

取得針對初學者和進階開發人員的深入教學

檢視教學

資源

尋找開發資源並取得您的問題解答

檢視資源