Deep Learning 学习随记(八)CNN(Convolutional neural network)理解
前面Andrew Ng的讲义基本看完了。Andrew讲的真是通俗易懂,只是不过瘾啊,讲的太少了。趁着看完那章convolution and pooling, 自己又去翻了翻CNN的相关东西。
当时看讲义时,有一点是不太清楚的,就是讲义只讲了一次convolution和一次pooling,而且第一次的convolution很容易理解,针对一副图像来的,但是经过一次convolution和pooling
后,一副图像变成了好多副特征图(feature map)这时候再进行convolution时,该怎么办呢?所以去瞅了瞅CNN的相关论文。
CNN最经典的案例应该是LeNet-5这个数字识别的任务了吧。这里可以看下Yann Lecun大牛网页 http://yann.lecun.com/exdb/lenet/index.html, 以及tutorial: http://deeplearning.net/tutorial/lenet.html。
另外,一篇比较详细的讲CNN的中文博客(懒得看英语的话,就直接看这篇博客了):http://blog.csdn.net/zouxy09/article/details/8781543。
这里面都给出了CNN的结构图如下。

具体每个的含义这里也不说了,可以参考前面提到的资料。
看了结构图基本了解了。有几点一开始没看懂的需要说明的:
1. 关于每一个C层的feature map的个数。
比如C1是6个,C3是16个,这个应该是经验值,或者是通过实验给出的一个比较优的值,这点好多资料都没有说清楚。不过要注意的是,一般后面的要比前面的个数多些。
2. 关于后面的C层。比如S2到C3,并不是一一对应的。
也就是说,并不是对S2中的每一个feature map与后面16个卷积核进行卷积。而是取其中几个。看了下面图应该很容易理解:

