paddle.fluid.layers.ones(shape, dtype, force_cpu=False)[source]

The OP creates a tensor of specified shape and dtype, and fills it with 1. Its stop_gradient will be set to True to stop gradient computation.

  • shape (tuple|list) – Shape of output tensor.

  • dtype (np.dtype|core.VarDesc.VarType|str) – Data type of output tensor, it supports bool, float16, float32, float64, int32 and int64.

  • force_cpu (bool, optional) – Whether force to store the output tensor in CPU memory. If force_cpu is False, the output tensor will be stored in running device memory. Default: False.


A tensor of data type dtype with shape shape and all elements set to 1.

Return type



import paddle.fluid as fluid
data = fluid.layers.ones(shape=[2, 4], dtype='float32') # [[1., 1., 1., 1.], [1., 1., 1., 1.]]