实验4 卷积神经网络【机器学习】

该博客文章展示了如何使用PyTorch搭建一个简单的卷积神经网络(CNN)模型来训练MNIST数据集。作者首先导入所需库,定义网络结构,设置训练参数,然后进行模型训练。在训练过程中,每600步打印一次损失值。训练完成后,模型在测试集上的准确率被计算并显示。最后,模型被保存以便后续使用。
摘要由CSDN通过智能技术生成

前言

仅供学习交流使用
请您阅读文章声明,默认同意该声明

代码 自己

import torch
import torchvision
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim

num_epochs = 6
batch_size = 100
learning_rate = 0.1
device = torch.device("cpu")  # 这代表将模型加载到指定设备上cpu

train_dataset = torchvision.datasets.MNIST('./data/', train=True, download=True,
                                           transform=torchvision.transforms.Compose([
                                               torchvision.transforms.ToTensor(),
                                               torchvision.transforms.Normalize(
                                                   (0.1307,), (0.3081,))
                                           ]))
test_dataset = torchvision.datasets.MNIST('./data/', train=False, download=True,
                                          transform=torchvision.transforms.Compose([
                                              torchvision.transforms.ToTensor(),
                                              torchvision.transforms.Normalize(
                                                  (0.1307,), (0.3081,))
                                          ]))

train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)


class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.layer1 = nn.Sequential(  # input shape(1,28,28)
            nn.Conv2d(in_channels=1,  # input height
                      out_channels=16,  # n_filter
                      kernel_size=3,  # filter_size
                      stride=1,  # filter_step
                      padding=1  # conv2d出来的图片不变
                      ),  # output_shape(16,28,28)
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)  # 2x2采样,output_shape(16,14,14)
        )
        self.layer2 = nn.Sequential(nn.Conv2d(16, 32, 3, 1, 1),
                                    nn.ReLU(),
                                    nn.MaxPool2d(2))  # output_size(32,7,7)
        self.out = nn.Linear(32 * 7 * 7, 10)

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = x.view(x.size(0), -1)  # flat_(batch_size,32x7x7)
        output = self.out(x)
        return output


model = CNN()

# 损失函数
criterion = nn.CrossEntropyLoss()
# 优化器选择
optimizer = optim.Adam(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = images.to(device)
        labels = labels.to(device)

        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, labels)

        # Backprpagation and optimization
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if i % 600 == 0:
            print("Epoch :{} \t Loss:{:.6f}".format(epoch, loss.item()))

def acc(labels, outputs):
    _, predicted = torch.max(outputs.data, 1)
    num = len(labels)
    right = (predicted == labels).sum().item()
    return num, right


with torch.no_grad():
    correct, total = 0, 0
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        num, right = acc(labels, outputs)
        correct = correct + right
        total = total + num
    print('Accuracy of the network on the 10000 test images:{}%'.format(100 * correct / total))

torch.save(model, 'model_total.ckpt')
# torch.save(net.state_dict(),'model_para.ckpt')

# -------------------------------------------------------------------
print("-------------------------导出数据------------------------------------------")
model = torch.load('model_total.ckpt')
'''
net=NeuralNet(input_size,[500,100],num_classes)
net.load_state_dict(torch.load('model_para.ckpt'))
'''

with torch.no_grad():
    correct, total = 0, 0
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        num, right = acc(labels, outputs)
        correct = correct + right
        total = total + num
    print('Accuracy of the network on the 10000 test images:{}%'.format(100 * correct / total))





结果

在这里插入图片描述

最后

请您阅读文章声明,默认同意该声明
打赏通道
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日星月云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值