ones_like

paddle. ones_like ( x: paddle.Tensor, dtype: DTypeLike | None = None, name: str | None = None, *, device: PlaceLike | None = None, requires_grad: bool = False, pin_memory: bool = False ) paddle.Tensor [source]

Returns a Tensor filled with the value 1, with the same shape and data type (use dtype if dtype is not None) as x.

Note

Alias Support: The parameter name input can be used as an alias for x. For example, ones_like(input=tensor_x, ...) is equivalent to ones_like(x=tensor_x, ...).

Parameters
  • x (Tensor) – The input tensor which specifies shape and dtype. The dtype of x can be bool, float16, float32, float64, int32, int64. alias: input.

  • dtype (str|np.dtype, optional) – The data type of the output tensor. Supported data types: bool, float16, float32, float64, int32, int64. If dtype is None, the data type is the same as x. Default is None.

  • name (str|None, optional) – For details, please refer to api_guide_Name. Generally, no setting is required. Default: None.

  • device (PlaceLike|None, optional) – The desired device of returned tensor. if None, uses the current device for the default tensor type (see paddle.device.set_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. Default: None.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • pin_memory (bool, optional) – If set, return tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False

Returns

A Tensor filled with the value 1, with the same shape and data type (use dtype if dtype is not None) as x.

Return type

Tensor

Examples

>>> import paddle

>>> x = paddle.to_tensor([1,2,3])
>>> out1 = paddle.ones_like(x)
>>> print(out1.numpy())
[1 1 1]
>>> out2 = paddle.ones_like(x, dtype='int32')
>>> print(out2.numpy())
[1 1 1]