• Pytorch intermediate(一) CNN


    使用pytorch实现了2层卷积神经网络,包含有batchnorm层。

    在测试阶段需要model.eval(),使用移动平均值和方差代替训练过程中的均值和方差。

    1. import torch
    2. import torch.nn as nn
    3. import torchvision
    4. import torchvision.transforms as transforms
    5. # Device configuration
    6. device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    7. # Hyper parameters
    8. num_epochs = 5
    9. num_classes = 10
    10. batch_size = 100
    11. learning_rate = 0.001
    12. # MNIST dataset
    13. train_dataset = torchvision.datasets.MNIST(root='../../data/',
    14. train=True,
    15. transform=transforms.ToTensor(),
    16. download=True)
    17. test_dataset = torchvision.datasets.MNIST(root='../../data/',
    18. train=False,
    19. transform=transforms.ToTensor())
    20. # Data loader
    21. train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
    22. batch_size=batch_size,
    23. shuffle=True)
    24. test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
    25. batch_size=batch_size,
    26. shuffle=False)
    27. # Convolutional neural network (two convolutional layers)
    28. class ConvNet(nn.Module):
    29. def __init__(self, num_classes=10):
    30. super(ConvNet, self).__init__()
    31. self.layer1 = nn.Sequential(
    32. nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
    33. nn.BatchNorm2d(16),
    34. nn.ReLU(),
    35. nn.MaxPool2d(kernel_size=2, stride=2))
    36. self.layer2 = nn.Sequential(
    37. nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
    38. nn.BatchNorm2d(32),
    39. nn.ReLU(),
    40. nn.MaxPool2d(kernel_size=2, stride=2))
    41. self.fc = nn.Linear(7*7*32, num_classes)
    42. def forward(self, x):
    43. out = self.layer1(x)
    44. out = self.layer2(out)
    45. out = out.reshape(out.size(0), -1)
    46. out = self.fc(out)
    47. return out
    48. model = ConvNet(num_classes).to(device)
    49. # Loss and optimizer
    50. criterion = nn.CrossEntropyLoss()
    51. optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    52. # Train the model
    53. total_step = len(train_loader)
    54. for epoch in range(num_epochs):
    55. for i, (images, labels) in enumerate(train_loader):
    56. images = images.to(device)
    57. labels = labels.to(device)
    58. # Forward pass
    59. outputs = model(images)
    60. loss = criterion(outputs, labels)
    61. # Backward and optimize
    62. optimizer.zero_grad()
    63. loss.backward()
    64. optimizer.step()
    65. if (i+1) % 100 == 0:
    66. print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
    67. .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
    68. # Test the model
    69. model.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
    70. with torch.no_grad():
    71. correct = 0
    72. total = 0
    73. for images, labels in test_loader:
    74. images = images.to(device)
    75. labels = labels.to(device)
    76. outputs = model(images)
    77. _, predicted = torch.max(outputs.data, 1)
    78. total += labels.size(0)
    79. correct += (predicted == labels).sum().item()
    80. print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
    81. # Save the model checkpoint
    82. torch.save(model.state_dict(), 'model.ckpt')

  • 相关阅读:
    Java基础之正则表达式
    ​LeetCode解法汇总2300. 咒语和药水的成功对数
    谈一谈程序化交易接口的优势是什么?
    创建 NFT 市场需要多少钱:解释与实践
    【左程云算法全讲13】暴力递归
    长春理工大学2013年全国硕士研究生统一入学考试自命题试题
    E. Split Into Two Sets(染色法判断二分图)
    前端测试工具 SwitchHosts + Charles + Proxy SwitchyOmega
    部署LAMP平台
    多多练习,扎实基本功
  • 原文地址:https://blog.csdn.net/qq_41828351/article/details/90740750