PyTorch 教程-卷积神经网络模型的训练
在上一个主题中,我们实现了我们的CNN模型。现在,我们的下一个任务是对其进行训练。为了训练我们的CNN模型,我们将使用CUDA张量类型,它将执行与CPU张量相同的功能,但用于计算。
训练我们的CNN模型的步骤如下:
步骤1:
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
步骤2:
model=LeNet.to(device)
步骤3:
criteron=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(model.parameters(),lr=0.00001)
步骤4:
epoch=12loss_history=[]correct_history=[]
👉点击领取:最全Python资料合集
步骤5:
for e in range(epochs):for input, labels in training_loader:
步骤6:
我们正在处理卷积神经网络,其中输入首先被传递。我们将图像传递给四个维度,因此无需将其展平。
input=input.to(device)labels=labels.to(device)
outputs=model(inputs)
步骤7:
loss1=criteron(outputs,labels)optimizer.zero_grad()loss1.backward()optimizer.step()
步骤8:
loss+=loss1.item()
epoch_loss=loss/len(training_loader)loss_history.append(epoch_loss)
步骤9:
在接下来的步骤中,我们将找到网络的准确性。我们将初始化correct变量并将其值设为零。我们将比较模型对每个训练图像进行的预测与图像的实际标签,以显示在一个epoch内有多少个图像被正确分类。
_,preds=torch.max(outputs,1)
步骤10:
correct+=torch.sum(preds==labels.data)
epoch_acc=correct.float()/len(training_loader)print('training_loss:{:.4f},{:.4f}'.format(epoch_loss,epoch_acc.item()))
步骤11:
plt.plot(loss_history,label='Running Loss History')plt.plot(correct_history,label='Running correct History')
完整代码:
import torchimport matplotlib.pyplot as pltimport numpy as npimport torch.nn.functional as funcimport PIL.ImageOpsfrom torch import nnfrom torchvision import datasets,transformsdevice=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")transform1=transforms.Compose([transforms.Resize((28,28)),transforms.ToTensor(),transforms.Normalize((0.5,),(0.5,))])training_dataset=datasets.MNIST(root='./data',train=True,download=True,transform=transform1)training_loader=torch.utils.data.DataLoader(dataset=training_dataset,batch_size=100,shuffle=True)class LeNet(nn.Module):def __init__(self):super().__init__()self.conv1=nn.Conv2d(1,20,5,1)self.conv2=nn.Conv2d(20,50,5,1)self.fully1=nn.Linear(4*4*50,500)self.fully2=nn.Linear(500,10)def forward(self,x):x=func.relu(self.conv1(x))x=func.max_pool2d(x,2,2)x=func.relu(self.conv2(x))x=func.max_pool2d(x,2,2)x=x.view(-1,4*4*50) #Reshaping the output into desired shapex=func.relu(self.fully1(x)) #Applying relu activation function to our first fully connected layerx=self.fully2(x) #We will not apply activation function here because we are dealing with multiclass datasetreturn xmodel=LeNet().to(device)criteron=nn.CrossEntropyLoss()optimizer=torch.optim.Adam(model.parameters(),lr=0.00001)epochs=12loss_history=[]correct_history=[]for e in range(epochs):loss=0.0correct=0.0for e in range(epochs):loss=0.0correct=0.0for inputs,labels in training_loader:inputs=inputs.to(device)labels=labels.to(device)outputs=model(inputs)loss1=criteron(outputs,labels)optimizer.zero_grad()loss1.backward()optimizer.step()_,preds=torch.max(outputs,1)loss+=loss1.item()correct+=torch.sum(preds==labels.data)else:epoch_loss=loss/len(training_loader)epoch_acc=correct.float()/len(training_loader)loss_history.append(epoch_loss)correct_history.append(epoch_acc)print('training_loss:{:.4f},{:.4f}'.format(epoch_loss,epoch_acc.item()))plt.plot(loss_history,label='Running Loss History')plt.plot(correct_history,label='Running correct History')plt.show()1 热门推荐