layer_norm

paddle.static.nn.layer_norm ( input, scale=True, shift=True, begin_norm_axis=1, epsilon=1e-05, param_attr=None, bias_attr=None, act=None, name=None ) [源代码]

该OP实现了层归一化层(Layer Normalization Layer),其可以应用于小批量输入数据。更多详情请参考:Layer Normalization

计算公式如下

layer_norm - 图1

  • layer_norm - 图2

    : 该层神经元的向量表示

  • layer_norm - 图3

    : 层中隐藏神经元个数

  • layer_norm - 图4

    : 添加较小的值到方差中以防止除零

  • layer_norm - 图5

    : 可训练的比例参数

  • layer_norm - 图6

    : 可训练的偏差参数

参数:

  • input (Tensor) - 维度为任意维度的多维 Tensor ,数据类型为float32或float64。

  • scale (bool, 可选) - 指明是否在归一化后学习自适应增益 g 。默认值:True。

  • shift (bool, 可选) - 指明是否在归一化后学习自适应偏差 b 。默认值:True。

  • begin_norm_axis (int, 可选) - 指明归一化将沿着 begin_norm_axisrank(input) 的维度执行。默认值:1。

  • epsilon (float, 可选) - 指明在计算过程中是否添加较小的值到方差中以防止除零。默认值:1e-05。

  • param_attr (ParamAttr, 可选) - 指定权重参数属性的对象。默认值为None,表示使用默认的权重参数属性。具体用法请参见 ParamAttr

  • bias_attr (ParamAttr, 可选) - 指定偏置参数属性的对象。默认值为None,表示使用默认的偏置参数属性。具体用法请参见 ParamAttr

  • act (str, 可选) - 应用于输出上的激活函数,如tanh、softmax、sigmoid,relu等,支持列表请参考 激活函数 ,默认值为None。

  • name (str, 可选) - 具体用法请参见 Name ,一般无需设置,默认值为None。。

返回:表示归一化结果的 Tensor ,数据类型和 input 一致,返回维度和 input 一致。

代码示例

  1. import paddle
  2. paddle.enable_static()
  3. x = paddle.static.data(name='x', shape=[8, 32, 32], dtype='float32')
  4. output = paddle.static.nn.layer_norm(input=x, begin_norm_axis=1)
  5. print(output.shape) # [8, 32, 32]