Differences with torch.range

View Source On Gitee

torch.range

torch.range(start=0,
            end,
            step=1,
            *,
            out=None,
            dtype=None,
            layout=torch.strided,
            device=None,
            requires_grad=False
            )

For more information, see torch.range.

mindspore.ops.range

mindspore.ops.range(start,
                    end,
                    step
                    )

For more information, see mindspore.ops.range.

Differences

API function of MindSpore is consistent with that of PyTorch.

MindSpore: The dtype of output Tensor is the same as input Tensor.

PyTorch: the dtype of output Tensor is determined by the parameter dtype.

Categories

Subcategories

PyTorch

MindSpore

Difference

input

input 1

start

start

The data type of start parameter in MindSpore is Tensor and the start has no default value, while the data type of start parameter in PyTorch is float and the default value is 0

input 2

end

end

The data type of end parameter in MindSpore is Tensor, while the data type of end parameter in PyTorch is float

input 3

step

step

The data type of step parameter in MindSpore is Tensor and the step has no default value, while the data type of step parameter in PyTorch is float and the default value is 0

input 4

out

-

For details, see General Difference Parameter Table

input 5

dtype

-

The dtype of output Tensor in MindSpore is the same as input Tensor,while the dtype of output Tensor in PyTorch is determined by the parameter dtype

input 6

layout

-

For details, see General Difference Parameter Table

input 7

device

-

For details, see General Difference Parameter Table

input 8

requires_grad

-

For details, see General Difference Parameter Table

Code Example

# PyTorch
import torch

output = torch.range(0, 10, 4, dtype=torch.float32)
print(output)
# tensor([0., 4., 8.])

# MindSpore
import mindspore as ms
from mindspore import Tensor, ops

start = Tensor(0, ms.float32)
limit = Tensor(10, ms.float32)
delta = Tensor(4, ms.float32)
output = ops.range(start, limit, delta)
print(output)
# [0. 4. 8.]