MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / arange

Function arange

imperative/python/megengine/functional/tensor.py:70–125  ·  view source on GitHub ↗

r"""Returns evenly spaced values within the half-open interval ``[start, stop)`` as a one-dimensional tensor. Note: This function cannot guarantee that the interval does not include the stop value in those cases where step is not an integer and floating-point rounding errors aff

(
    start: Union[int, float] = 0,
    stop: Optional[Union[int, float]] = None,
    step: Union[int, float] = 1,
    *,
    dtype="float32",
    device=None,
)

Source from the content-addressed store, hash-verified

68
69
70def arange(
71 start: Union[int, float] = 0,
72 stop: Optional[Union[int, float]] = None,
73 step: Union[int, float] = 1,
74 *,
75 dtype="float32",
76 device=None,
77) -> Tensor:
78 r"""Returns evenly spaced values within the half-open interval ``[start, stop)`` as a one-dimensional tensor.
79
80 Note:
81 This function cannot guarantee that the interval does not include the stop value in those cases
82 where step is not an integer and floating-point rounding errors affect the length of the output tensor.
83
84 Args:
85 start(Number): if ``stop`` is specified, the start of interval (inclusive); otherwise,
86 the end of the interval (exclusive). If ``stop`` is not specified, the default starting value is ``0``.
87 stop(Number): the end of the interval.
88 step(Number): the distance between two adjacent elements ( ``out[i+1] - out[i]`` ). Must not be 0 ;
89 may be negative, this results i an empty tensor if stop >= start .
90
91 Keyword args:
92 dtype(:attr:`.Tensor.dtype`, optional): output tensor data type.
93 device(:attr:`.Tensor.device`, optional): device on which to place the created tensor.
94
95 .. seealso:: :func:`~.functional.linspace`
96
97 Returns:
98 A one-dimensional tensor containing evenly spaced values.
99
100 The length of the output tensor must be ``ceil((stop-start)/step)``
101 if ``stop - start`` and ``step`` have the same sign, and length 0 otherwise.
102
103 Examples:
104 >>> F.arange(5)
105 Tensor([0. 1. 2. 3. 4.], device=xpux:0)
106 >>> F.arange(1, 4)
107 Tensor([1. 2. 3.], device=xpux:0)
108
109 """
110 if stop is None:
111 start, stop = 0, start
112
113 if not isinstance(start, Tensor):
114 start = Tensor(start, dtype="float32", device=device)
115 if not isinstance(stop, Tensor):
116 stop = Tensor(stop, dtype="float32", device=device)
117 if not isinstance(step, Tensor):
118 step = Tensor(step, dtype="float32", device=device)
119
120 num = ceil((stop - start) / step)
121 stop = start + step * (num - 1)
122 result = linspace(start, stop, num, device=device)
123 if np.dtype(dtype) != np.float32:
124 return result.astype(dtype)
125 return result
126
127

Callers 1

diag_plane_interpretFunction · 0.70

Calls 5

ceilFunction · 0.70
linspaceFunction · 0.70
TensorClass · 0.50
dtypeMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected