网站支付功能建设,单页网站排名没有,设计房子的软件免费,快速微信网站建设前一篇文章#xff0c;学习率调整策略 | PyTorch 深度学习实战
本系列文章 GitHub Repo: https://github.com/hailiang-wang/pytorch-get-started CNN 卷积神经网络 CNN什么是卷积工作原理深度学习的卷积运算提取特征不同特征核的效果比较卷积核感受野共享权重池化 示例源码 …前一篇文章学习率调整策略 | PyTorch 深度学习实战
本系列文章 GitHub Repo: https://github.com/hailiang-wang/pytorch-get-started CNN 卷积神经网络 CNN什么是卷积工作原理深度学习的卷积运算提取特征不同特征核的效果比较卷积核感受野共享权重池化 示例源码 Links CNN
什么是卷积
【通信原理 入坑之路】——深入、详细地理解通信里面“卷积”概念
卷积首先是一种数学运算。两个多项式通过滑动求解多项式参数。 深度学习的卷积概念就是借鉴了通信领域使用了卷积。跨学科运用知识一直是大牛们的惯用手段。掌握人类已经精通的领域的经验然后推广到前沿领域。
工作原理
利用卷积操作实现平移、扭曲情况下依然能识别特征
图片是一个二维数据如果只是利用全连接网络那么数据的二维特征就丢失了原始的物理信息丢失了。比如同一个人出现在不同的照片中很可能是在不同的位置作为同样的一张人脸当其出现在图片中的不同位置1都可以正确的识别和分类呢
深度学习的卷积运算
深度学习领域的卷积参考文章。
卷积核是一个小矩阵在输入矩阵上滑动。
最终得到一个新的 output 矩阵。
提取特征
因为这种运算Output 实际上代表了卷积核 Kernel 作用于 Input 后过滤出来的特征。每一个卷积核就是一个过滤器从源图片中提取特定的形状。为了理解这一点看下面这张图。 以黑白两个颜色实现卷积运算最终输入图片里和特征核Single filter重叠的部分得到了加强和特征核不一致的部分得到了抑制。
不同特征核的效果比较
当特征核变大增加多个特征提取器那么就可以识别一张图片上的特征组从而判定图片中包含的物体的分类。
左侧是运算符中间是对应的特征核右侧是输出的图片 当然计算机不是【看图】而是通过卷积后的矩阵从数字上去检查分类。当输出的矩阵组成一个全连接使用目标的标注数据计算出损失就可以学习分类的权重实现分类的效果。
卷积核
卷积核也称为特征提取器后者的名字更加的形象特征提取器类似于通信领域的滤波器。
感受野
感受野Receptive Field的定义是卷积神经网络每一层输出的特征图feature map上的像素点在输入图片上映射的区域大小。参考文章 共享权重
使用同一个特征核过滤图片也就是一个特征核对于一个图片上的多个感受野特征核的矩阵不变。
使用梯度下降原理更新参数时参数包括了每个卷积核虽然一个卷积核是滑动在多个感受野得到输出矩阵的但是特征核更新时不会针对单独的某个感受野。
对于一个卷积神经网络都包括哪些参数参考文章。
池化
经过多个卷积核以后维度更多虽然因为保留了重要的特征信息但是会远远的大于分类信息在加入最后的全连接层之前还需要浓缩一下信息类似于结晶。
这个操作就是池化比如常用的最大池化方法如下 示例源码
下面以一段 PyTorch 代码为例使用卷积神经网络完成图片分类任务。 CNN Modelimport torch
import torchvision.datasets as ds
import torchvision.transforms as ts
from torch.utils.data import DataLoader
from torch.autograd import Variable
import randomtorch.manual_seed(777)# reproducibility# parameters
batch_size100
learning_rate0.001
epochs2# MNIST dataset
ds_trainds.MNIST(root../../../DATA/MNIST_data,trainTrue,transformts.ToTensor(),downloadTrue)
ds_testds.MNIST(root../../../DATA/MNIST_data,trainFalse,transformts.ToTensor(),downloadTrue)
# dataset loader
dlDataLoader(datasetds_train,batch_sizebatch_size,shuffleTrue)# CNN Model (2 conv layers)
class CNN(torch.nn.Module):def __init__(self):super(CNN,self).__init__()# L1 ImgIn shape(?, 28, 28, 1)# Conv - (?, 28, 28, 32)# Pool - (?, 14, 14, 32)self.layer1torch.nn.Sequential(torch.nn.Conv2d(1,32,kernel_size3,stride1,padding1),#padding1进行0填充torch.nn.ReLU(),torch.nn.MaxPool2d(kernel_size2,stride2))# L2 ImgIn shape(?, 14, 14, 32)# Conv -(?, 14, 14, 64)# Pool -(?, 7, 7, 64)self.layer2torch.nn.Sequential(torch.nn.Conv2d(32,64,kernel_size3,stride1,padding1),torch.nn.ReLU(),torch.nn.MaxPool2d(kernel_size2,stride2))# Final FC 7x7x64 inputs - 10 outputsself.fctorch.nn.Linear(7*7*64,10)torch.nn.init.xavier_uniform(self.fc.weight)def forward(self,x):outself.layer1(x)outself.layer2(out)outout.view(out.size(0),-1)# Flatten them for FCoutself.fc(out)return out# instantiate CNN model
modelCNN()# define cost/loss optimizer
criteriontorch.nn.CrossEntropyLoss()# Softmax is internally computed.
optimizertorch.optim.Adam(model.parameters(),lrlearning_rate)# train my model
print(Learning started. It takes sometime.)
for epoch in range(epochs):avg_cost0total_batchlen(ds_train)//batch_sizefor step,(batch_xs,batch_ys) in enumerate(dl):xVariable(batch_xs)#[100, 1, 28, 28] image is already size of (28x28), no reshapeyVariable(batch_ys)#[100] label is not one-hot encodedoptimizer.zero_grad()hmodel(x)costcriterion(h,y)cost.backward()optimizer.step()avg_costcost/total_batchprint(epoch1,avg_cost.item())
print(Learning Finished!)# Test model and check accuracy
model.eval()#将模型设置为评估/测试模式 set the model to evaluation mode (dropoutFalse)# x_testds_test.test_data.view(len(ds_test),1,28,28).float()
x_testds_test.test_data.view(-1,1,28,28).float()
y_testds_test.test_labelspremodel(x_test)print(pre.data)
print(pre.data)
print(**3)pretorch.max(pre.data,1)[1].float()
acc(prey_test.data.float()).float().mean()
print(acc, acc)rrandom.randint(0,len(x_test)-1)
x_rx_test[r:r1]
y_ry_test[r:r1]
pre_rmodel(x_r)# IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
# https://discuss.pytorch.org/t/indexerror-dimension-out-of-range-expected-to-be-in-range-of-1-0-but-got-1/54267/12
print(pre_r.data)
print(pre_r.data)
print(**3)pre_rtorch.max(pre_r.data,-1)[1].float()
print(pre_r)
print(pre_r)acc_r(pre_ry_r.data).float().mean()
print(acc_r)Links
卷积神经网络中感受野的详细介绍感受野详解【通信原理 入坑之路】——深入、详细地理解通信里面“卷积”概念How to calculate the number of parameters in CNN?【深度学习】人人都能看得懂的卷积神经网络——入门篇 图片相关任务包括图片分类、物体检测、实例分割、目标跟踪等。这些任务有不同的功能但是都依赖于图片中包含的特征这些特征都是可能平移、变幻、扭曲的。 ↩︎