简单回顾一下PyTorch深度学习实践概论笔记10-卷积神经网络基础篇的练习题。如下图所示:
• Try a more complex CNN:(尝试更复杂的CNN)
• Conv2d Layer *3
• ReLU Layer * 3
• MaxPooling Layer * 3
• Linear Layer * 3
• Try different configuration of this CNN:(尝试不同的CNN配置)
• Compare their performance.
老师给的课后练习中建议的CNN包含3个卷积层、3个ReLU激活层、3个池化层和3个线性层,我自己尝试去构造了一下,没有成功。然后我就去掉了一个池化层,构造了一个包含3个卷积层(3*3卷积层)、3个ReLU激活层、2个池化层(最大池化层)和3个线性层的CNN。
输入维度 | 输出维度 | 计算过程 | |
conv1 | (64,1,28,28) | (64,16,26,26) | 28-3+1=26 |
relu | (64,16,26,26) | (64,16,26,26) | 不变 |
conv2 | (64,16,26,26) | (64,32,24,24) | 26-3+1=24 |
relu | (64,32,24,24) | (64,32,24,24) | 不变 |
pooling | (64,32,24,24) | (64,32,12,12) | 24/2=12 |
conv3 | (64,32,12,12) | (64,64,10,10) | 12-3+1=10 |
relu | (64,64,10,10) | (64,64,10,10) | 不变 |
pooling | (64,64,10,10) | (64,64,5,5) | 10/2=5 |
fc1 | (64,1600) | (64,512) | 64*5*5=1600 |
fc2 | (64,512) | (64,100) | |
fc2 | (64,100) | (64,10) |
【小建议】在自己搭建模型时,最好在forward函数中打印x.shape,有助于之后解决bug!
具体设计模型的代码如下:
#2.设计模型
class ExNet(torch.nn.Module):
def __init__(self):
super(ExNet, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 16, kernel_size=3)
self.conv2 = torch.nn.Conv2d(16, 32, kernel_size=3)
self.conv3 = torch.nn.Conv2d(32, 64, kernel_size=3)
self.pooling = torch.nn.MaxPool2d(2)
self.fc1 = torch.nn.Linear(1600, 512)
self.fc2 = torch.nn.Linear(512, 64)
self.fc3 = torch.nn.Linear(64, 10)
def forward(self, x):
batch_size = x.size(0)
# x = x.view(-1,1*28*28)
x = F.relu(self.conv1(x))
print(x.shape)
x = self.pooling(F.relu(self.conv2(x)))
print(x.shape)
x = self.pooling(F.relu(self.conv3(x)))
print(x.shape)
#方法一:
# x = x.view(-1,64*4*4) # flatten
#方法二:
x = x.view(batch_size,-1)
print(x.shape)
x = F.relu(self.fc1(x))
print(x.shape)
x = F.relu(self.fc2(x))
print(x.shape)
x = self.fc3(x)
print(x.shape)
return x
modele = ExNet()
print(modele)
输出模型结果如下:
ExNet(
(conv1): Conv2d(1, 16, kernel_size=(3, 3), stride=(1, 1))
(conv2): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1))
(conv3): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))
(pooling): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(fc1): Linear(in_features=1600, out_features=512, bias=True)
(fc2): Linear(in_features=512, out_features=64, bias=True)
(fc3): Linear(in_features=64, out_features=10, bias=True)
)
其他部分的代码和本节内容类似,最后测试集的准确率为99%,和之前9%相比,准确率大大提高。
完整的代码如下(可跑通):
#0.导库
import torch
#构建DataLoader的库
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
#使用函数relu()的库
import torch.nn.functional as F
#构建优化器的库
import torch.optim as optim#1.准备数据集
# batch_size = 64
transform = transforms.Compose([
transforms.ToTensor(), #将PIL图像转化成Tensor
transforms.Normalize((0.1307, ), (0.3081, )) #正则化,归一化,0.1307是均值,0.3081是标准差,这两个值是根据所有数据集算出来的
])
train_dataset = datasets.MNIST(root='./data/',
train=True,
download=False,
transform=transform)
train_loader = DataLoader(train_dataset,
shuffle=True,
batch_size=64)
test_dataset = datasets.MNIST(root='./data/',
train=False,
download=False,
transform=transform)
test_loader = DataLoader(test_dataset,
shuffle=False,
batch_size=64)
#2.设计模型
class ExNet(torch.nn.Module):
def __init__(self):
super(ExNet, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 16, kernel_size=3)
self.conv2 = torch.nn.Conv2d(16, 32, kernel_size=3)
self.conv3 = torch.nn.Conv2d(32, 64, kernel_size=3)
self.pooling = torch.nn.MaxPool2d(2)
self.fc1 = torch.nn.Linear(1600, 512)
self.fc2 = torch.nn.Linear(512, 64)
self.fc3 = torch.nn.Linear(64, 10)
def forward(self, x):
batch_size = x.size(0)
x = F.relu(self.conv1(x))
x = self.pooling(F.relu(self.conv2(x)))
x = self.pooling(F.relu(self.conv3(x)))
x = x.view(batch_size,-1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
modele = ExNet()
# print(modele)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#把整个模型的参数,缓存,所有的模块都放到cuda里面,转成cuda tensor
modele.to(device)
#3.构造损失和优化器
criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(modele.parameters(), lr=0.01, momentum=0.5)
#4.训练代码与测试代码
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
#print("inputs.shape",inputs.shape)#torch.Size([64])
#print("target.shape",target.shape)#torch.Size([64, 1, 28, 28])
#加入下面这行,把每一步的inputs和targets迁移到GPU
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad()
# forward + backward + update
outputs = modele(inputs)
#print("inputs.shape",inputs.shape)#torch.Size([64, 1, 28, 28])
#print("outputs.shape",outputs.shape)#torch.Size([100, 10])
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
if batch_idx % 300 == 299:
print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 2000))
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
inputs, target = data
#加入下面这行,把每一步的inputs和targets迁移到GPU
inputs, target = inputs.to(device), target.to(device)
outputs = modele(inputs)
_, predicted = torch.max(outputs.data, dim=1)
total += target.size(0)
correct += (predicted == target).sum().item()
print('Accuracy on test set: %d %% [%d/%d]' % (100 * correct / total, correct, total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
运行结果如下:
[1, 300] loss: 0.207
[1, 600] loss: 0.044
[1, 900] loss: 0.026
Accuracy on test set: 96 % [9655/10000]
[2, 300] loss: 0.017
[2, 600] loss: 0.015
[2, 900] loss: 0.013
Accuracy on test set: 97 % [9786/10000]
[3, 300] loss: 0.010
[3, 600] loss: 0.011
[3, 900] loss: 0.009
Accuracy on test set: 98 % [9850/10000]
[4, 300] loss: 0.008
[4, 600] loss: 0.008
[4, 900] loss: 0.008
Accuracy on test set: 98 % [9840/10000]
[5, 300] loss: 0.006
[5, 600] loss: 0.006
[5, 900] loss: 0.006
Accuracy on test set: 98 % [9866/10000]
[6, 300] loss: 0.005
[6, 600] loss: 0.005
[6, 900] loss: 0.005
Accuracy on test set: 98 % [9883/10000]
[7, 300] loss: 0.005
[7, 600] loss: 0.004
[7, 900] loss: 0.004
Accuracy on test set: 98 % [9870/10000]
[8, 300] loss: 0.003
[8, 600] loss: 0.004
[8, 900] loss: 0.004
Accuracy on test set: 99 % [9905/10000]
[9, 300] loss: 0.003
[9, 600] loss: 0.003
[9, 900] loss: 0.003
Accuracy on test set: 98 % [9894/10000]
[10, 300] loss: 0.003
[10, 600] loss: 0.002
[10, 900] loss: 0.002
Accuracy on test set: 98 % [9874/10000]
可以看到输出结果最后的一次test准确率有99%,效果不错。
说明:记录学习笔记,如果错误欢迎指正!写文章不易,转载请联系我。