一、库&数据集

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 高跟鞋
    PyTorch 案例 | fashion-MNIST(28*28像素图) - 图1
  1. import torch
  2. from torchvision import datasets, transforms
  3. #Transform for the data
  4. transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, ))])
  5. #training data
  6. trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data', download=True, train=True, transform=transform)
  7. trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
  8. #test data
  9. testset= datasets.FashionMNIST('~/.pytorch/F_MNIST_data', download=True, train=False, transform=transform)
  10. testloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

2. 目标

指标:训练损失低于某一阈值

二、模型的基本框架

1. 搭建网络结构

  1. #方法1)nn.Sequential()
  2. model = nn.Sequential(nn.Linear(784, 196)
  3. nn.ReLU(),
  4. nn.Linear(196, 49),
  5. nn.ReLU(),
  6. nn.Linear(49, 10))
  7. #方法2)封装成类(继承nn.Module)
  8. class Classifier(nn.Module):
  9. def __init__(self):
  10. super().__init__()
  11. self.fc1 = nn.Linear(784, 256)
  12. self.fc2 = nn.Linear(256, 128)
  13. self.fc3 = nn.Linear(128, 64)
  14. self.fc4 = nn.Linear(64, 10)
  15. def forward(self, x):
  16. x = x.view(x.shape[0], -1)
  17. x = F.relu(self.fc1(x))
  18. x = F.relu(self.fc2(x))
  19. x = F.relu(self.fc3(x))
  20. x = F.log_softmax(self.fc4(x), dim=1)
  21. return x

2. 训练+验证测试

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

PyTorch 案例 | fashion-MNIST(28*28像素图) - 图2

三、模型优化

1. 针对过拟合

  • 方法1)早期停止early-stopping@复杂度图
    比如8-10次迭代即可
  • 方法2)dropout
    随机关闭输入(针对中间隐藏层),防止一部分权值“拥兵自重”
  1. #dropout完整代码
  2. class Network(nn.Module):
  3. def __init__(self):
  4. super().__init__()
  5. self.fc1 = nn.Linear(784, 196)
  6. self.fc2 = nn.Linear(196, 49)
  7. self.fc3 = nn.Linear(49, 10)
  8. self.dropout = nn.Dropout(p=0.25)
  9. def forward(self, x):
  10. x = x.view(x.shape[0], -1)
  11. x = self.dropout(F.relu(self.fc1(x)))
  12. x = self.dropout(F.relu(self.fc2(x)))
  13. x = F.log_softmax(self.fc3(x), dim=1)
  14. return x
  15. #训练模型
  16. model = Network()
  17. criterion = nn.NLLLoss()
  18. optimizer = optim.Adam(model.parameters(), lr=0.002)
  19. epochs = 6
  20. train_losses, test_losses = [],[]
  21. for e in range(epochs):
  22. running_loss = 0
  23. for images, labels in trainloader:
  24. optimizer.zero_grad()
  25. log_ps = model(images)
  26. loss = criterion(log_ps, labels)
  27. loss.backward()
  28. optimizer.step()
  29. running_loss += loss.item()
  30. test_loss = 0
  31. accuracy = 0
  32. with torch.no_grad():
  33. model.eval() #设置dropout
  34. for images, labels in testloader:
  35. log_ps = model(images) #结果维度 64*10
  36. test_loss += criterion(log_ps, labels)
  37. ps = torch.exp(log_ps)
  38. tp_ps, tp_class = ps.topk(1, dim=1) #结果维度 64*1
  39. equals = (tp_class == labels.view(*tp_class.shape))
  40. accuracy += torch.mean(equals.type(torch.FloatTensor))
  41. train_losses.append(running_loss/len(trainloader))
  42. test_losses.append(test_loss/len(testloader))
  43. model.train()
  44. print("{}" .format())
  45. #验证测试
  46. %matplotlib inline
  47. %config InlineBackend.figure_format = 'retina'
  48. import helper #自定义模块
  49. model.eval() #测试环节,关闭dropout
  50. dataiter = iter(testloader)
  51. images, labels = dataiter.next()
  52. img = images[0]
  53. img = img.view(1, 784) #行向量
  54. with torch.no_grad():
  55. output = model(img)
  56. ps = torch.exp(output)
  57. #可视化
  58. helper.view_classify(img.view(1, 28, 28), ps, version='Fashion')

PyTorch 案例 | fashion-MNIST(28*28像素图) - 图3


References

nn.Module类详解