fill_constant(shape, dtype, value, force_cpu=False, out=None)
This OP creates a Tensor with specified shape and dtype, and initializes it with a constant specifed by value.
The attribute stop_gradient of the created Tensor is setted to True.
shape (list|tuple|Variable) – Shape of the Tensor to be created. The data type is
shapeis a list or tuple, the elements of it should be integers or Tensors with shape . If
shapeis an Variable, it should be an 1-D Tensor .
dtype (np.dtype|core.VarDesc.VarType|str) – Data type of the output tensor which can be float16, float32, float64, int32, int64.
value (float) – The constant value used to initialize the Tensor to be created.
force_cpu (True) – data should be on CPU if it’s true, defalut value is False.
out (Variable, optional) – Optional output which can be any created Variable that meets the requirements to store the result of operation. if out is None, a new Varibale will be create to store the result.
Tensor which is created according to shape and dtype.
- Return type
TypeError: The dtype must be one of bool, float16, float32, float64, int32 and int64 and the data type of out Tensor must be the same as the dtype.
import paddle.fluid as fluid # attr shape is a list which doesn't contain Variable 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 Variable 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 an Variable Tensor. shape = fluid.layers.fill_constant([1,2], "int32", 2) # shape=[2,2] data4 = fluid.layers.fill_constant(shape=shape, dtype='bool', value=True) # data4=[[True,True],[True,True]]