• fluid.initializer
    • Bilinear
    • BilinearInitializer
    • Constant
    • ConstantInitializer
    • force_init_on_cpu
    • init_on_cpu
    • MSRA
    • MSRAInitializer
    • Normal
    • NormalInitializer
    • NumpyArrayInitializer
    • TruncatedNormal
    • TruncatedNormalInitializer
    • Uniform
    • UniformInitializer
    • Xavier
    • XavierInitializer

    fluid.initializer

    SourceEnglish

    Bilinear

    SourceEnglish

    • paddle.fluid.initializer.Bilinear
    • BilinearInitializer 的别名

    BilinearInitializer

    SourceEnglish

    • class paddle.fluid.initializer.BilinearInitializer
    • 该初始化函数用于转置卷积函数,进行上采样。用户通过任意整型因子放大shape为(B,C,H,W)的特征图。用法如下:

    代码示例:

    1. import paddle.fluid as fluid
    2. factor = 2
    3. C = 2
    4. w_attr = fluid.initializer.ParamAttr(
    5. learning_rate=0.,
    6. regularizer=fluid.regularizer.L2Decay(0.),
    7. initializer=fluid.initializer.Bilinear())
    8. x = fluid.layers.data(name="data", shape=[3, 32, 32],
    9. dtype="float32")
    10. conv_up = fluid.layers.conv2d_transpose(
    11. input=x,
    12. num_filters=C,
    13. output_size=None,
    14. filter_size=2 * factor - factor % 2,
    15. padding=int(math.ceil((factor - 1) / 2.)),
    16. stride=factor,
    17. groups=C,
    18. param_attr=w_attr,
    19. bias_attr=False)

    num_filters = C和groups = C 表示这是按通道转置的卷积函数。滤波器shape为(C,1,K,K),K为filter_size。该初始化函数为滤波器的每个通道设置(K,K)插值核。输出特征图的最终输出shape为(B,C,factorH,factorW)。注意学习率和权重衰减设为0,以便在训练过程中双线性插值的系数值保持不变

    Constant

    SourceEnglish

    • paddle.fluid.initializer.Constant
    • ConstantInitializer 的别名

    ConstantInitializer

    SourceEnglish

    • class paddle.fluid.initializer.ConstantInitializer(value=0.0, force_cpu=False)
    • 常量初始器

    • 参数:

      • value (float) - 用常量初始化变量代码示例
    1. import paddle.fluid as fluid
    2. x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
    3. fc = fluid.layers.fc(input=x, size=10,
    4. param_attr=fluid.initializer.Constant(value=2.0))

    force_init_on_cpu

    SourceEnglish

    • paddle.fluid.initializer.force_init_on_cpu()
    • 标志位,是否强制在CPU上进行变量初始化。

    返回:状态,是否应强制在CPU上强制进行变量初始化

    返回类型:bool

    代码示例

    1. import paddle.fluid as fluid
    2. if fluid.initializer.force_init_on_cpu():
    3. step = fluid.layers.create_global_var(shape=[2,3], value=1.0, dtype='float32')

    init_on_cpu

    SourceEnglish

    • paddle.fluid.initializer.init_on_cpu()
    • 强制变量在 cpu 上初始化。

    代码示例

    1. import paddle.fluid as fluid
    2. with fluid.initializer.init_on_cpu():
    3. step = fluid.layers.create_global_var(shape=[2,3], value=1.0, dtype='float32')

    MSRA

    SourceEnglish

    • paddle.fluid.initializer.MSRA
    • MSRAInitializer 的别名

    MSRAInitializer

    SourceEnglish

    • class paddle.fluid.initializer.MSRAInitializer(uniform=True, fan_in=None, seed=0)
    • 实现MSRA初始化(a.k.a. Kaiming初始化)

    该类实现权重初始化方法,方法来自Kaiming He,Xiangyu Zhang,Shaoqing Ren 和 Jian Sun所写的论文: Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification 。这是一个鲁棒性特别强的初始化方法,并且适应了非线性激活函数(rectifier nonlinearities)。

    在均匀分布中,范围为[-x,x],其中:

    fluid.initializer - 图1

    在正态分布中,均值为0,标准差为:

    fluid.initializer - 图2

    • 参数:
      • uniform (bool) - 是否用均匀分布或正态分布
      • fan_in (float) - MSRAInitializer的fan_in。如果为None,fan_in沿伸自变量
      • seed (int) - 随机种子

    注解

    在大多数情况下推荐设置fan_in为None

    代码示例

    1. import paddle.fluid as fluid
    2. x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
    3. fc = fluid.layers.fc(input=x, size=10, param_attr=fluid.initializer.MSRA(uniform=False))

    Normal

    SourceEnglish

    • paddle.fluid.initializer.Normal
    • NormalInitializer 的别名

    NormalInitializer

    SourceEnglish

    • class paddle.fluid.initializer.NormalInitializer(loc=0.0, scale=1.0, seed=0)
    • 随机正态(高斯)分布初始化器

    • 参数:

      • loc (float) - 正态分布的平均值
      • scale (float) - 正态分布的标准差
      • seed (int) - 随机种子代码示例
    1. import paddle.fluid as fluid
    2. x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
    3. fc = fluid.layers.fc(input=x, size=10,
    4. param_attr=fluid.initializer.Normal(loc=0.0, scale=2.0)

    NumpyArrayInitializer

    SourceEnglish

    • class paddle.fluid.initializer.NumpyArrayInitializer(value)
    • 使用Numpy型数组来初始化参数变量。

    • 参数:

      • value (numpy) - 用于初始化变量的一个Numpy型数组。代码示例
    1. import paddle.fluid as fluid
    2. x = fluid.layers.data(name="x", shape=[5], dtype='float32')
    3. fc = fluid.layers.fc(input=x, size=10,
    4. param_attr=fluid.initializer.NumpyArrayInitializer(numpy.array([1,2])))

    TruncatedNormal

    SourceEnglish

    • paddle.fluid.initializer.TruncatedNormal
    • TruncatedNormalInitializer 的别名

    TruncatedNormalInitializer

    SourceEnglish

    • class paddle.fluid.initializer.TruncatedNormalInitializer(loc=0.0, scale=1.0, seed=0)
    • Random Truncated Normal(高斯)分布初始化器

    • 参数:

      • loc (float) - 正态分布的平均值
      • scale (float) - 正态分布的标准差
      • seed (int) - 随机种子代码示例
    1. import paddle.fluid as fluid
    2. x = fluid.layers.data(name='x', shape=[1], dtype='float32')
    3. fc = fluid.layers.fc(input=x, size=10,
    4. param_attr=fluid.initializer.TruncatedNormal(loc=0.0, scale=2.0))

    Uniform

    SourceEnglish

    • paddle.fluid.initializer.Uniform
    • UniformInitializer 的别名

    UniformInitializer

    SourceEnglish

    • class paddle.fluid.initializer.UniformInitializer(low=-1.0, high=1.0, seed=0)
    • 随机均匀分布初始化器

    • 参数:

      • low (float) - 下界
      • high (float) - 上界
      • seed (int) - 随机种子代码示例
    1. import paddle.fluid as fluid
    2. x = fluid.layers.data(name='x', shape=[1], dtype='float32')
    3. fc = fluid.layers.fc(input=x, size=10,
    4. param_attr=fluid.initializer.Uniform(low=-0.5, high=0.5))

    Xavier

    SourceEnglish

    • paddle.fluid.initializer.Xavier
    • XavierInitializer 的别名

    XavierInitializer

    SourceEnglish

    • class paddle.fluid.initializer.XavierInitializer(uniform=True, fan_in=None, fan_out=None, seed=0)
    • 该类实现Xavier权重初始化方法( Xavier weight initializer),Xavier权重初始化方法出自Xavier Glorot和Yoshua Bengio的论文 Understanding the difficulty of training deep feedforward neural networks

    该初始化函数用于保持所有层的梯度尺度几乎一致。

    在均匀分布的情况下,取值范围为[-x,x],其中:

    fluid.initializer - 图3

    正态分布的情况下,均值为0,标准差为:

    fluid.initializer - 图4

    • 参数:
      • uniform (bool) - 是否用均匀分布或者正态分布
      • fan_in (float) - 用于Xavier初始化的fan_in。如果为None,fan_in沿伸自变量
      • fan_out (float) - 用于Xavier初始化的fan_out。如果为None,fan_out沿伸自变量
      • seed (int) - 随机种子

    注解

    在大多数情况下推荐将fan_in和fan_out设置为None

    代码示例

    1. import paddle.fluid as fluid
    2. queries = fluid.layers.data(name='x', shape=[1], dtype='float32')
    3. fc = fluid.layers.fc(
    4. input=queries, size=10,
    5. param_attr=fluid.initializer.Xavier(uniform=False))