- paddle.fluid.layers.tensor. fill_constant ( shape, dtype, value, force_cpu=False, out=None, name=None ) [source]
This OP creates a Tensor with specified shape and dtype, and initializes it with a constant specified by value.
The attribute stop_gradient of the created Tensor is set to True.
shape (list|tuple|Tensor) – Shape of the output Tensor, the data type of
shapeis int32 or int64. If
shapeis a list or tuple, the elements of it should be integers or Tensors with shape . If
shapeis an Tensor, it should be an 1-D Tensor with date type int32 or int64.
dtype (np.dtype|str) – Data type of the output Tensor which can be float16, float32, float64, uint8, int16, int32, int64.
value (bool|float|int|Tensor) – The constant value used to initialize the Tensor to be created. If
valueis an Tensor, it should be an 1-D Tensor.
force_cpu (bool, optional) – data should be on CPU if it’s true, default value is False.
out (Tensor, optional) – Optional output which can be any created Tensor that meets the requirements to store the result of operation. if
outis None, a new Tensor will be create to store the result.
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.
Tensor which is created according to shape and dtype.
- Return type
import paddle.fluid as fluid # attr shape is a list which doesn't contain Tensor. data1 = fluid.layers.fill_constant(shape=[2,1], value=0, dtype='int64') # data1=[,] data2 = fluid.layers.fill_constant(shape=[2,1], value=5, dtype='int64', out=data1) # data1=[, ] data2=[, ] # attr shape is a list which contains Tensor. positive_2 = fluid.layers.fill_constant(, "int32", 2) data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[[1.5, 1.5]] # attr shape is a Tensor. shape = fluid.layers.fill_constant(, "int32", 2) # shape=[2,2] data4 = fluid.layers.fill_constant(shape=shape, dtype='bool', value=True) # data4=[[True,True],[True,True]] # attr value is a Tensor. val = fluid.layers.fill_constant(, "float32", 2.0) # val=[2.0] data5 = fluid.layers.fill_constant(shape=[2,1], value=val, dtype='float32') #data5=[[2.0],[2.0]]