纵向是S2层的6个feature map,横向是C3的16个卷积核,X表示两者相连。比如说,第0个卷积核,只用在了前面3个feature map上,把这3个卷积结果加权相加或者平均就得到C3层的第一个(如按照上图标示应该是第0个)feature map。至于这个对应表示怎么来的,也不得而知啊,应该也是经验或者通过大量实验得来的吧。这点还不是很清楚...当然,如果想全部相连也不是不可以,只是对5个相加或者进行加权平均而已(比如第15号卷积核)。
3. 关于每一层的卷积核是怎么来的。
从Andrew的讲义中,我们是先从一些小patch里用稀疏自编码学习到100个特征(隐层100个单元),然后相当于100个卷积核(不知这样理解对不对)。这样子后面的卷积层的核怎么做呢?每一层都用前一层pooling(或者降采样)后得到的feature map再进行一次自编码学习?。这里就想到了去看toolbox里的CNN的代码,但感觉不是同一个套路:
下面是cnnsetup.m的代码:
function net = cnnsetup(net, x, y)
inputmaps = ;
mapsize = size(squeeze(x(:, :, ))); for l = : numel(net.layers) % layer
if strcmp(net.layers{l}.type, 's')
mapsize = mapsize / net.layers{l}.scale;
assert(all(floor(mapsize)==mapsize), ['Layer ' num2str(l) ' size must be integer. Actual: ' num2str(mapsize)]);
for j = : inputmaps
net.layers{l}.b{j} = ;
end
end
if strcmp(net.layers{l}.type, 'c')
mapsize = mapsize - net.layers{l}.kernelsize + ;
fan_out = net.layers{l}.outputmaps * net.layers{l}.kernelsize ^ ;
for j = : net.layers{l}.outputmaps % output map
fan_in = inputmaps * net.layers{l}.kernelsize ^ ;
for i = : inputmaps % input map
net.layers{l}.k{i}{j} = (rand(net.layers{l}.kernelsize) - 0.5) * * sqrt( / (fan_in + fan_out));
end
net.layers{l}.b{j} = ;
end
inputmaps = net.layers{l}.outputmaps;
end
end
% 'onum' is the number of labels, that's why it is calculated using size(y, 1). If you have 20 labels so the output of the network will be 20 neurons.
% 'fvnum' is the number of output neurons at the last layer, the layer just before the output layer.
% 'ffb' is the biases of the output neurons.
% 'ffW' is the weights between the last layer and the output neurons. Note that the last layer is fully connected to the output layer, that's why the size of the weights is (onum * fvnum)
fvnum = prod(mapsize) * inputmaps;
onum = size(y, ); net.ffb = zeros(onum, );
net.ffW = (rand(onum, fvnum) - 0.5) * * sqrt( / (onum + fvnum));
end
里面inputmaps是上一层的feature map数。outputmaps当前层的feature map数。 其中有一行代码是
net.layers{l}.k{i}{j} = (rand(net.layers{l}.kernelsize) - 0.5) * 2 * sqrt(6 / (fan_in + fan_out));
这一句应该就是初始化卷积核了。这里是随机生成的一个在某个范围内的kernelsize*kernelsize的卷积核。其中i和j分别对应inputmaps和outputmaps。也就是说是为每一个连接初始化了一个卷积核。
下面再看下cnnff.m即前向传播的部分代码:
function net = cnnff(net, x)
n = numel(net.layers);
net.layers{}.a{} = x;
inputmaps = ; for l = : n % for each layer
if strcmp(net.layers{l}.type, 'c')
% !!below can probably be handled by insane matrix operations
for j = : net.layers{l}.outputmaps % for each output map
% create temp output map
z = zeros(size(net.layers{l - }.a{}) - [net.layers{l}.kernelsize - net.layers{l}.kernelsize - ]);
for i = : inputmaps % for each input map
% convolve with corresponding kernel and add to temp output map
z = z + convn(net.layers{l - }.a{i}, net.layers{l}.k{i}{j}, 'valid');
end
% add bias, pass through nonlinearity
net.layers{l}.a{j} = sigm(z + net.layers{l}.b{j});
end
% set number of input maps to this layers number of outputmaps
inputmaps = net.layers{l}.outputmaps;
elseif strcmp(net.layers{l}.type, 's')
% downsample
for j = : inputmaps
z = convn(net.layers{l - }.a{j}, ones(net.layers{l}.scale) / (net.layers{l}.scale ^ ), 'valid'); % !! replace with variable
net.layers{l}.a{j} = z( : net.layers{l}.scale : end, : net.layers{l}.scale : end, :);
end
end
end % concatenate all end layer feature maps into vector
net.fv = [];
for j = : numel(net.layers{n}.a)
sa = size(net.layers{n}.a{j});
net.fv = [net.fv; reshape(net.layers{n}.a{j}, sa() * sa(), sa())];
end
% feedforward into output perceptrons
net.o = sigm(net.ffW * net.fv + repmat(net.ffb, , size(net.fv, ))); end
其中卷积层的代码确实是用了提前初始化的卷积核:
for j = : net.layers{l}.outputmaps % for each output map
% create temp output map
z = zeros(size(net.layers{l - }.a{}) - [net.layers{l}.kernelsize - net.layers{l}.kernelsize - ]);
for i = : inputmaps % for each input map
% convolve with corresponding kernel and add to temp output map
z = z + convn(net.layers{l - }.a{i}, net.layers{l}.k{i}{j}, 'valid');
end
% add bias, pass through nonlinearity
net.layers{l}.a{j} = sigm(z + net.layers{l}.b{j});
end
这里,使用的全连接的方式,不像2中提到的那样有选择性的连接。
Deep Learning 学习随记(八)CNN(Convolutional neural network)理解的更多相关文章
- CNN(Convolutional Neural Network)
CNN(Convolutional Neural Network) 卷积神经网络(简称CNN)最早可以追溯到20世纪60年代,Hubel等人通过对猫视觉皮层细胞的研究表明,大脑对外界获取的信息由多层的 ...
- Deep Learning 学习随记(七)Convolution and Pooling --卷积和池化
图像大小与参数个数: 前面几章都是针对小图像块处理的,这一章则是针对大图像进行处理的.两者在这的区别还是很明显的,小图像(如8*8,MINIST的28*28)可以采用全连接的方式(即输入层和隐含层直接 ...
- Deep Learning学习随记(一)稀疏自编码器
最近开始看Deep Learning,随手记点,方便以后查看. 主要参考资料是Stanford 教授 Andrew Ng 的 Deep Learning 教程讲义:http://deeplearnin ...
- 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 4、Logistic Regression with a Neural Network mindset
Logistic Regression with a Neural Network mindset Welcome to the first (required) programming exerci ...
- 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 3、Python Basics with numpy (optional)
Python Basics with numpy (optional)Welcome to your first (Optional) programming exercise of the deep ...
- Deep Learning 学习随记(五)深度网络--续
前面记到了深度网络这一章.当时觉得练习应该挺简单的,用不了多少时间,结果训练时间真够长的...途中debug的时候还手贱的clear了一下,又得从头开始运行.不过最终还是调试成功了,sigh~ 前一篇 ...
- Deep Learning 学习随记(五)Deep network 深度网络
这一个多周忙别的事去了,忙完了,接着看讲义~ 这章讲的是深度网络(Deep Network).前面讲了自学习网络,通过稀疏自编码和一个logistic回归或者softmax回归连接,显然是3层的.而这 ...
- Deep Learning 学习随记(四)自学习和非监督特征学习
接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...
- Deep Learning学习随记(二)Vectorized、PCA和Whitening
接着上次的记,前面看了稀疏自编码.按照讲义,接下来是Vectorized, 翻译成向量化?暂且这么认为吧. Vectorized: 这节是老师教我们编程技巧了,这个向量化的意思说白了就是利用已经被优化 ...
随机推荐
- 【HDOJ】2966 In case of failure
KD树,这东西其实在ML经常被使用,不过30s的时限还是第一次见. /* 2966 */ #include <iostream> #include <string> #incl ...
- Django中的Form(二)
一.保存用户输入内容 如果用户输入一张表单提交后出现错误时,会出现重现填写的情况.我们可以把用户输入的信息保存下来,并返回到前台页面,这样用户就无需再次输入. views.py # coding:ut ...
- 分布式系统里session同步的那些事儿
几周前,有个盆友问老王,说现在有多台服务器,怎么样来解决这些服务器间的session同步问题?老王一下就来精神了,因为在n年以前,老王还在学校和几个同学一起所谓创业的时候,也遇到了类似的问题.当时查了 ...
- hdoj 1599 find the mincost route【floyd+最小环】
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- java_method_readFile读取文件文本txt
/** * @Title: TxtAndCsvUtils.java * @Package cn.com.qmhd.tools * @Description: TODO(读取txt和CSV文档) * @ ...
- SQL server 创建表,索引,主键,外键
if object_id('student', 'U') is not null drop table student go create table student( sno varchar(20) ...
- 解决方法:未能加载文件或程序集“Microsoft.Office.Interop.Excel。。
.NET错误提示:未能加载文件或程序集“Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToke ...
- Android 颜色渲染(三) Shader颜色渲染
版权声明:本文为博主原创文章,未经博主允许不得转载. 相信看过在上一篇中提到的三篇文章后,大家已经对颜色处理方面有更深的体会. 之前讲到颜色渐变的效果, 具体怎么做呢,这就需要应用颜色渲染. 首先要介 ...
- Android之TextureView浅析
近期.在改动Android4.4的原生相机Camera2,非常习惯的去寻找SurfaceView.结果任凭我使用grep还是ack.都无法搜索到SurfaceView,最后还是通过代码CameraAc ...
- linux安装总结(亲测)
一:磁盘分区情况 NTFS,FAT32 这是两种常用的磁盘格式 windows7自带的磁盘工具也可以分出上面的格式,用软件也可以.但是FAT32最大的复制文件进去的限度时4G. EXT3只能用软件才能 ...