深度学习中的线形层及其他层,你了解多少?
摘要:主要介绍神经网络线性层的计算,即torch.nn.Linear的原理及应用。并插入一些神经网络的其他层介绍,及调用pytorch中网络模型的方法。
一、正则化层中nn.BatchNorm2d简介
主要作用:对输入函数采用正则化。正则化的主要作用是加快神经网络的训练速度。
class torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, device=None, dtype=None)
输入参数:
num_features: 形状为\((N, C, H, W)\)
其他参数默认即可
举例:
# With Learnable Parameters
m = nn.BatchNorm2d(100)
# Without Learnable Parameters
m = nn.BatchNorm2d(100, affine=False)
input = torch.randn(20, 100, 35, 45)
output = m(input)
该函数用得不多
二、其他层简介
1. Recurrent Layers(Recurrent层)
内含RNN、LSTM等函数,主要在nlp领域用的比较多
官方文档: Recurrent Layers
2. Transformer Layers
3. Linear Layers(线性层)
nn.Linear
class torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None
(1)参数介绍及计算方法
参数介绍:
in_features
out_features
bias(bool)
线性层具体参数解释如下图:
\(in\_features=d\),即指的是in_features的个数
\(out\_features=L\),即指的是out_features的个数
计算\(g\)的方法(以上图\(g_1\)为例):
\(x_1,\dots,x_i,\dots,x_d\)每个指向\(g_1\)的箭头上,均有:
\[k_i*x_i+b_i
\]
其中,\(b_i\)代表偏置,参数\(bias=True\),则加上\(b\);\(bias=False\),则不加\(b\)
在每次训练神经网络的过程中,均会调整\(k_i\)、\(b_i\)的值,直到它变成一个合适的数值
由此可得:
\[g_1=\sum^{d}_{i=1}{k_ix_i+b_i}
\]
(2)代码示例
以典型的VGG16 Model网络结构为例:
因此,设置in_features=4096; out_feature=1000
下面代码以一个尺寸为n×n的图像为例,先将图像展开成一行,即1×\(n^2\)的尺寸。最后将1×\(n^2\)尺寸的图像通过线性层,转化为1×10尺寸的图像。
