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

Function full

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

r"""Returns a new tensor having a specified shape and filled with given value. Args: shape(int...): output tensor shape. value(Scalar): fill value. Keyword args: dtype(:attr:`.Tensor.dtype`, optional): output tensor data type. If ``dtype`` is ``None``,

(
    shape: Union[int, Tuple[int, ...]],
    value: Union[bool, int, float],
    *,
    dtype=None,
    device=None,
)

Source from the content-addressed store, hash-verified

280
281
282def full(
283 shape: Union[int, Tuple[int, ...]],
284 value: Union[bool, int, float],
285 *,
286 dtype=None,
287 device=None,
288) -> Tensor:
289 r"""Returns a new tensor having a specified shape and filled with given value.
290
291 Args:
292 shape(int...): output tensor shape.
293 value(Scalar): fill value.
294
295 Keyword args:
296 dtype(:attr:`.Tensor.dtype`, optional): output tensor data type.
297 If ``dtype`` is ``None``, the output tensor data type must be inferred from ``value``.
298 If the value is an ``int``, the output tensor data type must be the default integer data type.
299 If the value is a ``float``, the output tensor data type must be the default floating-point data type.
300 If the value is a ``bool``, the output tensor must have boolean data type.
301 device(:attr:`.Tensor.device`, optional): device on which to place the created tensor.
302
303 Returns:
304 a tensor where every element is equal to ``value``.
305
306 Examples:
307 >>> F.full((2, 3), 6)
308 Tensor([[6 6 6]
309 [6 6 6]], dtype=int32, device=xpux:0)
310 """
311
312 if isinstance(shape, int):
313 shape = (shape,)
314 if device is None:
315 device = get_default_device()
316 x = Const(value, dtype, device)
317 if type(shape) in (list, tuple) and len(shape) == 0:
318 return x
319 return broadcast_to(x, shape)
320
321
322def ones(

Callers 1

fill_Function · 0.85

Calls 3

broadcast_toFunction · 0.70
get_default_deviceFunction · 0.50
ConstClass · 0.50

Tested by

no test coverage detected