欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PyTorch一小时掌握之神经网络分类篇

程序员文章站 2022-03-25 23:40:38
目录概述对于 mnist 手写数据集的具体介绍, 我们在 tensorflow 中已经详细描述过, 在这里就不多赘述. 有兴趣的同学可以去看看之前的文章: 在上一节的内容里, 我们用 pytorch...

概述

对于 mnist 手写数据集的具体介绍, 我们在 tensorflow 中已经详细描述过, 在这里就不多赘述. 有兴趣的同学可以去看看之前的文章:

在上一节的内容里, 我们用 pytorch 实现了回归任务, 在这一节里, 我们将使用 pytorch 来解决分类任务.

导包

import torchvision
import torch
import torch.nn as nn
import torch.nn.functional as f
import torch.optim as optim
import matplotlib.pyplot as plt

设置超参数

# 设置超参数
n_epochs = 3
batch_size_train = 64
batch_size_test = 1000
learning_rate = 0.01
momentum = 0.5
log_interval = 10
random_seed = 1
torch.manual_seed(random_seed)

读取数据

# 数据读取
train_loader = torch.utils.data.dataloader(
    torchvision.datasets.mnist('./data/', train=true, download=true,
                               transform=torchvision.transforms.compose([
                                   torchvision.transforms.totensor(),
                                   torchvision.transforms.normalize(
                                       (0.1307,), (0.3081,))
                               ])),
    batch_size=batch_size_train, shuffle=true)
    
test_loader = torch.utils.data.dataloader(
    torchvision.datasets.mnist('./data/', train=false, download=true,
                               transform=torchvision.transforms.compose([
                                   torchvision.transforms.totensor(),
                                   torchvision.transforms.normalize(
                                       (0.1307,), (0.3081,))
                               ])),
    batch_size=batch_size_test, shuffle=true)

examples = enumerate(test_loader)
batch_idx, (example_data, example_targets) = next(examples)

# 调试输出
print(example_targets)
print(example_data.shape)

输出结果:
tensor([7, 6, 7, 5, 6, 7, 8, 1, 1, 2, 4, 1, 0, 8, 4, 4, 4, 9, 8, 1, 3, 3, 8, 6,
2, 7, 5, 1, 6, 5, 6, 2, 9, 2, 8, 4, 9, 4, 8, 6, 7, 7, 9, 8, 4, 9, 5, 3,
1, 0, 9, 1, 7, 3, 7, 0, 9, 2, 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 4, 4, 5,
8, 2, 3, 2, 4, 9, 1, 9, 6, 7, 1, 2, 1, 1, 2, 6, 9, 7, 1, 0, 1, 4, 2, 7,
7, 8, 3, 2, 8, 2, 7, 6, 1, 1, 9, 1, 0, 9, 1, 3, 9, 3, 7, 6, 5, 6, 2, 0,
0, 3, 9, 4, 7, 3, 2, 9, 0, 9, 5, 2, 2, 4, 1, 6, 3, 4, 0, 1, 6, 9, 1, 7,
0, 8, 0, 0, 9, 8, 5, 9, 4, 4, 7, 1, 9, 0, 0, 2, 4, 3, 5, 0, 4, 0, 1, 0,
5, 8, 1, 8, 3, 3, 2, 1, 2, 6, 8, 2, 5, 3, 7, 9, 3, 6, 2, 2, 6, 2, 7, 7,
6, 1, 8, 0, 3, 5, 7, 5, 0, 8, 6, 7, 2, 4, 1, 4, 3, 7, 7, 2, 9, 3, 5, 5,
9, 4, 8, 7, 6, 7, 4, 9, 2, 7, 7, 1, 0, 7, 2, 8, 0, 3, 5, 4, 5, 1, 5, 7,
6, 7, 3, 5, 3, 4, 5, 3, 4, 3, 2, 3, 1, 7, 4, 4, 8, 5, 5, 3, 2, 2, 9, 5,
8, 2, 0, 6, 0, 7, 9, 9, 6, 1, 6, 6, 2, 3, 7, 4, 7, 5, 2, 9, 4, 2, 9, 0,
8, 1, 7, 5, 5, 7, 0, 5, 2, 9, 5, 2, 3, 4, 6, 0, 0, 2, 9, 2, 0, 5, 4, 8,
9, 0, 9, 1, 3, 4, 1, 8, 0, 0, 4, 0, 8, 5, 9, 8])
torch.size([1000, 1, 28, 28])

