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

Function linspace

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

r"""Returns evenly spaced numbers over a specified interval. Returns ``num`` evenly spaced samples, calculated over the interval ``[start, stop]``. Args: start(Number): the start of the interval. stop(Number): the end of the interval. num(int): number of values to g

(
    start: Union[int, float],
    stop: Union[int, float],
    num: int,
    *,
    dtype="float32",
    device: Optional[CompNode] = None,
)

Source from the content-addressed store, hash-verified

126
127
128def linspace(
129 start: Union[int, float],
130 stop: Union[int, float],
131 num: int,
132 *,
133 dtype="float32",
134 device: Optional[CompNode] = None,
135) -> Tensor:
136 r"""Returns evenly spaced numbers over a specified interval.
137
138 Returns ``num`` evenly spaced samples, calculated over the interval ``[start, stop]``.
139
140 Args:
141 start(Number): the start of the interval.
142 stop(Number): the end of the interval.
143 num(int): number of values to generate.
144
145 Keyword args:
146 dtype(:attr:`.Tensor.dtype`, optional): output tensor data type.
147 If ``dtype`` is not given, the data type is inferred from ``start`` and ``stop``.
148 device(:attr:`.Tensor.device`, optional): device on which to place the created tensor.
149
150 Returns:
151 a one-dimensional tensor containing evenly spaced values.
152
153 .. seealso:: :func:`~.functional.arange`
154
155 Examples:
156 >>> F.linspace(1, 10, 10)
157 Tensor([ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.], device=xpux:0)
158
159 >>> F.linspace(2., 3., 5)
160 Tensor([2. 2.25 2.5 2.75 3. ], device=xpux:0)
161 """
162 for item in (start, stop, num):
163 cur_device = getattr(item, "device", None)
164 if device is None:
165 device = cur_device
166 else:
167 if not (cur_device is None or device == cur_device):
168 raise ("ambiguous device for linspace opr")
169
170 if not isinstance(start, Tensor):
171 start = Tensor(start, device=device)
172 if not isinstance(stop, Tensor):
173 stop = Tensor(stop, device=device)
174 if not isinstance(num, Tensor):
175 num = Tensor(num, device=device)
176
177 op = builtin.Linspace(comp_node=device)
178 (result,) = apply(op, start, stop, num)
179 if np.dtype(dtype) != np.float32:
180 return result.astype(dtype)
181 return result
182
183
184def eye(N: int, M: int = None, *, dtype="float32", device=None) -> Tensor:

Callers 3

ctc_lossFunction · 0.70
arangeFunction · 0.70
_get_idxFunction · 0.70

Calls 5

LinspaceMethod · 0.80
TensorClass · 0.50
applyFunction · 0.50
dtypeMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected