[PyTorch]案例-框架入门 fashion-MNIST(28*28像素图)

一、库&数据集

  1. 数据集简介
  • 样本数:60,000
  • 样本(图片像素):28*28
  • 10个类别:
    0 T-shirt/top
    1 Trouser
    2 Pullover
    3 Dress 礼服
    4 Coat 外套
    5 Sandal 凉鞋
    6 Shirt 衬衫
    7 Sneaker 运动鞋
    8 Bag
    9 Ankle boot 高跟鞋


    样本

import torch
from torchvision import datasets, transforms
#Transform for the data
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, ))])
#training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
#test data
testset= datasets.FashionMNIST('~/.pytorch/F_MNIST_data', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
  1. 目标
    指标:训练损失低于某一阈值

二、模型的基本框架

  1. 搭建网络结构
#方法1)nn.Sequential()
model = nn.Sequential(nn.Linear(784, 196)
                      nn.ReLU(),
                      nn.Linear(196, 49),
                      nn.ReLU(),
                      nn.Linear(49, 10))
#方法2)封装成类(继承nn.Module)
class Classifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 256)
        self.fc2 = nn.Linear(256, 128)
        self.fc3 = nn.Linear(128, 64)
        self.fc4 = nn.Linear(64, 10)
    def forward(self, x):
        x = x.view(x.shape[0], -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = F.log_softmax(self.fc4(x), dim=1)
        return x
  1. 训练+验证测试
  1. 目标函数(损失函数)
    nn.CrossEntropyLoss()

  2. 优化器(权值更新)
    optim.SGD()
    optim.Adam()

#目标函数(损失函数)
criterion = nn.CrossEntropyLoss() #相当于 nn.LogSoftmax(dim=1) & nn.NLLLoss()
#优化器(权值更新)
optimizer = optim.SGD(model.parameters(), lr=0.03)
#模型训练
epoch = 5
for e in range(epoch):
    running_loss = 0
    for images, labels in trainloader:
        images = images.view(images.shape[0], -1)
        optimizer.zero_grad()
        logits = model(images)
        loss = criterion(logits, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    print("Training loss: {} \n" .format(running_loss/len(trainloader))) 
#验证测试
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import helper #自定义模块
dataiter = iter(testloader)
images, labels = dataiter.next()
img = images[0]
img = img.resize_(1, 784) #行向量
ps = F.softmax(model(img), dim=1)  #model 是nn.Sequantial()网络 缺少输出层
#可视化
helper.view_classify(img.resize_(1, 28, 28), ps, version='Fashion')


测试结果
https://www.jianshu.com/p/fbef0148b582

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论