ones_like

paddle. ones_like ( x, dtype=None, name=None ) [source]

This OP 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) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

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

Raises
  • TypeError – If dtype is not None and is not bool, float16, float32,

  • float64, int32 or int64.

Examples

import paddle

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