torch.cumsum(input, dim, *, dtype=None, out=None) -> Tensor
For more information, see torch.cumsum.
mindspore.ops.cumsum(x, axis, dtype=None) -> Tensor
For more information, see mindspore.ops.cumsum.
PyTorch: Calculates the cumulative sum of the input Tensor on the specified axis.
MindSpore: MindSpore API implements functions basically same as PyTorch, but there are differences in parameter settings.
Categories | Subcategories | PyTorch | MindSpore | Difference |
---|---|---|---|---|
Parameters | Parameter 1 | input | x | Same function, different parameter names |
Parameter 2 | dim | axis | Same function, different parameter names | |
Parameter 3 | dtype | dtype | - | |
Parameter 4 | out | - | Not involved |
When the input tensor is the same and the accumulation axis is -1, the innermost layer of the tensor is accumulated from left to right, and the two APIs achieve the same function.
# PyTorch
import torch
from torch import tensor
import numpy as np
a = tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))
y = torch.cumsum(a, dim=-1)
print(y.numpy())
# [[ 3. 7. 13. 23.]
# [ 1. 7. 14. 23.]
# [ 4. 7. 15. 22.]
# [ 1. 4. 11. 20.]]
# MindSpore
from mindspore import Tensor
import mindspore.ops as ops
import numpy as np
x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))
y = ops.cumsum(x, -1)
print(y)
# [[ 3. 7. 13. 23.]
# [ 1. 7. 14. 23.]
# [ 4. 7. 15. 22.]
# [ 1. 4. 11. 20.]]
When the input tensor and the accumulation axis are the same, torch.cumsum and MindSpore get the same result by setting the data type of the output y to int8 through the parameter dtype.
# PyTorch
import torch
from torch import tensor
import numpy as np
a = tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))
y = torch.cumsum(a, dim=0, dtype=torch.int8)
print(y.numpy())
# [[ 3 4 6 10]
# [ 4 10 13 19]
# [ 8 13 21 26]
# [ 9 16 28 35]]
print(y.dtype)
# torch.int8
# MindSpore
import mindspore
from mindspore import Tensor
import mindspore.ops as ops
x = Tensor([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]], mindspore.float32)
y = ops.cumsum(x, 0, dtype=mindspore.int8)
print(y)
# [[ 3 4 6 10]
# [ 4 10 13 19]
# [ 8 13 21 26]
# [ 9 16 28 35]]
print(y.dtype)
# Int8
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。