目录
预备知识
导包
构建数据集
神经网络结构
训练测试精度可视化
计算模型精度
损失可视化
输出网络结构信息
训练神经网络
定义参数
载入数据
载入神经网络结构、损失及优化
训练及测试
损失、精度可视化
question
预备知识
- pytorch(二):torch常用API应用及详解、torch.max()
- 卷积层设置及输出大小计算
- torch.utils.data.DataLoader使用方法
- 加载手写数据集load_digits()
- sklearn的train_test_split()的参数含义解释
- torch.nn介绍
- nn.Sequential
- torch.nn.Conv2d()函数
- relu()激活函数、relu图像、其他相关函数
- nn.MaxPool2d()
- nn.Linear()全连接层
- x = x.view(x.size(0), -1)、Flatten
- nn.CrossEntropyLoss()
- torch.optim 优化器的使用
- torch的Module模块中named_parameters函数
- epoch
- torch的net.train() 与net.eval()
- enumerate(train_loader, start=0)函数的详解
- python __getitem__()方法理解(扩充)
- pytorch 损失函数及其应用代码详解、Pytorch十九种损失函数的使用、pytorch的nn.MSELoss损失函数
- loss.backward()
- print()+end=""+\r实现滚动条显示
- torch.no_grad和验证模式
- (predicted == labels).sum().item()作用
- python基础之np.arange函数
- Pytorch查看模型参数并计算模型参数量与可训练参数量
导包
import torch
from sklearn import datasets #加载数据集用
from sklearn import model_selection #数据集切分
import torch.utils.data as Data #分批次loader数据集使用
import torch.nn as nn #神经网络API
import torch.optim as optim #神经网络优化器
import numpy as np #数字格式转换
import matplotlib.pyplot as plt #数据可视化
构建数据集
class train_mini_train():'''构建训练集'''def __init__(self):# 加载手写数字数据集digits,有标签,class为1~10self.X,self.y = \datasets.load_digits(return_X_y=True)print('样本总数:',len(self.X))print('样本特征维度:', len(self.X[0]))#数据集切分self.X_train,self.X_test,self.y_train,self.y_test = \model_selection.train_test_split(self.X,self.y,random_state=0)print('构建训练集样本总数:', len(self.y_train))def __len__(self):#返回训练集数据量return len(self.y_train)def __getitem__(self, index):return torch.tensor(self.X_train[index].reshape(1,8,8),dtype=torch.float32),self.y_train[index]class test_mini_test():'''构建测试集'''def __init__(self):self.X,self.y=datasets.load_digits(return_X_y=True)self.X_train,self.X_test,self.y_train,self.y_test =\model_selection.train_test_split(self.X,self.y,random_state=0)print('构建测试集样本总数:', len(self.y_test))def __getitem__(self, index):return torch.tensor(self.X_test[index].reshape(1,8,8),dtype=torch.float32),self.y_test[index]def __len__(self):return len(self.y_test)
神经网络结构
class SimpleNet(nn.Module):'''定义神经网络结构'''def __init__(self):super(SimpleNet,self).__init__()self.conv1 = nn.Sequential(#(1, 8, 8)nn.Conv2d(in_channels=1,out_channels=4,kernel_size=2,stride=1,padding=1), #(4, 8, 8))self.conv2 = nn.Sequential(nn.ReLU(), # (4, 8, 8))self.conv3 = nn.Sequential(nn.MaxPool2d(kernel_size=2) # (4,4,4) 不改变通道数)self.conv4 = nn.Sequential(nn.Conv2d(in_channels=4,out_channels=8,kernel_size=2,stride=1,padding=1), #(8,5,5))self.conv5 = nn.Sequential(nn.ReLU(), # (8,5,5))self.conv6 = nn.Sequential(nn.MaxPool2d(kernel_size=2) # (8,2,2))self.fc = nn.Linear(8*2*2,10) #(10)def forward(self, x):x = self.conv1(x)x = self.conv2(x)x = self.conv3(x)x = self.conv4(x)x = self.conv5(x)x = self.conv6(x)x = x.view(x.size(0),-1) #相当于Flattenx = self.fc(x)return x
训练测试精度可视化
def plot_train_and_test_result(train_accs,test_accs):epoches = np.arange(1,len(train_accs)+1,dtype=np.int32)plt.plot(epoches,train_accs,label='train accuracy')plt.plot(epoches,test_accs, label='test accuracy')plt.xlabel('epoches')plt.ylabel('accuracy')plt.legend()
计算模型精度
def eval_on_dataloader(name,loader,len):acc = 0.0with torch.no_grad():for data in loader:images,labels = dataoutputs = net(images)# torch.max返回两个数值,一个[0]是最大值,一个[1]是最大值的下标predict_y = torch.max(outputs,dim=1)[1]acc += (predict_y == labels.long()).sum().item()accurate = acc/lenreturn accurate
损失可视化
def plot_loss_result(losses):epoches = np.arange(1, len(losses) + 1, dtype=np.int32)plt.plot(epoches, losses, label='loss')plt.xlabel('epoches')plt.ylabel('loss')plt.legend()
输出网络结构信息
def printNetInfo(net):'''输出神经网络结构的信息'''for name,parameters in net.named_parameters():print(name,":",parameters.size())
训练神经网络
定义参数
batch_size = 6
learning_rate = 3e-3
epoches = 10
载入数据
#载入训练集与测试集数据train_data = train_mini_train()test_data = test_mini_test()train_loader = Data.DataLoader(dataset=train_data,batch_size=batch_size,shuffle=True)test_loader = Data.DataLoader(dataset=test_data,batch_size=batch_size,shuffle=True)
载入神经网络结构、损失及优化
#载入网络结构net = SimpleNet()# 声明损失函数及优化器loss_fn = nn.CrossEntropyLoss()optim = optim.Adam(params=net.parameters(),lr=learning_rate)# 输出网络结构信息# printNetInfo(net)
训练及测试
train_accs = []test_accs = []losses = []for epoch in range(epoches):net.train() # 训练for step,data in enumerate(train_loader,start=0):images,labels = dataoptim.zero_grad() # 优化器梯度清0logits = net(images) # 输入images 经过网络推断输出结果loss = loss_fn(logits,labels.long()) # 计算损失函数loss.backward() # 反向传播求梯度optim.step() #优化器进一步优化rate = (step+1)/len(train_loader)a = "*"*int(rate*50)b = "."*int((1-rate)*50)print("\repoch:%s train loss:%3.0f%%:%.4f"%(epoch,int(rate*100),loss),end=" ")losses.append(loss)net.eval() # 测试train_acc = eval_on_dataloader("train",train_loader,train_data.__len__())train_accs.append(train_acc)test_acc = eval_on_dataloader("test", test_loader, test_data.__len__())test_accs.append(test_acc)print("train_acc:", train_acc, " test_acc:", test_acc)
损失、精度可视化
#模型训练损失可视化plot_loss_result(losses)plt.show()#训练集与测试集精度可视化plot_train_and_test_result(train_accs,test_accs)plt.show()
question
- AttributeError: module ‘sklearn‘ has no attribute ‘datasets‘解决
- RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'target'
solution:data.long()数据类型转换