【深度学习】

学习好几天,终于能自己敲出来CNN模型了

import torch.optim
from torchvision.datasets import MNIST
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision.transforms import Normalize, ToTensor, Compose

# 1.加载数据集
train_data = MNIST(root='./data', train=True, transform=ToTensor(), download=True)  # 将pil转化为tensor
test_data = MNIST(root='./data', train=False, transform=ToTensor())

# 2.数据加载
train_loader = DataLoader(train_data, batch_size=64, shuffle=True)
test_loader = DataLoader(test_data, batch_size=64, shuffle=True)


# 原始是1*28*28
# 构建模型
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(1, 16, 3, 1, 1),  # 28-3+2+1=28
            nn.ReLU(),
            nn.MaxPool2d(2)  # 14*14
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 3, 1, 2),  # 14-3+4+1=16
            nn.ReLU(),
            nn.MaxPool2d(2)  # 8*8
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(32, 64, 3, 1, 1),  # 8-3+2+1=8
            nn.ReLU(),
            nn.MaxPool2d(2)
        )
        self.linear = nn.Linear(4 * 4 * 64, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = x.view(x.size(0), -1)
        output = self.linear(x)
        return output


net = CNN()
optim = torch.optim.SGD(net.parameters(), lr=0.01)
loss_fn = nn.CrossEntropyLoss()


def accuracy(prediction, target):#定义一个准确度
    index = torch.max(prediction, 1)[1]#寻找索引在哪
    rightnumber = torch.eq(index, target).sum().item()
    return rightnumber, len(target)

epoches = 10
# 开始训练模型
for epoch in range(epoches):
    trainright = []
    for step, (data, target) in enumerate(train_loader):
        net.train()
        output = net(data)
        loss = loss_fn(output, target)
        trainright.append(accuracy(output, target))
        optim.zero_grad()
        loss.backward()
        optim.step()

        if step % 100 == 0:
            testright = []
            for (data, target) in test_loader:
                net.eval()
                output = net(data)
                loss = loss_fn(output, target)
                testright.append(accuracy(output, target))

            train_r = (sum([tup[0] for tup in trainright]), sum([tup[1] for tup in trainright]))
            val_r = (sum([tup[0] for tup in testright]), sum([tup[1] for tup in testright]))

            print('当前epoch:{} [{}/{} ({:.0f}%)]\t 损失:{:.6f}\t 训练集准确率: {:.2f}%\t 测试集准确率:{:.2f}%'.format(
                epoch, step * 64, len(train_loader.dataset),
                       100. * step / len(train_loader),
                loss.data,
                       100. * train_r[0] / train_r[1],
                       100. * val_r[0] / val_r[1]
            ))

总结

【深度学习】_第1张图片
最后效果98%,算是还可以啦。

你可能感兴趣的:(机器学习)