PyTorch深度学习实践(10. Basic CNN-4)

源自课程:《PyTorch深度学习实践》完结合集

Chapter10 卷积神经网络(基础篇)

import torch
import torch.nn.functional as F
from torch.optim import optimizer
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms


# 搭建卷积神经网络
class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5)
        self.pooling = torch.nn.MaxPool2d(2)
        self.fc = torch.nn.Linear(320, 10)

    def forward(self, x):
        batch_size = x.size(0)
        x = F.relu(self.pooling(self.conv1(x)))
        x = F.relu(self.pooling(self.conv2(x)))
        x = x.view(batch_size, -1)
        x = self.fc(x)
        return x


# 实例化模型,如果需要迁移到GPU上,则要使用to(device)语句
# 一共要将"模型  训练数据  测试数据"转换到GPU上
model = Net()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)

# 设置batch_size大小,以及使用transform对样本数据进行标准化
batch_size = 64
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
])

# 加载下载公开数据集的训练、测试数据(主要区别是Train=True/False)
# 注意:创建Dataset类的子类用于读取数据,而datasets则是加载下载公开数据集
train_dataset = datasets.MNIST(root="New_program//MNIST",
                               train=True,
                               download=True,
                               transform=transform)

# 训练集中的数据一般shuffle=True,而测试集中为False
train_loader = DataLoader(train_dataset,
                          shuffle=True,
                          batch_size=batch_size,
                          num_workers=2)

test_dataset = datasets.MNIST(root="New_program//MNIST",
                              train=False,
                              download=True,
                              transform=transform)

test_loader = DataLoader(test_dataset,
                         shuffle=False,
                         batch_size=batch_size,
                         num_workers=2)


# 构建损失函数和优化器,此处使用的是交叉熵损失,因此网络最后一层无需激活函数
criterion = torch.nn.CrossEntropyLoss()
# SGD带冲量,旨在帮助避免陷入局部最优点
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.5)


# 开始进行迭代训练
def train(epoch):
    running_loss = 0
    for batch_idx, data in enumerate(train_loader, 0):
        # 正向传播、计算损失
        inputs, target = data
        inputs, target = inputs.to(device), target.to(device)
        outputs = model(inputs)
        loss = criterion(outputs, target)

        # 梯度清零、反向传播、参数更新
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        running_loss = running_loss + loss.item()

        # 以300次作为一个iteration
        if batch_idx % 300 == 299:
            print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 300))
            running_loss = 0.0


# 进行模型测试
def test():
    correct = 0
    total = 0
    # 测试时无需对梯度进行跟踪
    with torch.no_grad():
        # 测试时不需要batch_idx,单个样本测试即可
        for data in test_loader:
            images, labels = data
            images, labels = images.to(device), labels.to(device)
            outputs = model(images)
            _, predicted = torch.max(outputs.data, dim=1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
            print('Accuracy on test set: %d %%' % (100 * correct / total))


if __name__ == '__main__':
    for epoch in range(10):
        train(epoch)
        test()

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