前面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)理解的更多相关文章

  1. CNN(Convolutional Neural Network)

    CNN(Convolutional Neural Network) 卷积神经网络(简称CNN)最早可以追溯到20世纪60年代,Hubel等人通过对猫视觉皮层细胞的研究表明,大脑对外界获取的信息由多层的 ...

  2. Deep Learning 学习随记(七)Convolution and Pooling --卷积和池化

    图像大小与参数个数: 前面几章都是针对小图像块处理的,这一章则是针对大图像进行处理的.两者在这的区别还是很明显的,小图像(如8*8,MINIST的28*28)可以采用全连接的方式(即输入层和隐含层直接 ...

  3. Deep Learning学习随记(一)稀疏自编码器

    最近开始看Deep Learning,随手记点,方便以后查看. 主要参考资料是Stanford 教授 Andrew Ng 的 Deep Learning 教程讲义:http://deeplearnin ...

  4. 课程一(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 ...

  5. 课程一(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 ...

  6. Deep Learning 学习随记(五)深度网络--续

    前面记到了深度网络这一章.当时觉得练习应该挺简单的,用不了多少时间,结果训练时间真够长的...途中debug的时候还手贱的clear了一下,又得从头开始运行.不过最终还是调试成功了,sigh~ 前一篇 ...

  7. Deep Learning 学习随记(五)Deep network 深度网络

    这一个多周忙别的事去了,忙完了,接着看讲义~ 这章讲的是深度网络(Deep Network).前面讲了自学习网络,通过稀疏自编码和一个logistic回归或者softmax回归连接,显然是3层的.而这 ...

  8. Deep Learning 学习随记(四)自学习和非监督特征学习

    接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...

  9. Deep Learning学习随记(二)Vectorized、PCA和Whitening

    接着上次的记,前面看了稀疏自编码.按照讲义,接下来是Vectorized, 翻译成向量化?暂且这么认为吧. Vectorized: 这节是老师教我们编程技巧了,这个向量化的意思说白了就是利用已经被优化 ...

随机推荐

  1. LeetCode解题报告:Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Yo ...

  2. [LeetCode#263]Factorial Trailing Zeroes

    Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positiv ...

  3. bzoj1475

    明显的二分图最大独立点权集 ans=总点权-最小割(最大流) ..] ,,-,);       dy:..] ,-,,);       inf=; type node=record        ne ...

  4. bootstrap真是个好东西

    之前就知道有bootstrap这么个东东,但是因为本身不做web,也就没有仔细了解.这次一个项目合作方使用django和bootstrap做的,有机会接触了一些,感觉确实非常好! 今天下午利用一个下午 ...

  5. eclipse配置j2ee项目

    1.下载jdk (1.5,1.6) 安装 从sun的官方网站下载,我下的是jdk-1_5_0_19-nb-6_5_1-windows-ml.exe,集成netbean的版本,下载后一路默认安装. 配置 ...

  6. java多线程编程(3)买票

    1,买票非同步版本 http://www.cnblogs.com/anbylau2130/archive/2013/04/17/3025347.html很详细 public class 多线程2 { ...

  7. ACM1024动态规划

    #include <cstdio> #include <algorithm> #define FI(a, b, c) for(int a = (b); a <= (c); ...

  8. 《A First Course in Mathematical Modeling》-chaper2-建模过程、比例性及几何相似性

    这一章节着重从整体的层面给出数学建模过程中一个泛式流程,它给出了在给现实模型建立数学模型的框架性思路,但是需要注意的是,虽然这里称其为一种“泛式”思路,但是在具体的问题的分析中,整个建模过程还是充满了 ...

  9. Scrambled Polygon - POJ 2007(求凸包)

    给一些点,这些点都是一个凸包上的顶点,以第一个点为起点顺时针把别的点拍排一下序列. 分析:最简单的极坐标排序了..................... 代码如下: ----------------- ...

  10. Broken line - SGU 124(判断点与多边形的关系)

    题目大意:RT 分析:构造一条射线,如果穿越偶数条边,那么就在多边形外面,如果穿越奇数条边,那么就在多边形里面. 代码如下: ===================================== ...