ones

paddle. ones ( shape, dtype=None, name=None ) [source]

Create a Tensor of specified shape and dtype and fill it with 1.

Parameters
  • shape (tuple|list|Tensor) – Shape of the Tensor to be created. The data type is int32 or int64 . If shape is a list or tuple, the elements of it should be integers or 0-D Tensor with shape []. If shape is an Tensor, it should be an 1-D Tensor which represents a list.

  • dtype (np.dtype|str, optional) – Data type of output Tensor, it should be one of bool, float16, float32, float64, int32 and int64. If it is set to None, the data type will be float32.

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

Returns

A Tensor of data type dtype with shape shape and all elements are 1.

Return type

Tensor

Examples

import paddle

# shape is a list/tuple
data1 = paddle.ones(shape=[3, 2])
# [[1. 1.]
#  [1. 1.]
#  [1. 1.]]

# shape is a Tensor
shape = paddle.to_tensor([3, 2])
data2 = paddle.ones(shape=shape)
# [[1. 1.]
#  [1. 1.]
#  [1. 1.]]

# shape is a Tensor List
shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
data3 = paddle.ones(shape=shape)
# [[1. 1.]
#  [1. 1.]
#  [1. 1.]]