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 of shape should be int32 or int64.

  • 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

# default dtype for ones OP
data1 = paddle.ones(shape=[3, 2])
# [[1. 1.]
#  [1. 1.]
#  [1. 1.]]

data2 = paddle.ones(shape=[2, 2], dtype='int32')
# [[1 1]
#  [1 1]]

# shape is a Tensor
shape = paddle.full(shape=[2], dtype='int32', fill_value=2)
data3 = paddle.ones(shape=shape, dtype='int32')
# [[1 1]
#  [1 1]]