【深度学习】分类问题代码实战之初始手写数据集

发布于:2024-12-06 ⋅ 阅读:(24) ⋅ 点赞:(0)

        手写数据集分类问题通常是指通过机器学习模型对手写数字进行分类。最著名的手写数字数据集是 MNIST(Modified National Institute of Standards and Technology) 数据集,它包含了大量的手写数字图像,广泛用于图像分类和机器学习的研究与教学。在手写数据集分类问题中,目标是将手写数字图像(通常是 28x28 像素的灰度图像)映射到对应的数字标签(0 到 9)。例如,如果输入的图像是数字“3”,模型的目标就是预测该图像是数字“3”。

utils.py:

import torch
from matplotlib import pyplot as plt
def plot_curve(data):
    fig = plt.figure()
    plt.plot(range(len(data), data, color='blue'))
    plt.legend(['value'],loc='upper right')
    plt.xlabel('step')
    plt.ylabel('value')
    plt.show()

def plot_image(img, label, name):
    fig = plt.figure()
    for i in range(6):
        plt.subplot(2, 3, i + 1)
        plt.tight_layout()
        plt.imshow(img[i][0] * 0.3081 + 0.1307, cmap='gray', interpolation = 'none')
        plt.title("{}:{}".format(name, label[i].item()))
        plt.xticks([])
        plt.yticks([])
    plt.show()

def one_hot(label, depth=10):
    out = torch.zeros(label.size(0), depth)
    idx = torch.LongTensor(label).view(-1, 1)
    out.scatter_(dim = 1, index = idx, value = 1)
    return out

mnist_train.py:

# 导入问题所需要的关键包
import torch
from torch import nn
from torch.nn import functional as F
from torch import optim
import torchvision
from matplotlib import pyplot as plt
from utils import plot_image, plot_curve, one_hot

# step 1.load dataset加载数据集
train_loader = torch.utils.data.DataLoader(torchvision.datasets.MNIST('mnist data', train=True, download=True, transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(), torchvision.transforms.Normalize((0.1307,),(0.3081,))])), batch_size=batch_size, shuffle=True)

test_loader = torch.utils.data.DataLoader(torchvision.datasets.MNIST('mnist data/', train=False, download=True, transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(), torchvision.transforms.Normalize((0.1307,),(0.3081,))])), batch_size=batch_size, shuffle=False)

x, y = next(iter(train_loader))
print(x.shape, y.shape, x.min(), x.max())
plot_image(x, y, 'image sample')

# 构建网络模型
class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        
        self.fc1 = nn.Linear(28 * 28, 256)
        self.fc2 = nn.Linear(256, 64)
        self.fc3 = nn.Linear(64, 10)

     def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
       
        return x

# 进行网络的初始化
net = Net()
# 定义优化器
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
# 初始化数组存储loss数值便于绘图
train_loss =[] 

# 对数据集迭代
for epoch in range(3):
    # 对batch迭代
    for batch_idx, (x, y) in enumerate(train_loader):
        # x:[b,1,28,28],y:[512]
        # [b, 784]
        x = x.view(x.size(0), 28*28)
        # [b, 10]
        out = net(x)
        y_onehot = one_hot(y)
        # loss=mse(out, y_onehot) 计算loss数值
        loss = F.mse_loss(out, y_onehot)
        # 将梯度置零操作
        optimizer.zero_grad()
        # 反向传播
        loss.backward()
        # 更新权重值
        optimizer.step()
        # 累加loss值
        train_loss.append(loss.item())
        # 每十个batch进行loss值打印
        if batch_idx % 10 ==0:
            print(epoch, batch_idx, loss.item())

# 绘制loss曲线
plot_curve(train_loss)

# 进行测试
total_correct = 0
for x, y in test_loader:
    x = x.view(x.size(0), 28*28)
    out = net(x)
    # out[b, 10], pred[b]
    pred = out.argmax(dim=1)
    correct = pred.eq(y).sum().float()
    totol_correct += correct

total_num = len(test_loader.dataset)
acc = total_correct / total_num
print('test acc:', acc)

# 进行样例打印
x, y = next(iter(test_loader))
out = net(x.view(x.size(0), 28*28))
pred = out.argmax(dim=1)
plot_image(x, pred, 'test')

网站公告

今日签到

点亮在社区的每一天
去签到