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

Function ones

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

r"""Returns a new tensor having a specified shape and filled with ones. Args: shape(int...): the shape of the output tensor. Keyword args: dtype(:attr:`.Tensor.dtype`, optional): output tensor data type. device(:attr:`.Tensor.device`, optional): device on which to p

(
    shape: Union[int, Tuple[int, ...]],
    *,
    dtype="float32",
    device: Optional[CompNode] = None
)

Source from the content-addressed store, hash-verified

320
321
322def ones(
323 shape: Union[int, Tuple[int, ...]],
324 *,
325 dtype="float32",
326 device: Optional[CompNode] = None
327) -> Tensor:
328 r"""Returns a new tensor having a specified shape and filled with ones.
329
330 Args:
331 shape(int...): the shape of the output tensor.
332
333 Keyword args:
334 dtype(:attr:`.Tensor.dtype`, optional): output tensor data type.
335 device(:attr:`.Tensor.device`, optional): device on which to place the created tensor.
336
337 Returns:
338 a tensor containing ones.
339
340 Examples:
341 >>> F.ones(5)
342 Tensor([1. 1. 1. 1. 1.], device=xpux:0)
343 >>> F.ones((5, ), dtype='int32')
344 Tensor([1 1 1 1 1], dtype=int32, device=xpux:0)
345 >>> F.ones((2, 2))
346 Tensor([[1. 1.]
347 [1. 1.]], device=xpux:0)
348 """
349 if isinstance(shape, int):
350 shape = (shape,)
351 if device == None:
352 device = get_default_device()
353 op = builtin.Fill(1, dtype)
354 shape = astensor1d(shape, dtype="int32", device=device)
355 (x,) = apply(op, shape)
356 return x
357
358
359def zeros(

Callers 3

_gen_tile_idxFunction · 0.70
one_hotFunction · 0.70
_simulate_errorFunction · 0.70

Calls 3

astensor1dFunction · 0.85
get_default_deviceFunction · 0.50
applyFunction · 0.50

Tested by

no test coverage detected