Free Will

深度学习系列(12):pytorch实现卷积神经网络

1.载入模块

1
2
3
4
5
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable

其中torchvision.transforms 用于数据预处理,torchvision.datasets加载内置数据集

2.设置参数

1
2
3
num_epochs = 5
batch_size = 100
learning_rate = 0.001

迭代次数num_epochs设置为5;批处理样本数batch_size设置为100;学习率learning_rate设置为0.001。

3.加载数据集

加载训练集,将MNIST数据集自动从网上下载并解压,train=true表示取出训练集部分,并变换为张量。

1
2
3
4
train_dataset = dsets.MNIST(root='../data/',
train=True,
transform=transforms.ToTensor(),
download=True)

加载测试集,train=False即表示取出测试集部分,并变换为张量。

1
2
3
test_dataset = dsets.MNIST(root='../data/',
train=False,
transform=transforms.ToTensor())

将训练集的60000张图片划分成600份,每份100张图,用于mini-batch输入。同时将测试集的10000张图片分成100份,每份100张图。shffule=True在表示不同批次的数据遍历时,打乱顺序,反之则不打乱顺序。

1
2
3
4
5
6
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)

4.CNN模型(两个卷积层)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, padding=2),#卷积:1 input image channel, 16 output channels, 5x5 square convolution kernel,2 zero padding)
nn.BatchNorm2d(16),#归一化
nn.ReLU(),#非线性激活函数ReLU
nn.MaxPool2d(2))#池化层
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2))
self.fc = nn.Linear(7*7*32, 10)#全连接层,in_features, out_features, bias=True
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
# 正常情况下, 我们都会用类进行封装一个网络
cnn = CNN()

5.损失函数与优化方法

1
2
criterion = nn.CrossEntropyLoss()#损失函数,这里为交叉熵
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)#优化方法,这里使用Adam

6.训练模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# wrap them in Variable
images = Variable(images)
labels = Variable(labels)
# Forward + Backward + Optimize
optimizer.zero_grad()
outputs = cnn(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
if (i+1) % 100 == 0:
print ('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
Epoch [1/5], Iter [100/600] Loss: 0.1363
Epoch [1/5], Iter [200/600] Loss: 0.0487
Epoch [1/5], Iter [300/600] Loss: 0.0688
Epoch [1/5], Iter [400/600] Loss: 0.1273
Epoch [1/5], Iter [500/600] Loss: 0.0283
Epoch [1/5], Iter [600/600] Loss: 0.0375
Epoch [2/5], Iter [100/600] Loss: 0.0398
Epoch [2/5], Iter [200/600] Loss: 0.0595
Epoch [2/5], Iter [300/600] Loss: 0.0793
Epoch [2/5], Iter [400/600] Loss: 0.0166
Epoch [2/5], Iter [500/600] Loss: 0.0235
Epoch [2/5], Iter [600/600] Loss: 0.0128
Epoch [3/5], Iter [100/600] Loss: 0.0273
Epoch [3/5], Iter [200/600] Loss: 0.0507
Epoch [3/5], Iter [300/600] Loss: 0.0384
Epoch [3/5], Iter [400/600] Loss: 0.0150
Epoch [3/5], Iter [500/600] Loss: 0.0086
Epoch [3/5], Iter [600/600] Loss: 0.0616
Epoch [4/5], Iter [100/600] Loss: 0.0243
Epoch [4/5], Iter [200/600] Loss: 0.0112
Epoch [4/5], Iter [300/600] Loss: 0.0391
Epoch [4/5], Iter [400/600] Loss: 0.0140
Epoch [4/5], Iter [500/600] Loss: 0.0324
Epoch [4/5], Iter [600/600] Loss: 0.0053
Epoch [5/5], Iter [100/600] Loss: 0.0358
Epoch [5/5], Iter [200/600] Loss: 0.0109
Epoch [5/5], Iter [300/600] Loss: 0.0066
Epoch [5/5], Iter [400/600] Loss: 0.0028
Epoch [5/5], Iter [500/600] Loss: 0.0380
Epoch [5/5], Iter [600/600] Loss: 0.0518

7.模型测试

1
2
3
4
5
6
7
8
9
10
11
cnn.eval()
correct = 0
total = 0
for images,labels in test_loader:
images = Variable(images)
outputs = cnn(images)
_,predicted = torch.max(outputs.data,1)
total += labels.size(0)
correct += (predicted == labels).sum()
print ('Test Accuracy of model on the 10000 test images:%d %%'%(100*correct/total))
Test Accuracy of model on the 10000 test images:99 %

8.保存模型

1
torch.save(cnn.state_dict(),'cnn.pkl')


应统联盟


连接十万名应统专业同学


阿药算法


打通算法面试任督二脉