首页 > 其他分享 >实验五:全连接神经网络手写数字识别实验

实验五:全连接神经网络手写数字识别实验

时间:2022-11-27 11:00:10浏览次数:37  
标签:loss layer self torch 神经网络 实验 test 手写 model

【实验目的】

理解神经网络原理,掌握神经网络前向推理和后向传播方法;

掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。

【实验内容】

1.使用pytorch框架,设计一个全连接神经网络,实现Mnist手写数字字符集的训练与识别。

 

【实验报告要求】

修改神经网络结构,改变层数观察层数对训练和检测时间,准确度等参数的影响;
修改神经网络的学习率,观察对训练和检测效果的影响;
修改神经网络结构,增强或减少神经元的数量,观察对训练的检测效果的影响。

 


import torch import torch.nn.functional as functional import torch.optim as optim from torch.utils.data import DataLoader from torchvision import datasets from torchvision import transforms # global definitions BATCH_SIZE = 100 MNIST_PATH = "../../../Data/MNIST" # transform sequential transform = transforms.Compose([ transforms.ToTensor(), # mean std transforms.Normalize((0.1307,), (0.3081,)) ]) # training dataset train_dataset = datasets.MNIST(root=MNIST_PATH, train=True, download=True, transform=transform) # training loader train_loader = DataLoader(train_dataset, shuffle=True, batch_size=BATCH_SIZE) # test dataset test_dataset = datasets.MNIST(root=MNIST_PATH, train=False, download=True, transform=transform) # test loader test_loader = DataLoader(test_dataset, shuffle=False, batch_size=BATCH_SIZE) class FullyNeuralNetwork(torch.nn.Module): def __init__(self): super().__init__() # layer definitions self.layer_1 = torch.nn.Linear(784, 512) # 28 x 28 = 784 pixels as input self.layer_2 = torch.nn.Linear(512, 256) self.layer_3 = torch.nn.Linear(256, 128) self.layer_4 = torch.nn.Linear(128, 64) self.layer_5 = torch.nn.Linear(64, 10) def forward(self, data): # transform the image view x = data.view(-1, 784) # do forward calculation x = functional.relu(self.layer_1(x)) x = functional.relu(self.layer_2(x)) x = functional.relu(self.layer_3(x)) x = functional.relu(self.layer_4(x)) x = self.layer_5(x) # return results return x def train(epoch, model, criterion, optimizer): running_loss = 0.0 for batch_idx, data in enumerate(train_loader, 0): inputs, target = data optimizer.zero_grad() # forward, backward, update outputs = model(inputs) loss = criterion(outputs, target) loss.backward() optimizer.step() # print loss running_loss += loss.item() if batch_idx % 100 == 0: print('[%d, %5d] loss: %.3f' % (epoch, batch_idx, running_loss / 100)) running_loss = 0.0 def test(model): correct = 0 total = 0 with torch.no_grad(): for images, labels in test_loader: outputs = model(images) _, predicated = torch.max(outputs.data, dim=1) total += labels.size(0) correct += (predicated == labels).sum().item() print("Accuracy on test set: %d %%" % (100 * correct / total)) if __name__ == "__main__": # full neural network model model = FullyNeuralNetwork() # LOSS function criterion = torch.nn.CrossEntropyLoss() # parameters optimizer # stochastic gradient descent optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.5) # training and do gradient descent calculation for epoch in range(5): # training data train(epoch, model, criterion, optimizer) # test model test(model)

 

 

#测试集上准确率
y_test=acc_list_test
plt.plot(y_test)
plt.xlabel("Epoch")
plt.ylabel("Accuracy On TestSet")
plt.show()

 

标签:loss,layer,self,torch,神经网络,实验,test,手写,model
From: https://www.cnblogs.com/feng-ying201613305/p/16929141.html

相关文章

  • 实验5 继承和多态
    task4Pets.h#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings);virtual......
  • 实验五:全连接神经网络手写数字识别实验
    实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,设......
  • canvas绘制手写签名【移动端+web】--支持下载
    一个简单的实例<head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-widt......
  • 弱口令实验室招新赛 WP
    MISC签到扫描二维码关注青少年CTF,输入ls查看当前目录文件,发现flag,直接cat得到答案EasyMisc先放到010里面看,发现是压缩包文件,改后缀为zip 查看可以看到Base64解码......
  • 实验5 继承和多态
    task4:pets.hpp1#pragmaonce2#include<iostream>3#include<string>4usingnamespacestd;56//定义基类,机器宠物类MachinePets7classMachinePe......
  • RNA-seq 详细教程:实验设计(2)
    学习目标了解设置重复对于RNA-seq分析的重要性了解生物重复次数、测序深度和鉴定到的差异表达基因之间的关系了解如何设计RNA-seq实验,以避免批次效应1.注意事项......
  • 实验五 继承和多态
    实验任务4#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:stringnickname;MachinePets(conststri......
  • 实验三·bdev原理和源码分析
    任务配置bdev运行环境运行hello_bdev程序并分析源码通过bdev接口写入数据并读取Bdev是在物理设备上的进一步抽象,物理层有NVM、NVMeOF、CEPHRBD等,通过bdev向......
  • 实验四·blobstore原理和源码分析
    实验任务学习Blob基本原理完成hello_blob程序运行修改底层bdev为nvmeBlob构建在bdev之上,是对数据存储的进一步抽象,类似于文件,但是不具备文件POSIX接口,可近似按文件形......
  • 实验五:全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......