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

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

时间:2022-11-28 16:03:54浏览次数:35  
标签: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)

 

标签:loss,layer,self,torch,神经网络,实验,test,手写,model
From: https://www.cnblogs.com/123yechao/p/16932419.html

相关文章

  • B-神经网络模型复杂度分析
    前言一,模型计算量分析卷积层FLOPs计算全连接层的FLOPs计算二,模型参数量分析卷积层参数量BN层参数量全连接层参数量三,模型内存访问代价计算卷积层MA......
  • 实验五
    #pragmaonce#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(){}MachinePets(conststri......
  • 神经网络反向传播
    【题目】1.下图为三层神经网络结构,表中为输入的4条样本数据,计算第2个训练样本的前向传播过程,网络参数的初始为:W1W1=[[0.1,0.2],[0.2,0.3]],theta1=[0.3,0.3],W2W2=[[0.4,......
  • 实验5 继承和多态
    task4pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;usingstd::string;classMachinePets{public:MachinePets(consts......
  • 实验五:全神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架......
  • 实验5
    task4'''pets.hpp'''#pragmaonceusingnamespacestd;#include<iostream>classMachinePets{friendvoidplay(MachinePets&obj);public:MachinePe......
  • 实验5
    实验5.4pets.hpp1#pragmaonce2#include<iostream>3#include<string>4usingnamespacestd;5classMachinePets{6public:7MachineP......
  • PGL图学习之图神经网络ERNIESage、UniMP进阶模型[系列八]
    PGL图学习之图神经网络ERNIESage、UniMP进阶模型[系列八]原项目链接:fork一下即可:https://aistudio.baidu.com/aistudio/projectdetail/5096910?contributionType=1相关项......
  • RNN循环神经网络根据不同国家特点和首字母生成名字name
    数据地址:https://download.pytorch.org/tutorial/data.zip1"""2@File:HomeWork6_RNN_names.py3@Author:silvan4@Time:2022/11/2810:565......
  • 实验五:全连接神经网络手写数字识别实验
    实验五:全连接神经网络手写数字识别实验 【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程......