Pytorch 简明教程
PyTorch - Introduction to Convents
卷积网络主要关于从头开始构建 CNN 模型。网络架构将包含以下步骤的组合 -
-
Conv2d
-
MaxPool2d
-
Rectified Linear Unit
-
View
-
Linear Layer
Training the Model
训练模型的过程与图像分类问题相同。以下代码片段完成了在给定数据集上训练模型的过程 -
def fit(epoch,model,data_loader,phase
= 'training',volatile = False):
if phase == 'training':
model.train()
if phase == 'training':
model.train()
if phase == 'validation':
model.eval()
volatile=True
running_loss = 0.0
running_correct = 0
for batch_idx , (data,target) in enumerate(data_loader):
if is_cuda:
data,target = data.cuda(),target.cuda()
data , target = Variable(data,volatile),Variable(target)
if phase == 'training':
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output,target)
running_loss + =
F.nll_loss(output,target,size_average =
False).data[0]
preds = output.data.max(dim = 1,keepdim = True)[1]
running_correct + =
preds.eq(target.data.view_as(preds)).cpu().sum()
if phase == 'training':
loss.backward()
optimizer.step()
loss = running_loss/len(data_loader.dataset)
accuracy = 100. * running_correct/len(data_loader.dataset)
print(f'{phase} loss is {loss:{5}.{2}} and {phase} accuracy is {running_correct}/{len(data_loader.dataset)}{accuracy:{return loss,accuracy}})
该方法包括用于训练和验证的不同逻辑。使用不同模式的主要原因有两个 -
-
在训练模式下,丢弃会删除一定百分比的值,而这在验证或测试阶段不应该发生。
-
对于训练模式,我们要计算梯度并更改模型参数的值,但在测试或验证阶段不需要反向传播。