第10讲 CNN Model传送门:Basic CNN Model
作业如下:
根据讲解视频,自己编写了一个网络,结构如下:
3个CONV层,3个激活层(RELU),3个Pooling层,3个FC 层
实现代码如下:
# -*- coding: UTF-8 -*-
'''===============================================
@Author :kidding
@Date :2021/2/8 14:19
@File :Basic_CNN_Exercise
@IDE :PyCharm
=================================================='''
import torch
import numpy as np
from torchvision import transforms
from torchvision import datasets
import torch.nn.functional as F
from torch.utils.data import DataLoader,Dataset
import matplotlib.pyplot as plt
'''
1、PrePare Dataset
'''
batch_size = 32
#transforms.ToTensor() 将一个PIL图像 转变为一个Tensor,像素值为[0,1]
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))
])
train_dataset = datasets.MNIST(root='./dataset/mnist',train=True,download=False,transform=transform)
train_loader = DataLoader(train_dataset,batch_size=batch_size,shuffle=True)
test_dataset = datasets.MNIST(root='./dataset/mnist',train=False,download=False,transform=transform)
test_loader = DataLoader(test_dataset,batch_size=batch_size,shuffle=False)
'''
2、Design Model
'''
class Net(torch.nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = torch.nn.Conv2d(1,10,kernel_size=5)#输出的通道为10,kernel_size是卷积核的大小,这里定义的是5x5
self.conv2 = torch.nn.Conv2d(10,20,kernel_size=5)
self.conv3 = torch.nn.Conv2d(20,40,kernel_size=3)
self.pooling = torch.nn.MaxPool2d(2)
# 最后是我们做分类用的线性层
self.fc1 = torch.nn.Linear(40,60)
self.fc2 = torch.nn.Linear(60,30)
self.fc3 = torch.nn.Linear(30,10)
def forward(self,x):
#将输入图像(n,1,28,28)展平为 (n,784)
batch_size = x.size(0) #求batch_size x.size(0)表示张量 x 的维度
x = F.relu(self.pooling(self.conv1(x)))
x = F.relu(self.pooling(self.conv2(x)))
x = F.relu(self.pooling(self.conv3(x)))
x = x.view(batch_size,-1) #展平操作
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x) #最后一层不做RELU激活。
return x
#实例化
model = Net()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
'''
3、Construct loss and optimizer
'''
#定义一个损失函数,来计算我们模型输出的值和标准值的差距
criterion = torch.nn.CrossEntropyLoss()
#定义一个优化器,训练模型咋训练的,就靠这个,他会反向的更改相应层的权重
optimizer = torch.optim.SGD(model.parameters(),lr=0.01,momentum = 0.5)
'''
4、Training Cycle : forward-->backward-->update
'''
def train(epoch):
running_loss = 0.0
for batch_idx,data in enumerate(train_loader,0): #每次取一个样本
inputs,target = data
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad() #优化器清0
#forward
outputs = model(inputs)
loss = criterion(outputs,target)
# backeard 反向求梯度
loss.backward()
# update 更新权重
optimizer.step()
running_loss += loss.item() #把损失加起来
# 每600次输出一下数据
if batch_idx % 600 == 599:
print("Epoch:",epoch+1,"batch_idx:",batch_idx+1,"Loss={:.4f}".format(running_loss/600))
running_loss = 0.0
return running_loss/600
def test():
correct = 0
total = 0
with torch.no_grad(): #测试不会计算梯度
for data in test_loader:
images,labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images)
# 我们取概率最大的那个数作为输出
_,predicted = torch.max(outputs.data,dim=1) #取出最大值的下标 "_,"为占位符,代表每行最大值的下标, dim = 1 列是第0个维度,行是第1个维度
total += labels.size(0)
# 计算正确率
correct += (predicted == labels).sum().item() # 张量之间的比较运算
print('Accuracy on test set: %d %% [%d/%d]' % (100 * correct / total, correct, total))
return correct / total
'''
5、main
'''
if __name__=='__main__':
epoch_list = [] # 保存epoch
acc_list = [] # 保存每个epoch的准确率
Loss_List = [] # 保存每个epoch对应的loss
for epoch in range(10):
loss = train(epoch)
acc = test()
epoch_list.append(epoch)
acc_list.append(acc)
Loss_List.append(loss)
plt.plot(epoch_list, Loss_List)
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.grid(ls='--')
plt.show()
plt.plot(epoch_list, acc_list)
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.grid(ls='--')
plt.show()
网络效果:
在经过10轮的训练之后,Loss和Accuracy图像如下: