new_ones

paddle.Tensor. new_ones ( var: Tensor, size: ShapeLike, *, dtype: DTypeLike | None = None, device: PlaceLike | None = None, requires_grad: bool = False, pin_memory: bool = False ) Tensor

Create a new Tensor of the specified shape filled with ones.

Parameters
  • var (Tensor) – A reference Tensor for default dtype and device.

  • size (ShapeLike) – Shape of the new Tensor.

  • dtype (DTypeLike, optional) – Desired data type of the new Tensor. Defaults to var.dtype.

  • device (PlaceLike, optional) – Device on which to place the new Tensor. Defaults to var.place.

  • requires_grad (bool, optional) – Whether to track gradients. Default: False.

  • pin_memory (bool, optional) – Whether to pin memory. Default: False.

Returns

A new Tensor filled with ones.

Return type

Tensor

Examples

>>> import paddle
>>> x = paddle.zeros([2, 2])
>>> y = x.new_ones(3, 3)  # type: ignore
>>> y.numpy()
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)