当前位置:   article > 正文

Pytorch深度学习—— 自己设计模型训练MNIST(B站刘二大人P10作业)_自定义线性层 mnist

自定义线性层 mnist

作业内容:自己设计卷积核大小,池化层、以及线性层的参数,要求有三个卷积层,三个激活层、三个池化层以及三个线性层,用自己设计的卷积网络训练MNIST数据集。


选择下图结构的卷积神经网络来进行训练:

步骤:

  1. 选择 3 x 3 的卷积核,输入通道为 1,输出通道为 10:此时图像矩阵经过 3 x 3 的卷积核后会小1圈,也就是2个数位,变成 26 x 26,输出通道为10;
  2. 选择 2 x 2 的最大池化层:此时图像大小缩短一半,变成 13 x 13,通道数不变;
  3. 再次经过 2 x 2 的卷积核,输入通道为 10,输出通道为 20:此时图像再小1个数位,变成 12 * 12,输出通道为20;
  4. 再次经过 2 x 2 的最大池化层此时图像大小缩短一半,变成 6 x 6,通道数不变;
  5. 再次经过 3 x 3 的卷积核,输入通道为 20,输出通道为 30:此时图像再小2个数位,变成 4 * 4,输出通道为30;
  6. 再次经过 2 x 2 的最大池化层此时图像大小缩短一半,变成 2 x 2,通道数不变;
  7. 最后将图像整型变换成向量,输入到全连接层中:输入一共有 2 x 2 x 30 = 120 个元素,三层线性结构为(120,60),(60,30),(30,10)。

具体代码如下:

1 准备数据集

  1. batch_size = 64
  2. transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
  3. train_dataset = datasets.MNIST(root='../dataset/mnist', train=True, download=True, transform=transform)
  4. train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
  5. test_dataset = datasets.MNIST(root='../dataset/mnist', train=False, download=True, transform=transform)
  6. test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)

2 建立模型**

  1. class Net(torch.nn.Module):
  2. def __init__(self):
  3. super(Net, self).__init__()
  4. self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=3)
  5. self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=2)
  6. self.conv3 = torch.nn.Conv2d(20, 30, kernel_size=3)
  7. self.pooling = torch.nn.MaxPool2d(2)
  8. self.fc1 = torch.nn.Linear(120, 60)
  9. self.fc2 = torch.nn.Linear(60, 30)
  10. self.fc3 = torch.nn.Linear(30, 10)
  11. def forward(self, x):
  12. batch_size = x.size(0)
  13. x = F.relu(self.pooling(self.conv1(x)))
  14. x = F.relu(self.pooling(self.conv2(x)))
  15. x = F.relu(self.pooling(self.conv3(x)))
  16. x = x.view(batch_size, -1)
  17. x = self.fc1(x)
  18. x = self.fc2(x)
  19. x = self.fc3(x)
  20. return x
  21. model = Net()
  22. device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  23. model.to(device)

3 构造损失函数+优化器

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

4 训练+测试

  1. def train(epoch):
  2. running_loss=0.0
  3. for batch_idx,data in enumerate(train_loader,0):
  4. inputs,target=data
  5. inputs,target=inputs.to(device),target.to(device)
  6. optimizer.zero_grad()
  7. outputs=model(inputs)
  8. loss=criterion(outputs,target)
  9. loss.backward()
  10. optimizer.step()
  11. running_loss+=loss.item()
  12. if batch_idx%300==299:
  13. print('[%d,%.5d] loss:%.3f' % (epoch + 1, batch_idx + 1, running_loss / 2000))
  14. running_loss=0.0
  15. def test():
  16. correct=0
  17. total=0
  18. with torch.no_grad():
  19. for data in test_loader:
  20. inputs, target=data
  21. inputs,target=inputs.to(device),target.to(device)
  22. outputs=model(inputs)
  23. _,predicted=torch.max(outputs.data,dim=1)
  24. total+=target.size(0)
  25. correct+=(predicted==target).sum().item()
  26. print('Accuracy on test set:%d %% [%d%d]' %(100*correct/total,correct,total))
  27. if __name__=='__main__':
  28. for epoch in range(10):
  29. train(epoch)
  30. test()

5 完整代码+运行结果

  1. import torch
  2. from torchvision import transforms
  3. from torchvision import datasets
  4. from torch.utils.data import DataLoader
  5. import torch.nn.functional as F
  6. import torch.optim as optim
  7. batch_size = 64
  8. transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
  9. train_dataset = datasets.MNIST(root='../dataset/mnist', train=True, download=True, transform=transform)
  10. train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
  11. test_dataset = datasets.MNIST(root='../dataset/mnist', train=False, download=True, transform=transform)
  12. test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)
  13. class Net(torch.nn.Module):
  14. def __init__(self):
  15. super(Net, self).__init__()
  16. self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=3)
  17. self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=2)
  18. self.conv3 = torch.nn.Conv2d(20, 30, kernel_size=3)
  19. self.pooling = torch.nn.MaxPool2d(2)
  20. self.fc1 = torch.nn.Linear(120, 60)
  21. self.fc2 = torch.nn.Linear(60, 30)
  22. self.fc3 = torch.nn.Linear(30, 10)
  23. def forward(self, x):
  24. batch_size = x.size(0)
  25. x = F.relu(self.pooling(self.conv1(x)))
  26. x = F.relu(self.pooling(self.conv2(x)))
  27. x = F.relu(self.pooling(self.conv3(x)))
  28. x = x.view(batch_size, -1)
  29. x = self.fc1(x)
  30. x = self.fc2(x)
  31. x = self.fc3(x)
  32. return x
  33. model = Net()
  34. device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  35. model.to(device)
  36. criterion = torch.nn.CrossEntropyLoss()
  37. optimizer=optim.SGD(model.parameters(),lr=0.01,momentum=0.5)
  38. def train(epoch):
  39. running_loss=0.0
  40. for batch_idx,data in enumerate(train_loader,0):
  41. inputs,target=data
  42. inputs,target=inputs.to(device),target.to(device)
  43. optimizer.zero_grad()
  44. outputs=model(inputs)
  45. loss=criterion(outputs,target)
  46. loss.backward()
  47. optimizer.step()
  48. running_loss+=loss.item()
  49. if batch_idx%300==299:
  50. print('[%d,%.5d] loss:%.3f' % (epoch + 1, batch_idx + 1, running_loss / 2000))
  51. running_loss=0.0
  52. def test():
  53. correct=0
  54. total=0
  55. with torch.no_grad():
  56. for data in test_loader:
  57. inputs, target=data
  58. inputs,target=inputs.to(device),target.to(device)
  59. outputs=model(inputs)
  60. _,predicted=torch.max(outputs.data,dim=1)
  61. total+=target.size(0)
  62. correct+=(predicted==target).sum().item()
  63. print('Accuracy on test set:%d %% [%d%d]' %(100*correct/total,correct,total))
  64. if __name__=='__main__':
  65. for epoch in range(10):
  66. train(epoch)
  67. test()

运行结果如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/79198
推荐阅读
相关标签
  

闽ICP备14008679号