Pytorch CNN网络MNIST数据集图片识别

该博客介绍了使用PyTorch构建和训练卷积神经网络(CNN)进行MNIST手写数字识别的过程。首先配置库和参数,然后加载MNIST数据集并进行预处理。接着,定义了一个简单的CNN模型,包含两个卷积层和全连接层。通过交叉熵损失函数和随机梯度下降优化器进行模型训练,并在训练过程中记录损失和准确率。最后,对模型进行评估,展示测试集上的损失和准确率。
摘要由CSDN通过智能技术生成

Pytorch CNN网络MNIST数据集图片识别
1、配置库和配置参数
2、加载MNIST数据集
3、创建CNN模型
4、训练模型
5、模型评估

#1、配置库和配置参数

import torch
from torch import nn, optim
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
import time

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

#配置参数
torch.manual_seed(1)    #设置随机种子,确保结果可重复性
batch_size = 128    #批处理大小
learning_rate = 1e-2    #学习率
num_epoches = 2    #训练次数

#2、加载MNIST数据集
transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize([0.5], [0.5])]
)

#2、加载MNIST数据集
train_dataset = datasets.MNIST(root=r'D:\file\PythonProjict\dataset', train=True, transform=transform, download=True)   #保存位置,训练集数据,转换为Tensor,下载数据
test_dataset = datasets.MNIST(root=r'D:\file\PythonProjict\dataset', train=False, transform=transform)

#训练数据的加载方式:每次从train_dataset中随机(shuffle=True)选择batch_size个样本作为一个批次返回,因此所选择的数据可能会重复
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
#测试数据的加载方式:每次从test_dataset中选择batch_size个不同的样本作为一个批次返回,要覆盖到所有测试样本
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)

#3、创建CNN模型

class Cnn(nn.Module):
    def __init__(self):
        super(Cnn, self).__init__()
        # 卷积层
        self.conv = nn.Sequential(
            nn.Conv2d(1, 25, 3),
            nn.BatchNorm2d(25),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2),

            nn.Conv2d(25, 50, 3),
            nn.BatchNorm2d(50),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2)
        )
        # 全连接层
        self.fc = nn.Sequential(
            nn.Linear(50*5*5, 1024),
            nn.ReLU(True),
            nn.Linear(1024, 128),
            nn.ReLU(True),
            nn.Linear(128, 10)
        )
    # 前向通道
    def forward(self, x):
        out = self.conv(x)
        out = out.view(x.size(0), -1)
        out = self.fc(out)
        return out

model = Cnn().to(device)

#4、训练模型

#定义损失:交叉熵损失
criterion = nn.CrossEntropyLoss()
#定义优化器:随梯度下降SGD
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
#开始训练
model.train()
epoch = 0
train_loss = 0
train_accuracy = 0
for epoch in range(num_epoches):
    for img, label in train_loader:
        if torch.cuda.is_available():
            img = img.cuda()
            label = label.cuda()
        else:
            img = Variable(img)
            label = Variable(label)
        #前向传播
        out = model(img)
        loss = criterion(out, label)
        optimizer.zero_grad()
        train_loss += loss.item()*label.size(0)
        _, pred = torch.max(out, 1)
        num_correct = (pred == label).sum()
        train_accuracy += num_correct.item()
        #向后传播
        loss.backward()
        optimizer.step()
    print('the {}th trainning completed!'.format(epoch+1))
print('Train Times: {}, train_loss: {:.4}, train_accuracy:{:.4f}%'.format(epoch+1, train_loss/len(train_dataset)/num_epoches, 100*train_accuracy/len(train_dataset)/num_epoches))

# 5、模型评估
# with torch.no_grad():

test_start = time.time()
test_loss = 0
test_accuracy = 0
for epoch, (img, label) in enumerate(test_loader, 0):
    if torch.cuda.is_available():
        img = img.cuda()
        label = label.cuda()
    else:
        img = Variable(img)
        label = Variable(label)
    out = model(img)
    loss = criterion(out, label)
    test_loss += loss.data.item()*label.size(0)
    _, pred = torch.max(out, 1)
    num_correct = (pred == label).sum()
    test_accuracy += num_correct.item()
print('Test times: {}, test_loss: {:.4f}, test_accuracy: {:.4f}%'.format(epoch+1, test_loss / (len(test_dataset)), 100*test_accuracy / (len(test_dataset))))

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值