pytorch 学习:layer normalization

torch.nn.LayerNorm(
    normalized_shape, 
    eps=1e-05, 
    elementwise_affine=True)

参数介绍:

normalized_shape

输入尺寸

(多大的内容进行归一化)【默认是靠右的几个size(后面实例会说明)】

eps为保证数值稳定性(分母不能趋近或取0),给分母加上的值。默认为1e-5。
elementwise_affine

布尔值,当设为true,给该层添加可学习的仿射变换参数(γ,β)。

 实例

import torch
import numpy as np

x_test = np.array([[[1,2,-1,1],[3,4,-2,2]],
                   [[1,2,-1,1],[3,4,-2,2]]])
x_test = torch.from_numpy(x_test).float()
x_test
'''
tensor([[[ 1.,  2., -1.,  1.],
         [ 3.,  4., -2.,  2.]],

        [[ 1.,  2., -1.,  1.],
         [ 3.,  4., -2.,  2.]]])
''

#x_test的维度是[2,2,4]

每一个[i,j][0~3]进行归一化

m = torch.nn.LayerNorm(normalized_shape = [4])
output = m(x_test)
output

'''
tensor([[[ 0.2294,  1.1471, -1.6059,  0.2294],
         [ 0.5488,  0.9879, -1.6465,  0.1098]],

        [[ 0.2294,  1.1471, -1.6059,  0.2294],
         [ 0.5488,  0.9879, -1.6465,  0.1098]]],
       grad_fn=<NativeLayerNormBackward>)
'''

'''
print(0.2294+1.1471+-1.6059+0.2294)
print(0.5488+0.9879+-1.6465+0.1098)

-5.551115123125783e-17
-1.249000902703301e-16
'''

 每一个[i][0][0]~[i][1][3]进行归一化

m = torch.nn.LayerNorm(normalized_shape = [2,4])
output = m(x_test)
output
'''
tensor([[[-0.1348,  0.4045, -1.2136, -0.1348],
         [ 0.9439,  1.4832, -1.7529,  0.4045]],

        [[-0.1348,  0.4045, -1.2136, -0.1348],
         [ 0.9439,  1.4832, -1.7529,  0.4045]]],
       grad_fn=<NativeLayerNormBackward>)
'''

'''
print(-0.1348+0.4045+-1.2136+-0.1348)
print(0.9439+1.4832+-1.7529+0.4045)
print(-0.1348+0.4045+-1.2136+-0.1348+0.9439+1.4832+-1.7529+0.4045)


-1.0787
1.0787000000000004
1.6653345369377348e-16
'''

理论部分:机器学习笔记:神经网络层的各种normalization_UQI-LIUWJ的博客-CSDN博客 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UQI-LIUWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>