Pytorch入门之MNIST项目

项目分为四个阶段

The project process

1.Prepare the data

2.Build the model

3.Train the model

4.Analyze the model's results

利用torchvision来下载MNIST数据集

import torch
import torchvision.transforms
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets
batch_size = 1000
train_loader = torch.utils.data.DataLoader(datasets.MNIST('../data', train=True, download=True, transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(),torchvision.transforms.Normalize((0.1207,), (0.3081,))])), batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(datasets.MNIST('../data', train=False, transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(), torchvision.transforms.Normalize((0.1307,), (0.3081,))])), batch_size=batch_size, shuffle=True)

下载后,建立神经网络并进行预测

在这里采用了三层神经网络,每一层的激活函数采用ReLU,损失函数采用交叉熵函数,具体细节详见代码

import torch
import torch.nn as nn
import torch.optim as optim  # 包含标准优化操作(如 SGD 和 Adam)的子包。
import torch.nn.functional as F  # 一个函数接口,包含用于构建神经网络(如损失函数和卷积)的典型操作。
import torchvision  # 提供对计算机视觉的常用数据集、模型体系结构和图像转换的访问的包。
from torch.utils.data import DataLoader
from torchvision import datasets

torch.set_printoptions(linewidth=120)  # 设置PyTorch打印语句的打印选项
# 加载数据集
batch_size = 1000
train_loader = torch.utils.data.DataLoader(
    datasets.MNIST(
        root='./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
)


# 采用一个三层的神经网络,激活函数均为ReLU,损失函数为多分类交叉熵函数
class Network(nn.Module):
    def __init__(self):
        super().__init__()
        # 三层的神经网络,输入的维度是784,第一次降维到200,第二次仍为200,第三次降维到10
        self.layer1 = nn.Linear(784, 200)
        self.layer2 = nn.Linear(200, 200)
        self.layer3 = nn.Linear(200, 10)

    def forward(self, t):
        t = self.layer1(t)
        t = F.relu(t)
        t = self.layer2(t)
        t = F.relu(t)
        t = self.layer3(t)
        return t


def get_num_correct(preds, labels):
    return preds.argmax(dim=1).eq(labels).sum().item()


network = Network()
optimizer = optim.Adam(network.parameters(), lr=0.01)

for epoch in range(10):
    total_loss = 0
    total_correct = 0

    for batch in train_loader:
        images, labels = batch
        images = images.view(1000, 784)
        preds = network(images)
        loss = F.cross_entropy(preds, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        total_loss += loss.item()
        total_correct += get_num_correct(preds, labels)

    correct = total_correct / 60000,
    print("epoch", epoch, "\t correct_rate:%.4f" % correct, "\t loss:%.4f" % total_loss)

训练10个epoch的效果如下

Pytorch入门之MNIST项目_第1张图片

 

你可能感兴趣的:(机器学习,pytorch,python,深度学习)