ones_like

paddle. ones_like ( x, dtype=None, name=None ) [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.

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

  • 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, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

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]