可视化展示

# 画图 (前6个)
fig = plt.figure()
for i in range(6):
    plt.subplot(2, 3, i + 1)
    plt.tight_layout()
    plt.imshow(example_data[i][0], cmap='gray', interpolation='none')
    plt.title("ground truth: {}".format(example_targets[i]))
    plt.xticks([])
    plt.yticks([])
plt.show()

输出结果:

PyTorch一小时掌握之神经网络分类篇

建立模型

# 创建model
class net(nn.module):
    def __init__(self):
        super(net, self).__init__()
        self.conv1 = nn.conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.dropout2d()
        self.fc1 = nn.linear(320, 50)
        self.fc2 = nn.linear(50, 10)

    def forward(self, x):
        x = f.relu(f.max_pool2d(self.conv1(x), 2))
        x = f.relu(f.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = f.relu(self.fc1(x))
        x = f.dropout(x, training=self.training)
        x = self.fc2(x)
        return f.log_softmax(x)


network = net()
optimizer = optim.sgd(network.parameters(), lr=learning_rate,
                      momentum=momentum)

训练模型

# 训练
train_losses = []
train_counter = []
test_losses = []
test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)]


def train(epoch):
    network.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        optimizer.zero_grad()
        output = network(data)
        loss = f.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        if batch_idx % log_interval == 0:
            print('train epoch: {} [{}/{} ({:.0f}%)]\tloss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                       100. * batch_idx / len(train_loader), loss.item()))
            train_losses.append(loss.item())
            train_counter.append(
                (batch_idx * 64) + ((epoch - 1) * len(train_loader.dataset)))
            torch.save(network.state_dict(), './model.pth')
            torch.save(optimizer.state_dict(), './optimizer.pth')


def test():
    network.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            output = network(data)
            test_loss += f.nll_loss(output, target, size_average=false).item()
            pred = output.data.max(1, keepdim=true)[1]
            correct += pred.eq(target.data.view_as(pred)).sum()
    test_loss /= len(test_loader.dataset)
    test_losses.append(test_loss)
    print('\ntest set: avg. loss: {:.4f}, accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))


for epoch in range(1, n_epochs + 1):
    train(epoch)
    test()

输出结果:
train epoch: 1 [0/60000 (0%)] loss: 2.297471
train epoch: 1 [6400/60000 (11%)] loss: 1.934886
train epoch: 1 [12800/60000 (21%)] loss: 1.242982
train epoch: 1 [19200/60000 (32%)] loss: 0.979296
train epoch: 1 [25600/60000 (43%)] loss: 1.277279
train epoch: 1 [32000/60000 (53%)] loss: 0.721533
train epoch: 1 [38400/60000 (64%)] loss: 0.759595
train epoch: 1 [44800/60000 (75%)] loss: 0.469635
train epoch: 1 [51200/60000 (85%)] loss: 0.422614
train epoch: 1 [57600/60000 (96%)] loss: 0.417603

test set: avg. loss: 0.1988, accuracy: 9431/10000 (94%)

train epoch: 2 [0/60000 (0%)] loss: 0.277207
train epoch: 2 [6400/60000 (11%)] loss: 0.328862
train epoch: 2 [12800/60000 (21%)] loss: 0.396312
train epoch: 2 [19200/60000 (32%)] loss: 0.301772
train epoch: 2 [25600/60000 (43%)] loss: 0.253600
train epoch: 2 [32000/60000 (53%)] loss: 0.217821
train epoch: 2 [38400/60000 (64%)] loss: 0.395815
train epoch: 2 [44800/60000 (75%)] loss: 0.265737
train epoch: 2 [51200/60000 (85%)] loss: 0.323627
train epoch: 2 [57600/60000 (96%)] loss: 0.236692

test set: avg. loss: 0.1233, accuracy: 9622/10000 (96%)

train epoch: 3 [0/60000 (0%)] loss: 0.500148
train epoch: 3 [6400/60000 (11%)] loss: 0.338118
train epoch: 3 [12800/60000 (21%)] loss: 0.452308
train epoch: 3 [19200/60000 (32%)] loss: 0.374940
train epoch: 3 [25600/60000 (43%)] loss: 0.323300
train epoch: 3 [32000/60000 (53%)] loss: 0.203830
train epoch: 3 [38400/60000 (64%)] loss: 0.379557
train epoch: 3 [44800/60000 (75%)] loss: 0.334822
train epoch: 3 [51200/60000 (85%)] loss: 0.361676
train epoch: 3 [57600/60000 (96%)] loss: 0.218833

test set: avg. loss: 0.0911, accuracy: 9723/10000 (97%)

完整代码

import torchvision
import torch
import torch.nn as nn
import torch.nn.functional as f
import torch.optim as optim
import matplotlib.pyplot as plt

# 设置超参数
n_epochs = 3
batch_size_train = 64
batch_size_test = 1000
learning_rate = 0.01
momentum = 0.5
log_interval = 100
random_seed = 1
torch.manual_seed(random_seed)

# 数据读取
train_loader = torch.utils.data.dataloader(
    torchvision.datasets.mnist('./data/', train=true, download=true,
                               transform=torchvision.transforms.compose([
                                   torchvision.transforms.totensor(),
                                   torchvision.transforms.normalize(
                                       (0.1307,), (0.3081,))
                               ])),
    batch_size=batch_size_train, shuffle=true)

test_loader = torch.utils.data.dataloader(
    torchvision.datasets.mnist('./data/', train=false, download=true,
                               transform=torchvision.transforms.compose([
                                   torchvision.transforms.totensor(),
                                   torchvision.transforms.normalize(
                                       (0.1307,), (0.3081,))
                               ])),
    batch_size=batch_size_test, shuffle=true)

examples = enumerate(test_loader)
batch_idx, (example_data, example_targets) = next(examples)

# 调试输出
print(example_targets)
print(example_data.shape)

# 画图 (前6个)
fig = plt.figure()
for i in range(6):
    plt.subplot(2, 3, i + 1)
    plt.tight_layout()
    plt.imshow(example_data[i][0], cmap='gray', interpolation='none')
    plt.title("ground truth: {}".format(example_targets[i]))
    plt.xticks([])
    plt.yticks([])
plt.show()


# 创建model
class net(nn.module):
    def __init__(self):
        super(net, self).__init__()
        self.conv1 = nn.conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.dropout2d()
        self.fc1 = nn.linear(320, 50)
        self.fc2 = nn.linear(50, 10)

    def forward(self, x):
        x = f.relu(f.max_pool2d(self.conv1(x), 2))
        x = f.relu(f.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = f.relu(self.fc1(x))
        x = f.dropout(x, training=self.training)
        x = self.fc2(x)
        return f.log_softmax(x)


network = net()
optimizer = optim.sgd(network.parameters(), lr=learning_rate,
                      momentum=momentum)

# 训练
train_losses = []
train_counter = []
test_losses = []
test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)]


def train(epoch):
    network.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        optimizer.zero_grad()
        output = network(data)
        loss = f.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        if batch_idx % log_interval == 0:
            print('train epoch: {} [{}/{} ({:.0f}%)]\tloss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                       100. * batch_idx / len(train_loader), loss.item()))
            train_losses.append(loss.item())
            train_counter.append(
                (batch_idx * 64) + ((epoch - 1) * len(train_loader.dataset)))
            torch.save(network.state_dict(), './model.pth')
            torch.save(optimizer.state_dict(), './optimizer.pth')


def test():
    network.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            output = network(data)
            test_loss += f.nll_loss(output, target, size_average=false).item()
            pred = output.data.max(1, keepdim=true)[1]
            correct += pred.eq(target.data.view_as(pred)).sum()
    test_loss /= len(test_loader.dataset)
    test_losses.append(test_loss)
    print('\ntest set: avg. loss: {:.4f}, accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))


for epoch in range(1, n_epochs + 1):
    train(epoch)
    test()

到此这篇关于pytorch一小时掌握之神经网络分类篇的文章就介绍到这了,更多相关pytorch神经网络分类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!