实验五:全连接神经网络手写数字识别实验
【实验目的】
理解神经网络原理,掌握神经网络前向推理和后向传播方法;
掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。
【实验内容】
1.使用pytorch框架,设计一个全连接神经网络,实现Mnist手写数字字符集的训练与识别。
【实验报告要求】
修改神经网络结构,改变层数观察层数对训练和检测时间,准确度等参数的影响;
修改神经网络的学习率,观察对训练和检测效果的影响;
修改神经网络结构,增强或减少神经元的数量,观察对训练的检测效果的影响。
实验内容:
1 import torch 2 import numpy as np 3 from matplotlib import pyplot as plt 4 from torch.utils.data import DataLoader 5 from torchvision import transforms 6 from torchvision import datasets 7 import torch.nn.functional as F
1 batch_size = 64 2 learning_rate = 0.01 3 momentum = 0.5 4 EPOCH = 10 5 transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) 6 train_dataset = datasets.MNIST(root='./data/mnist', train=True, transform=transform, download=True) 7 test_dataset = datasets.MNIST(root='./data/mnist', train=False, transform=transform, download=True) 8 train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) 9 test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False) 10 class Net(torch.nn.Module): 11 def __init__(self): 12 super(Net, self).__init__() 13 self.conv1 = torch.nn.Sequential( 14 torch.nn.Conv2d(1, 10, kernel_size=5), 15 torch.nn.ReLU(), 16 torch.nn.MaxPool2d(kernel_size=2), 17 ) 18 self.conv2 = torch.nn.Sequential( 19 torch.nn.Conv2d(10, 20, kernel_size=5), 20 torch.nn.ReLU(), 21 torch.nn.MaxPool2d(kernel_size=2), 22 ) 23 self.fc = torch.nn.Sequential( 24 torch.nn.Linear(320, 50), 25 torch.nn.Linear(50, 10), 26 ) 27 28 def forward(self, x): 29 batch_size = x.size(0) 30 x = self.conv1(x) # 一层卷积层,一层池化层,一层激活层 31 x = self.conv2(x) 32 x = x.view(batch_size, -1) 33 x = self.fc(x) 34 return x 35 #实例化模型 36 model = Net() 37 criterion = torch.nn.CrossEntropyLoss() # 交叉熵损失 38 optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum) # lr学习率,momentum冲量 39 40 def train(epoch): 41 running_loss = 0.0 42 running_total = 0 43 running_correct = 0 44 for batch_idx, data in enumerate(train_loader, 0): 45 inputs, target = data 46 optimizer.zero_grad() 47 outputs = model(inputs) 48 loss = criterion(outputs, target) 49 loss.backward() 50 optimizer.step() 51 running_loss += loss.item() 52 # 把运行中的准确率acc算出来 53 _, predicted = torch.max(outputs.data, dim=1) 54 running_total += inputs.shape[0] 55 running_correct += (predicted == target).sum().item() 56 if batch_idx % 300 == 299: # 不想要每一次都出loss,浪费时间,选择每300次出一个平均损失,和准确率 57 print('[%d, %5d]: loss: %.3f , acc: %.2f %%' 58 % (epoch + 1, batch_idx + 1, running_loss / 300, 100 * running_correct / running_total)) 59 running_loss = 0.0 # 这小批300的loss清零 60 running_total = 0 61 running_correct = 0 # 这小批300的acc清零 62 63 #测试轮 64 def test(): 65 correct = 0 66 total = 0 67 with torch.no_grad(): # 测试集不用算梯度 68 for data in test_loader: 69 images, labels = data 70 outputs = model(images) 71 _, predicted = torch.max(outputs.data, dim=1) # dim = 1 列是第0个维度,行是第1个维度,沿着行(第1个维度)去找1.最大值和2.最大值的下标 72 total += labels.size(0) # 张量之间的比较运算 73 correct += (predicted == labels).sum().item() 74 acc = correct / total 75 print('[%d / %d]: Accuracy on test set: %.1f %% ' % (epoch+1, EPOCH, 100 * acc)) # 求测试的准确率,正确数/总数 76 return acc 77 #主函数:共进行10轮次的训练:每训练一轮,就进行一次测试。 78 if __name__ == '__main__': 79 acc_list_test = [] 80 for epoch in range(EPOCH): 81 train(epoch) 82 # if epoch % 10 == 9: #每训练10轮 测试1次 83 acc_test = test() 84 acc_list_test.append(acc_test)
1 #举例展示部分图 2 import matplotlib.pyplot as plt; 3 fig = plt.figure() 4 for i in range(16): 5 plt.subplot(4, 4, i+1) 6 z=train_dataset.train_data[i] 7 m=train_dataset.train_labels[i] 8 plt.imshow(z, cmap='gray', interpolation='none') 9 plt.title("Labels: {}".format(m)) 10 plt.xticks([]) 11 plt.yticks([]) 12 plt.show()
1 y_test=acc_list_test 2 plt.plot(y_test) 3 plt.xlabel("Epoch") 4 plt.ylabel("Accuracy On TestSet") 5 plt.show()
标签:plt,nn,torch,running,神经网络,实验,test,手写,size From: https://www.cnblogs.com/Xu820228/p/16934250.html