pytorch中LN(LayerNorm)及Relu和其变相的输出操作

作者:永远的小白虾 时间:2022-05-07 07:19:55 

主要就是了解一下pytorch中的使用layernorm这种归一化之后的数据变化,以及数据使用relu,prelu,leakyrelu之后的变化。


import torch
import torch.nn as nn
import torch.nn.functional as F
class model(nn.Module):
   def __init__(self):
       super(model, self).__init__()
       self.LN=nn.LayerNorm(10,eps=0,elementwise_affine=True)
       self.PRelu=nn.PReLU(init=0.25)
       self.Relu=nn.ReLU()
       self.LeakyReLU=nn.LeakyReLU(negative_slope=0.01,inplace=False)
   def forward(self,input ):
       out=self.LN(input)
       print("LN:",out)
       out1=self.PRelu(out)
       print("PRelu:",out1)
       out2=self.Relu(out)
       print("Relu:",out2)
       out3=self.LeakyReLU(out)
       print("LeakyRelu:",out3)
       return out
tensor=torch.tensor([-0.9,0.1,0,-0.1,0.9,-0.4,0.9,-0.5,0.8,0.1])
net=model()
print(tensor)
net(tensor)

输出:

tensor([-0.9000,  0.1000,  0.0000, -0.1000,  0.9000, -0.4000,  0.9000, -0.5000,
         0.8000,  0.1000])
LN: tensor([-1.6906,  0.0171, -0.1537, -0.3245,  1.3833, -0.8368,  1.3833, -1.0076,
         1.2125,  0.0171], grad_fn=<NativeLayerNormBackward>)
Relu: tensor([0.0000, 0.0171, 0.0000, 0.0000, 1.3833, 0.0000, 1.3833, 0.0000, 1.2125,
        0.0171], grad_fn=<ReluBackward0>)
PRelu: tensor([-0.4227,  0.0171, -0.0384, -0.0811,  1.3833, -0.2092,  1.3833, -0.2519,
         1.2125,  0.0171], grad_fn=<PreluBackward>)
LeakyRelu: tensor([-0.0169,  0.0171, -0.0015, -0.0032,  1.3833, -0.0084,  1.3833, -0.0101,
         1.2125,  0.0171], grad_fn=<LeakyReluBackward0>)

从上面可以看出,这个LayerNorm的归一化,并不是将数据限定在0-1之间,也没有进行一个类似于高斯分布一样的分数,只是将其进行了一个处理,对应的数值得到了一些变化,相同数值的变化也是相同的。

Relu的则是单纯将小于0的数变成了0,减少了梯度消失的可能性

PRelu是一定程度上的保留了负值,根据init给的值。

LeakyRelu也是一定程度上保留负值,不过比较小,应该是根据negative_slope给的值。

补充:PyTorch学习之归一化层(BatchNorm、LayerNorm、InstanceNorm、GroupNorm)

BN,LN,IN,GN从学术化上解释差异:

BatchNorm:batch方向做归一化,算NHW的均值,对小batchsize效果不好;BN主要缺点是对batchsize的大小比较敏感,由于每次计算均值和方差是在一个batch上,所以如果batchsize太小,则计算的均值、方差不足以代表整个数据分布

LayerNorm:channel方向做归一化,算CHW的均值,主要对RNN作用明显;

InstanceNorm:一个channel内做归一化,算H*W的均值,用在风格化迁移;因为在图像风格化中,生成结果主要依赖于某个图像实例,所以对整个batch归一化不适合图像风格化中,因而对HW做归一化。可以加速模型收敛,并且保持每个图像实例之间的独立。

GroupNorm:将channel方向分group,然后每个group内做归一化,算(C//G)HW的均值;这样与batchsize无关,不受其约束。

SwitchableNorm是将BN、LN、IN结合,赋予权重,让网络自己去学习归一化层应该使用什么方法。

pytorch中LN(LayerNorm)及Relu和其变相的输出操作

1 BatchNorm


torch.nn.BatchNorm1d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
torch.nn.BatchNorm3d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)

