fill_constant

paddle.fluid.layers.fill_constant ( shape, dtype, value, force_cpu=False, out=None ) [源代码]

该OP创建一个形状为shape并且数据类型为dtype的Tensor,同时用 value 中提供的常量初始化该Tensor。

创建的Tensor的stop_gradient属性默认为True。

参数:

  • shape (tuple|list|Variable)- 要创建的LoDTensor或者SelectedRows的形状。 数据类型为int32或int64。 如果shape是一个列表或元组,则其元素应该是形状为[1]的整数或Tensor。 如果shape是Variable,则它应该是一维Tensor。

  • dtype (np.dtype|core.VarDesc.VarType|str)- 创建LoDTensor或者SelectedRows的数据类型,支持数据类型为float16, float32, float64, int32, int64。

  • value (float|int)- 用于初始化输出LoDTensor或者SelectedRows的常量数据的值。

  • force_cpu (bool)- 用于标志LoDTensor或者SelectedRows是否创建在CPU上,默认值为False,若设为true,则数据必须在CPU上。

  • out (Variable,可选)- 用于存储创建的LoDTensor或者SelectedRows,可以是程序中已经创建的任何Variable。默认值为None,此时将创建新的Variable来保存输出结果。

返回: 根据shape和dtype创建的Tensor。

返回类型:变量(Variable)

代码示例

  1. import paddle.fluid as fluid
  2. data1 = fluid.layers.fill_constant(shape=[2,1], value=0, dtype='int64') #data1=[[0],[0]]
  3. data2 = fluid.layers.fill_constant(shape=[2,1], value=5, dtype='int64', out=data1)
  4. #data1=[[5],[5]] data2=[[5],[5]]
  5. # attr shape is a list which contains Variable Tensor.
  6. positive_2 = fluid.layers.fill_constant([1], "int32", 2)
  7. data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[1.5, 1.5]
  8. # attr shape is a Variable Tensor.
  9. shape = fluid.layers.fill_constant([1,2], "int32", 2) # shape=[2,2]
  10. data4 = fluid.layers.fill_constant(shape=shape, dtype='bool', value=True) # data4=[[True,True],[True,True]]