Constant

class paddle.nn.initializer.Constant ( value=0.0 ) [源代码]

该接口为常量初始化函数,用于权重初始化,通过输入的value值初始化输入变量;

参数:

  • value (float16|float32) - 用于初始化输入变量的值;

返回:参数初始化类的实例

代码示例

  1. import paddle
  2. import paddle.nn as nn
  3. data = paddle.rand([30, 10, 2], dtype='float32')
  4. linear = nn.Linear(2, 4, weight_attr=nn.initializer.Constant(value=2.0))
  5. res = linear(data)
  6. print(linear.weight.numpy())
  7. #result is [[2. 2. 2. 2.],[2. 2. 2. 2.]]