【PyTorch】多层感知机-训练Fashion-MNIST

使用fashion-MNIST演示PyTorch实现多层感知机的创建、训练和测试

导入依赖包

import torch
import torch.utils.data as Data
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
from torch.nn import init
import sys

加载数据集

mnist_train = torchvision.datasets.FashionMNIST(root='~/Datasets/FashionMNIST',train=True, download=True, transform=transforms.ToTensor())
mnist_test = torchvision.datasets.FashionMNIST(root='~/Datasets/FashionMNIST',train=False, download=True, transform=transforms.ToTensor())

batch_size = 128
if sys.platform.startswith('win'):
    num_workers = 0
else:
    num_workers = 4
    
train_iter = Data.DataLoader(mnist_train,batch_size=batch_size,shuffle=True,num_workers=num_workers)
test_iter = Data.DataLoader(mnist_test,batch_size=batch_size,shuffle=False,num_workers=num_workers)

创建模型

class FlattenLayer(nn.Module):
    def __init__(self):
        super(FlattenLayer,self).__init__()
    def forward(self,x):
        return x.view(x.shape[0],-1)

num_inputs = 28*28
num_outputs = 10
num_hiddens = 512
drop_prob1 = 0.2   
net = nn.Sequential(
        FlattenLayer(),
        nn.Linear(num_inputs,num_hiddens),
        nn.ReLU(),
        nn.Dropout(drop_prob1),
        nn.Linear(num_hiddens,num_outputs),    
        )
for params in net.parameters():
    init.normal_(params, mean=0, std=0.01)
print(net)

Sequential(
(0): FlattenLayer()
(1): Linear(in_features=784, out_features=512, bias=True)
(2): ReLU()
(3): Dropout(p=0.2, inplace=False)
(4): Linear(in_features=512, out_features=10, bias=True)
)


模型训练

num_epochs = 10
step = 0
loss = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(),lr=0.05)
for epoch in range(1,num_epochs+1):
    for x,y in train_iter:
        out = net(x)
        l = loss(out,y)
        optimizer.zero_grad()
        l.backward()
        optimizer.step()
        
        step += 1
        if step % 100 == 0:
            print("Epoch:{},Step:{},Loss:{}".format(epoch,step,l.item()))

Epoch:1,Step:100,Loss:1.1866531372070312
Epoch:1,Step:200,Loss:0.7550700902938843
Epoch:2,Step:300,Loss:0.6766654849052429
Epoch:2,Step:400,Loss:0.6058944463729858
Epoch:3,Step:500,Loss:0.5482058525085449

Epoch:9,Step:2100,Loss:0.541287899017334
Epoch:10,Step:2200,Loss:0.3729313910007477
Epoch:10,Step:2300,Loss:0.43224409222602844


模型测试

def evaluate_accuracy(data_iter,net):
    net.eval()
    acc_sum,n = 0.,0
    for x,y in data_iter:
        acc_sum += (net(x).argmax(dim=1)==y).float().sum().item()
        n += y.shape[0]
    return acc_sum/n

print(evaluate_accuracy(test_iter,net))

0.8315


你可能感兴趣的:(PyTorch,python,开发,pytorch,神经网络,深度学习)