读pytroch使用resnet18网络的代码

前言

在学pytorch,读读调用resnet训练模型的源码。
代码随便找了个,来自 b站up主Zomin的这个视频,项目代码在评论区置顶。

整体框架

在这里插入图片描述

单独看其中一次训练:

核心代码:

# 遍历data loader里的数据,对每个数据进行训练
# 梯度归零-正向传递(计算输出)-计算损失-反向传递-
for data, target in train_loader: 
data = data.to(device)
target = target.to(device)

# clear the gradients of all optimized variables(清除梯度)
optimizer.zero_grad()

# forward pass: compute predicted outputs by passing inputs to the model
# (正向传递:通过向模型传递输入来计算预测输出)
output = model(data).to(device)  

#(等价于output = model.forward(data).to(device) )
# calculate the batch loss(计算损失值)
loss = criterion(output, target)

# backward pass: compute gradient of the loss with respect to model parameters
# (反向传递:计算损失相对于模型参数的梯度)
loss.backward()

# perform a single optimization step (parameter update)
# 执行单个优化步骤(参数更新)
optimizer.step()

# update training loss(更新损失)
train_loss += loss.item()*data.size(0)

Moudule类的_call_impl()函数

通过该方法调用模型中的对应函数。

def _call_impl(self, *input, **kwargs):
 forward_call = (self._slow_forward if torch._C._get_tracing_state() else self.forward)
 # If we don't have any hooks, we want to skip the rest of the logic in
 # this function, and just call forward.
 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
         or _global_forward_hooks or _global_forward_pre_hooks):
     return forward_call(*input, **kwargs)

前向传播代码

在ResNet类中的_forward_impl(self,x)函数中,完成了残差网络的一次完整运算,主要分为三个部分。

def _forward_impl(self, x):
 # See note [TorchScript super()]
 x = self.conv1(x)
 x = self.bn1(x)
 x = self.relu(x)
 #x = self.maxpool(x)

 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)

 x = self.avgpool(x)
 x = torch.flatten(x, 1)
 x = self.fc(x)

 return x

一、降采样卷积

原版resnet采用7X7降采样卷积,但是因为cifar-10图片很小因此代码中改为3X3卷积并取消了最大池化层,减小数据损失。

x = self.conv1(x) # 卷积层
x = self
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值