0


基于pytorch平台实现对MNIST数据集的分类分析(前馈神经网络、softmax)基础版

基于pytorch平台实现对MNIST数据集的分类分析(前馈神经网络、softmax)基础版


文章目录


前言

本篇文章,将基于pytorch平台实现对MNIST数据集的分类分析,并分别以分类“的准确度”和“混淆矩阵”为衡量指标,分析模型的精度。


一、基于“前馈神经网络”模型,分类分析

注意:
根目录’E:\深度学习’,在这里根据自己实际情况建立;
运行程序时将自动下载数据集,数据集将下载至先前建立的根目录中,用于训练模型及测试。

import torch
from torch import nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torchvision.datasets as dsets
import torchvision.transforms as transforms

batch_size =100# MNIST dataset
train_dataset = dsets.MNIST(root='E:\深度学习', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = dsets.MNIST(root='E:\深度学习', train=False, transform=transforms.ToTensor(), download=True)# load_data
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True)# original_dataprint("train_data:", train_dataset.train_data.size())print("train_labels:", train_dataset.train_labels.size())print("test_data:", test_dataset.test_data.size())print("test_labels:", test_dataset.test_labels.size())# shuffle batch_size dataprint("batch_size:", train_loader.batch_size)print("load_train_data:", train_loader.dataset.train_data.shape)print("load_train_labels:", train_loader.dataset.train_labels.shape)

input_size =784
hidden_size =500
num_classes =10# #定义神经网络模型#一般把网络中具有可学习参数的层(如全连接层、卷积层等)放在构造函数__init__()中classNeural_net(nn.Module):#最重要__init__初始化方法,便于一些参数的传递def__init__(self, input_num, hidden_size, output_num):super(Neural_net, self).__init__()#调用父类构造函数,以继承父类一些属性
        self.layers1 = nn.Linear(input_num, hidden_size)
        self.layers2 = nn.Linear(hidden_size, output_num)# 最重要的forward方法,便于进行前向传播defforward(self, x):
        out = self.layers1(x)
        out = torch.relu(out)
        out = self.layers2(out)return out
net = Neural_net(input_size, hidden_size, num_classes)print(net)# training
learning_rate =1e-1
num_epoches =5
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate)for epoch inrange(num_epoches):print("current epoch = {}".format(epoch))for i,(images,labels)inenumerate(train_loader):
        images = Variable(images.view(-1,28*28))
        labels = Variable(labels)

        outputs = net(images)
        loss = criterion(outputs, labels)# calculate loss
        optimizer.zero_grad()# clear net state before backward
        loss.backward()
        optimizer.step()# update parametersif i%100==0:print("current loss = %.5f"%loss.item())# prediction
total =0
correct =0for images, labels in test_loader:
    images = Variable(images.view(-1,28*28))
    labels = Variable(labels)
    outputs = net(images)

    _,predicts = torch.max(outputs.data,1)
    total += labels.size(0)
    correct +=(predicts == labels).sum()from sklearn.metrics import confusion_matrix
    C2 = confusion_matrix(predicts, labels, labels=[0,1,2,3,4,5,6,7,8,9])import sklearn
    acc = sklearn.metrics.accuracy_score(labels, predicts)print("混淆矩阵",C2)print("混淆矩阵中的准确率为", acc)print("Accuracy = %.2f"%(100*correct/total))

二、基于“softmax”模型,分类分析

import torch
import torch.utils.data as Data
from torchvision import datasets,transforms
import torchvision
from torch.autograd import  Variable
import numpy as np
import matplotlib.pyplot as plt

transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=[0.5,],std=[0.5,])])
data_train=datasets.MNIST(root="D:\jupyter_data",  transform=transform, train=True,
                          download=True)
data_test=datasets.MNIST(root="D:\jupyter_data", transform=transform, train=False)

batch =256
train_iter = Data.DataLoader(dataset=data_train, batch_size=batch, shuffle=True)
test_iter = Data.DataLoader(data_test,batch, shuffle=True)print(train_iter)#定义和初始化模型for X,y in train_iter:print(X.shape)print(X.view(X.shape[0],-1).shape)break

num_inputs =784
num_outputs =10classLinearNet(torch.nn.Module):def__init__(self,num_inputs,num_outputs):super(LinearNet,self).__init__()
        self.linear = torch.nn.Linear(num_inputs,num_outputs)defforword(self,X):#这里X是28行28列.
        y=self.linear(X.view(X.shape[0],-1))return y  #参数中的-1就代表这个位置由其他位置的数字来推断,根据上面得到的shape,一批是256个,所以这里的shape[0]是256
net = LinearNet(num_inputs, num_outputs)
torch.nn.init.normal_(net.linear.weight, mean=0, std=0.01)
torch.nn.init.constant_(net.linear.bias, val=0)print(net)for X,y in train_iter:
    y_pre = net.forword(X)print(y_pre.shape)print(y_pre.view(y_pre.shape[0],-1).shape)break#定义损失函数
loss = torch.nn.CrossEntropyLoss()#定义优化算法
optimizer = torch.optim.SGD(net.linear.parameters(),lr=0.1)#训练模型
epoch_size =3defsoftmax_train(train_iter, test_iter, epoch_size, loss, optimizer, net, batchsize):for epoch inrange(0, epoch_size+1):
        train_acc_count =0
        train_loss =0
        n =0for X,y in train_iter:
            y_pre = net.forword(X)
            l = loss(y_pre, y).sum()#注意要加起来,这个是softmax+交叉熵的,之前的线性回归不用
            optimizer.zero_grad()#梯度清零
            l.backward()
            optimizer.step()#准确率
            train_acc_count +=(y_pre.argmax(dim=1)== y).sum().item()
            train_loss += l.item()#
            n += y.shape[0]print('epoch %d,loss %.4f, train acc %.3f'%(epoch +1, train_loss/n ,train_acc_count/n))
softmax_train(train_iter, test_iter, epoch_size, loss, optimizer, net, batch)deftest_acc(test_iter, net):
    test_acc_count =0
    n =0for X,y in test_iter:
        y_pre = net.forword(X)
        test_acc_count +=(y_pre.argmax(dim=1)== y).sum().item()
        n += y.shape[0]#y = y.detach().numpy()#y_pre = y_pre.detach().numpy()from sklearn.metrics import confusion_matrix
        C2 = confusion_matrix(y_pre.argmax(dim=1), y, labels=[0,1,2,3,4,5,6,7,8,9])import sklearn
        acc = sklearn.metrics.accuracy_score(y, y_pre.argmax(dim=1))print("混淆矩阵", C2)print("混淆矩阵中的准确率为", acc)print('test acc %.3f'%(test_acc_count/n))

test_acc(test_iter, net)

总结

以上是基于pytorch平台实现对MNIST数据集的分类分析(前馈神经网络、softmax)的“基础版”,升级版在下一篇博客基于pytorch平台实现对MNIST数据集的分类分析(前馈神经网络、softmax)升级版


本文转载自: https://blog.csdn.net/qq_51042577/article/details/124284146
版权归原作者 我是一颗跳跳糖哦 所有, 如有侵权,请联系我们删除。

“基于pytorch平台实现对MNIST数据集的分类分析(前馈神经网络、softmax)基础版”的评论:

还没有评论