首頁 > 軟體

PyTorch手寫數位資料集進行多分類

2022-03-09 10:00:02

一、實現過程

本文對經典手寫數位資料集進行多分類,損失函數採用交叉熵,啟用函數採用ReLU,優化器採用帶有動量的mini-batchSGD演演算法。

所有程式碼如下:

0、導包

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

1、準備資料

batch_size = 64
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,),(0.3081,))
])

# 訓練集
train_dataset = datasets.MNIST(root='G:/datasets/mnist',train=True,download=False,transform=transform)
train_loader = DataLoader(train_dataset,shuffle=True,batch_size=batch_size)
# 測試集
test_dataset = datasets.MNIST(root='G:/datasets/mnist',train=False,download=False,transform=transform)
test_loader = DataLoader(test_dataset,shuffle=False,batch_size=batch_size)

2、設計模型

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.l1 = torch.nn.Linear(784, 512)
        self.l2 = torch.nn.Linear(512, 256)
        self.l3 = torch.nn.Linear(256, 128)
        self.l4 = torch.nn.Linear(128, 64)
        self.l5 = torch.nn.Linear(64, 10)

    def forward(self, x):
        x = x.view(-1, 784)
        x = F.relu(self.l1(x))
        x = F.relu(self.l2(x))
        x = F.relu(self.l3(x))
        x = F.relu(self.l4(x))
        return self.l5(x)
model = Net()
# 模型載入到GPU上
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)

3、構造損失函數和優化器

criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(),lr=0.01,momentum=0.5)

4、訓練和測試

def train(epoch):
    running_loss = 0.0
    for batch_idx, data in enumerate(train_loader, 0):
        inputs, target = data
        optimizer.zero_grad()

        # forward+backward+update
        outputs = model(inputs.to(device))
        loss = criterion(outputs, target.to(device))
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        if batch_idx % 300 == 299:
            print('[%d,%d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 300))
            running_loss = 0.0
def test():
    correct = 0
    total = 0
    with torch.no_grad():
        for data in test_loader:
            images, labels = data
            outputs = model(images.to(device))
            _, predicted = torch.max(outputs.data, dim=1)
            total += labels.size(0)
            correct += (predicted.cpu() == labels).sum().item()
    print('Accuracy on test set: %d %%' % (100 * correct / total))

for epoch in range(10):
    train(epoch)
    test()

執行結果如下:

[1,300] loss: 2.166
[1,600] loss: 0.797
[1,900] loss: 0.405
Accuracy on test set: 90 %
[2,300] loss: 0.303
[2,600] loss: 0.252
[2,900] loss: 0.218
Accuracy on test set: 94 %
[3,300] loss: 0.178
[3,600] loss: 0.168
[3,900] loss: 0.142
Accuracy on test set: 95 %
[4,300] loss: 0.129
[4,600] loss: 0.119
[4,900] loss: 0.110
Accuracy on test set: 96 %
[5,300] loss: 0.094
[5,600] loss: 0.092
[5,900] loss: 0.091
Accuracy on test set: 96 %
[6,300] loss: 0.077
[6,600] loss: 0.070
[6,900] loss: 0.075
Accuracy on test set: 97 %
[7,300] loss: 0.061
[7,600] loss: 0.058
[7,900] loss: 0.058
Accuracy on test set: 97 %
[8,300] loss: 0.043
[8,600] loss: 0.051
[8,900] loss: 0.050
Accuracy on test set: 97 %
[9,300] loss: 0.041
[9,600] loss: 0.038
[9,900] loss: 0.043
Accuracy on test set: 97 %
[10,300] loss: 0.030
[10,600] loss: 0.032
[10,900] loss: 0.033
Accuracy on test set: 97 %

二、參考文獻

  • [1] https://www.bilibili.com/video/BV1Y7411d7Ys?p=9

 到此這篇關於PyTorch手寫數位資料集進行多分類的文章就介紹到這了,更多相關python多分類內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com