mindspore.ops.arange

mindspore.ops.arange(start=0, end=None, step=1, *, dtype=None)[源代码]

返回从 start 开始,步长为 step ,且不超过 end (不包括 end )的序列。

参数:
  • start (Union[float, int, Tensor], 可选) - 序列的起始数。如果为Tensor,则shape必须为()。默认值:0。

  • end (Union[float, int, Tensor], 可选) - 序列的终止数,不包含在序列中。如果为Tensor,则shape必须为()。默认值:None。如果为None,则默认为 start 的值,同时将0作为范围起始值。

  • step (Union[float, int, Tensor], 可选) - 表述序列中数值的步长。如果为Tensor,则shape必须为()。默认值:1。

关键字参数:
  • dtype (mindspore.dtype, 可选) - 返回Tensor的所需数据类型。默认值:None。如果未指定或者为None,则使用 startendstep 参数中精度最高的类型。

返回:

一维Tensor,数据类型由 dtype 决定。

异常:
  • TypeError - startendstep 既不是int也不是float也不是在支持类型中的TensorScalar(shape为()的特殊Tensor)。

  • ValueError - step 等于0。

  • ValueError - start 小于等于 end ,且 step 小于0。

  • ValueError - start 大于等于 end ,且 step 大于0。

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore as ms
>>> from mindspore import Tensor, ops
>>> output = ops.arange(1, 6)
>>> print(output)
[1 2 3 4 5]
>>> print(output.dtype)
Int64
>>> output = ops.arange(0, 3, 1.2)
>>> print(output)
[0.  1.2 2.4]
>>> print(output.dtype)
Float32
>>> output = ops.arange(7, 1, -2)
>>> print(output)
[7 5 3]
>>> print(output.dtype)
Int64
>>> output = ops.arange(ms.Tensor(12.0, dtype=ms.float64), 2, ms.Tensor(-1.0, dtype=ms.float32))
>>> print(output)
[12. 11. 10.  9.  8.  7.  6.  5.  4.  3.]
>>> print(output.dtype)
Float64