Differences with torch.Tensor.sum

View Source On Gitee

torch.Tensor.sum

torch.Tensor.sum(dim=None, keepdim=False, dtype=None)

For more information, see torch.Tensor.sum.

mindspore.Tensor.sum

mindspore.Tensor.sum(axis=None, dtype=None, keepdims=False, initial=None)

For more information, see mindspore.Tensor.sum.

Differences

MindSpore API has the same function as that of PyTorch, but the number and order of parameters are not the same.

PyTorch: No parameter initial. The relative order of parameters keepdim and dtype is different from MindSpore.

MindSpore: The starting value of the summation can be configured with the parameter initial. The relative order of the parameters keepdim and dtype differs from that of PyTorch.

Categories

Subcategories

PyTorch

MindSpore

Differences

Parameters

Parameter 1

dim

axis

Both parameters have different names, and both indicate the specified dimension of the summation

Parameter 2

keepdim

dtype

The relative order of keepdim and dtype are different

Parameter 3

dtype

keepdims

The relative order of keepdims and dtype are different

Parameter 4

-

initial

MindSpore can configure the starting value of the summation with the parameter initial, and PyTorch has no parameter initial.

Code Example

# PyTorch
import torch

b = torch.Tensor([10, -5])
print(torch.Tensor.sum(b))
# tensor(5.)

# MindSpore
import mindspore as ms

a = ms.Tensor([10, -5], ms.float32)
print(a.sum())
# 5.0
print(a.sum(initial=2))
# 7.0