(如有侵权,联系作者马上删除)
(残差结构是指在深度学习模型中使用残差连接(residual connections)的一种网络结构。在ResNet(Residual Network)模型中得到了广泛应用。
在传统的神经网络中,信息通过层与层之间的权重传递,而每一层的输出都需要经过非线性变换(如ReLU激活函数)。然而,随着网络层数的增加,梯度消失和梯度爆炸等问题会逐渐显现,导致训练困难。残差结构通过引入跳跃连接来解决这个问题。
具体而言,残差结构允许网络直接学习残差函数,即将前一层的输出和当前层的输入相加(本质),从而产生当前层的输出。这样做可以有效地传递梯度,并减轻了梯度消失和梯度爆炸的影响。此外,残差结构还有助于模型的优化和训练,以及提高了网络的表示能力。
在实际应用中,残差结构被广泛应用于深度卷积神经网络(CNN)和深度残差循环神经网络(Residual Recurrent Neural Network)等模型中,取得了很好的效果。通过使用残差结构,深度网络可以更容易地训练和优化,并在各种计算机视觉、自然语言处理等任务中取得了显著的性能提升。)-----------------笼统解释
1.Resnet优势:解决网络退化现象(不是过拟合)
(1)网络退化:(补课的孩子成绩比没有补课的还差)
①网络退化是指在深度神经网络中,随着网络层数的增加,模型的性能开始下降
②网络退化的原因主要有两个方面:
梯度消失:当网络层数较多时,反向传播算法需要从输出层反向传播到输入层,这会使得梯度在每一层中逐渐变小,最终可能消失。当梯度消失时,模型无法学习到有效的特征表示,从而导致性能下降。
权重病态:当网络层数较多时,权重的值容易出现病态(ill-conditioned)情况,即权重值之间的差异很大,使得网络变得不稳定。这会导致网络收敛速度变慢,以及性能下降。
如图所示:
(2)能解决的原因
2.工作示意
(红框是使用前的,蓝框是使用后的,蓝框能保证效果≥红框的)
3.残差概念=数学的
4.公式(红框内的)
5.代码(橙色方框内对应的) 示例代码①
def res_block_v1(x, input_filter, output_filter):
res_x = Conv2D(kernel_size=(3,3), filters=output_filter, strides=1, padding='same')(x)
res_x = BatchNormalization()(res_x)
res_x = Activation('relu')(res_x)
res_x = Conv2D(kernel_size=(3,3), filters=output_filter, strides=1, padding='same')(res_x)
res_x = BatchNormalization()(res_x)
if input_filter == output_filter:
identity = x
else: #需要升维或者降维
identity = Conv2D(kernel_size=(1,1), filters=output_filter, strides=1, padding='same')(x)
x = keras.layers.add([identity, res_x])
output = Activation('relu')(x)
return output
示例代码②
class ResBlock2(nn.Module):
def __init__(self,inchannel):
super(ResBlock2,self).__init__()
self.model = nn.Sequential(
nn.Conv2d(inchannel, inchannel, kernel_size=1,stride=1,padding=0),
nn.Conv2d(inchannel, inchannel, kernel_size=3,stride=2,padding=1),
nn.Conv2d(inchannel, inchannel, kernel_size=1,stride=1,padding=0),
nn.ReLU(),
nn.BatchNorm2d(inchannel),
nn.BatchNorm2d(inchannel),
nn.MaxPool2d(kernel_size=2,stride=2,padding=0),
nn.BatchNorm2d(inchannel*2)
)
def forward(self, x):
out0=self.model[6](x).........................输入残差
out1 = self.model[0](x)
out1 = self.model[3](out1)
out1 = self.model[4](out1)
out1=self.model[1](out1)
out1 = self.model[3](out1)
out1 = self.model[5](out1)
out1 = self.model[2](out1)
out1 = torch.cat((out1,out0), dim=1)............................组合残差
out1=self.model[7](out1)
out1 = self.model[3](out1)
return out1
上述ResBlock中黄色边框即是残差块