参数:

num_features: 来自期望输入的特征数,该期望输入的大小为'batch_size x num_features [x width]'

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

momentum: 动态均值和动态方差所使用的动量。默认为0.1。

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

track_running_stats:布尔值,当设为true,记录训练过程中的均值和方差;

实现公式:

track_running_stats:布尔值,当设为true,记录训练过程中的均值和方差;

实现公式:

pytorch中LN(LayerNorm)及Relu和其变相的输出操作

2 GroupNorm


torch.nn.GroupNorm(num_groups, num_channels, eps=1e-05, affine=True)

参数:

num_groups:需要划分为的groups

num_features:来自期望输入的特征数,该期望输入的大小为'batch_size x num_features [x width]'

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

momentum:动态均值和动态方差所使用的动量。默认为0.1。

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

实现公式:

pytorch中LN(LayerNorm)及Relu和其变相的输出操作

3 InstanceNorm


torch.nn.InstanceNorm1d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
torch.nn.InstanceNorm2d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
torch.nn.InstanceNorm3d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)

参数:

num_features:来自期望输入的特征数,该期望输入的大小为'batch_size x num_features [x width]'

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

momentum:动态均值和动态方差所使用的动量。默认为0.1。

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

track_running_stats:布尔值,当设为true,记录训练过程中的均值和方差;

实现公式:

pytorch中LN(LayerNorm)及Relu和其变相的输出操作

4 LayerNorm


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

参数:

normalized_shape: 输入尺寸


[∗×normalized_shape[0]×normalized_shape[1]×…×normalized_shape[−1]]

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

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

实现公式:

pytorch中LN(LayerNorm)及Relu和其变相的输出操作

5 LocalResponseNorm


torch.nn.LocalResponseNorm(size, alpha=0.0001, beta=0.75, k=1.0)

参数:

size:用于归一化的邻居通道数

alpha:乘积因子,Default: 0.0001

beta :指数,Default: 0.75

k:附加因子,Default: 1

实现公式:

pytorch中LN(LayerNorm)及Relu和其变相的输出操作

来源:https://blog.csdn.net/qq_41487299/article/details/106947938

标签:pytorch,LayerNorm,Relu
0
投稿

猜你喜欢

  • 设计较好付款流程的12个建议

    2009-06-08 12:45:00
  • javascript设计模式交流(一)Singleton Pattern

    2007-11-29 13:20:00
  • pytest多重断言的实现

    2021-10-12 03:30:25
  • Python爬虫分析微博热搜关键词的实现代码

    2022-11-29 16:13:44
  • Mysql中日期和时间函数介绍

    2008-05-24 08:16:00
  • Python的进程及进程池详解

    2021-05-18 08:51:46
  • 五分钟学会Python 模块和包、文件

    2023-06-01 20:05:38
  • python not关键字实例用法

    2023-12-05 10:25:50
  • TensorFlow深度学习之卷积神经网络CNN

    2022-07-03 17:43:15
  • python接口自动化之ConfigParser配置文件的使用详解

    2023-09-07 02:56:04
  • 如何用Cookie进行登录验证?

    2010-06-12 12:34:00
  • 将matplotlib绘图嵌入pyqt的方法示例

    2022-11-10 06:59:14
  • 详解python中index()、find()方法

    2021-02-25 02:38:59
  • python实现简易名片管理系统

    2022-12-23 13:14:53
  • pytorch 查看cuda 版本方式

    2022-07-16 23:43:05
  • asp.net实现图片以二进制流输出的两种方法

    2023-06-28 21:49:02
  • text-indent 隐藏文字时出现的 outline问题

    2007-12-02 17:31:00
  • 基于Python数据可视化利器Matplotlib,绘图入门篇,Pyplot详解

    2022-11-20 07:59:16
  • Python之字典对象的几种创建方法

    2023-04-28 13:15:57
  • 21行Python代码实现拼写检查器

    2023-10-11 03:33:03
  • asp之家 网络编程 m.aspxhome.com