前面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. Linux下查看CPU信息

    在linux操作系统中,CPU的信息在启动的过程中被装载到虚拟目录/proc下的cpuinfo文件中,我们可以通过 cat /proc/cpuinfo 查看一下:

  2. 《STL源码剖析》chapter2空间配置器allocator

    为什么不说allocator是内存配置器而说是空间配置器,因为空间不一定是内存,也可以是磁盘或其他辅助介质.是的,你可以写一个allocator,直接向硬盘取空间.sgi stl提供的配置器,配置的对 ...

  3. 【HDOJ】2782 The Worm Turns

    DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...

  4. Linux Kernel TUNSETIFF释放后重用本地拒绝服务漏洞(CVE-2013-4343)

    漏洞版本: Linux kernel <= 3.11 漏洞描述: BUGTRAQ ID: 62360 CVE(CAN) ID: CVE-2013-4343 Linux Kernel是Linux操 ...

  5. (转载)mysql_query( )返回值

    (转载)http://hi.baidu.com/tfbzccqceabfhyd/item/bd01db9f8995204af04215e4 调用mysql_query( ),当查询操作是update. ...

  6. 每天进步一点点--JS中的getYear()

    又是这两天在项目中遇到的,或许很简单,但真实第一次遇到,记录一下. 在页面上用JS获取了一下当前的日期,并用getYear()方法返回了当前的年度,2013也没问题,代码在IE中都测试通过了之后就提交 ...

  7. HDOJ1175连连看 DFS

    连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  8. Traffic Lights - SGU 103(最短路)

    题目大意:有一个城市的路线图,有N个交叉点,每两个交叉点之间只有一条路,现在想从交点u去交点v,不过这个路的交通比较特别,每个路都有一个交通灯,灯有两种颜色,蓝色和紫色,例如一条路线在交点s,t之间, ...

  9. 使用Spring-data-redis操作Redis的Sentinel

    介绍 Spring-Data-Redis项目(简称SDR) 是对Redis的Key-Value数据存储操作提供了更高层次的抽象,提供了一个对几种主要的redis的Java客户端(例 如:jedis,j ...

  10. css3图片滤镜

    http://www.zhanxin.info/development/2012-12-19-css-filter.html