for i inrange(epoch): print("----------------第{}轮训练开始-------------".format(i+1))
#训练步骤开始 for data in train_dataloader: imgs,targets=data outputs=network(imgs) loss=loss_fn(outputs,targets) #优化器优化模型 optimizer.zero_grad()#优化器梯度清零 loss.backward()#反向传播 optimizer.step()#优化器进行优化
for data in train_dataloader: imgs,targets=data outputs=network(imgs) loss=loss_fn(outputs,targets) #优化器优化模型 optimizer.zero_grad()#优化器梯度清零 loss.backward()#反向传播 optimizer.step()#优化器进行优化
total_train_step=total_train_step+1#记录训练次数 if total_train_step %100==0:#每100次才打印 print("训练次数:{},Loss:{}".format(total_train_step,loss.item()))
每100次才打印数据 ### 开始测试步骤
训练完毕之后,我们接下来采用测试数据进行测试
1 2 3 4 5 6 7 8
total_test_loss=0 with torch.no_grad(): for data in test_dataloader: imgs,targets =data outputs =network(imgs)#输入放到网络当中我们可以得到对应输出 loss =loss_fn(outputs,targets)#计算误差 total_test_loss=total_test_loss+loss.item() print("整体测试集上的Loss:{}".format(total_test_loss))
total_test_loss=0 total_accuracy=0#整体正确率 with torch.no_grad(): for data in test_dataloader: imgs,targets =data outputs =network(imgs)#输入放到网络当中我们可以得到对应输出 loss =loss_fn(outputs,targets)#计算误差 total_test_loss=total_test_loss+loss.item() accuracy=(outputs.argmax(1)==targets).sum()#横向准确率 total_accuracy=total_accuracy+accuracy print("整体测试集上的Loss:{}".format(total_test_loss)) print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size)) writer.add_scalar("test_loss",total_test_loss,total_test_step)##标题,测试误差值,测试次数 writer.add_scalar("test_accuracy",total_accuracy/test_data_size,total_test_step) total_test_step=total_test_step+1
import torchvision from torch import nn from torch.utils.data import DataLoader from torch.utils.tensorboard import SummaryWriter
from model import *#引入model中的所有import ##准备数据集 train_data=torchvision.datasets.CIFAR10(root="../data",train=True,transform=torchvision.transforms.ToTensor(), download=True)##数据集的目录,是否训练,将PIL型变为Tensor型,需要下载
for i inrange(epoch): print("----------------第{}轮训练开始-------------".format(i+1))
#训练步骤开始 network.train() for data in train_dataloader: imgs,targets=data outputs=network(imgs) loss=loss_fn(outputs,targets) #优化器优化模型 optimizer.zero_grad()#优化器梯度清零 loss.backward()#反向传播 optimizer.step()#优化器进行优化
total_train_step=total_train_step+1#记录训练次数 if total_train_step %100==0:#每100次才打印 print("训练次数:{},Loss:{}".format(total_train_step,loss.item())) writer.add_scalar("train_loss",loss.item(),total_train_step)##标题,loss值,训练次数 #测试步骤开始 network.eval() total_test_loss=0 total_accuracy=0#整体正确率 with torch.no_grad():#网络模型没有梯度,不需要梯度优化 for data in test_dataloader: imgs,targets =data outputs =network(imgs)#输入放到网络当中我们可以得到对应输出 loss =loss_fn(outputs,targets)#计算误差 total_test_loss=total_test_loss+loss.item() accuracy=(outputs.argmax(1)==targets).sum()#横向准确率 total_accuracy=total_accuracy+accuracy print("整体测试集上的Loss:{}".format(total_test_loss)) print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size)) writer.add_scalar("test_loss",total_test_loss,total_test_step)##标题,测试误差值,测试次数 writer.add_scalar("test_accuracy",total_accuracy/test_data_size,total_test_step) total_test_step=total_test_step+1 torch.save(network,"network_{}.pth".format(i))#保存每一轮的训练结果 print("模型已保存")
writer.close()
利用GPU去训练神经网络
只需要稍作改动
1 2 3 4
#创建网络模型 network=NetWork() if torch.cuda.is_available(): network=network.cuda()#利用gpu进行训练