前面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的代码:

  1. function net = cnnsetup(net, x, y)
  2. inputmaps = ;
  3. mapsize = size(squeeze(x(:, :, )));
  4.  
  5. for l = : numel(net.layers) % layer
  6. if strcmp(net.layers{l}.type, 's')
  7. mapsize = mapsize / net.layers{l}.scale;
  8. assert(all(floor(mapsize)==mapsize), ['Layer ' num2str(l) ' size must be integer. Actual: ' num2str(mapsize)]);
  9. for j = : inputmaps
  10. net.layers{l}.b{j} = ;
  11. end
  12. end
  13. if strcmp(net.layers{l}.type, 'c')
  14. mapsize = mapsize - net.layers{l}.kernelsize + ;
  15. fan_out = net.layers{l}.outputmaps * net.layers{l}.kernelsize ^ ;
  16. for j = : net.layers{l}.outputmaps % output map
  17. fan_in = inputmaps * net.layers{l}.kernelsize ^ ;
  18. for i = : inputmaps % input map
  19. net.layers{l}.k{i}{j} = (rand(net.layers{l}.kernelsize) - 0.5) * * sqrt( / (fan_in + fan_out));
  20. end
  21. net.layers{l}.b{j} = ;
  22. end
  23. inputmaps = net.layers{l}.outputmaps;
  24. end
  25. end
  26. % '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.
  27. % 'fvnum' is the number of output neurons at the last layer, the layer just before the output layer.
  28. % 'ffb' is the biases of the output neurons.
  29. % '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)
  30. fvnum = prod(mapsize) * inputmaps;
  31. onum = size(y, );
  32.  
  33. net.ffb = zeros(onum, );
  34. net.ffW = (rand(onum, fvnum) - 0.5) * * sqrt( / (onum + fvnum));
  35. end

里面inputmaps是上一层的feature map数。outputmaps当前层的feature map数。 其中有一行代码是

  1. 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即前向传播的部分代码:

  1. function net = cnnff(net, x)
  2. n = numel(net.layers);
  3. net.layers{}.a{} = x;
  4. inputmaps = ;
  5.  
  6. for l = : n % for each layer
  7. if strcmp(net.layers{l}.type, 'c')
  8. % !!below can probably be handled by insane matrix operations
  9. for j = : net.layers{l}.outputmaps % for each output map
  10. % create temp output map
  11. z = zeros(size(net.layers{l - }.a{}) - [net.layers{l}.kernelsize - net.layers{l}.kernelsize - ]);
  12. for i = : inputmaps % for each input map
  13. % convolve with corresponding kernel and add to temp output map
  14. z = z + convn(net.layers{l - }.a{i}, net.layers{l}.k{i}{j}, 'valid');
  15. end
  16. % add bias, pass through nonlinearity
  17. net.layers{l}.a{j} = sigm(z + net.layers{l}.b{j});
  18. end
  19. % set number of input maps to this layers number of outputmaps
  20. inputmaps = net.layers{l}.outputmaps;
  21. elseif strcmp(net.layers{l}.type, 's')
  22. % downsample
  23. for j = : inputmaps
  24. z = convn(net.layers{l - }.a{j}, ones(net.layers{l}.scale) / (net.layers{l}.scale ^ ), 'valid'); % !! replace with variable
  25. net.layers{l}.a{j} = z( : net.layers{l}.scale : end, : net.layers{l}.scale : end, :);
  26. end
  27. end
  28. end
  29.  
  30. % concatenate all end layer feature maps into vector
  31. net.fv = [];
  32. for j = : numel(net.layers{n}.a)
  33. sa = size(net.layers{n}.a{j});
  34. net.fv = [net.fv; reshape(net.layers{n}.a{j}, sa() * sa(), sa())];
  35. end
  36. % feedforward into output perceptrons
  37. net.o = sigm(net.ffW * net.fv + repmat(net.ffb, , size(net.fv, )));
  38.  
  39. end

其中卷积层的代码确实是用了提前初始化的卷积核:

  1. for j = : net.layers{l}.outputmaps % for each output map
  2. % create temp output map
  3. z = zeros(size(net.layers{l - }.a{}) - [net.layers{l}.kernelsize - net.layers{l}.kernelsize - ]);
  4. for i = : inputmaps % for each input map
  5. % convolve with corresponding kernel and add to temp output map
  6. z = z + convn(net.layers{l - }.a{i}, net.layers{l}.k{i}{j}, 'valid');
  7. end
  8. % add bias, pass through nonlinearity
  9. 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. [cocos2d]关于CCSprite的若干问题与误区

    文章 [cocos2d] 利用texture atlases生成动画 中介绍了如何生成动画并绑定在CCSprite实例上. 使用该代码遇到了几个问题,值得mark下 问题1.多实例 问题描述: 新建一 ...

  2. 创办支持多种屏幕尺寸的Android应用

    创建支持多种屏幕尺寸的Android应用 Android涉及各种各样的支持不同屏幕尺寸和密度的设备.对于应用程序,Android系统通过设备和句柄提供了统一的开发环境,大部分工作是校正每一个应用程序的 ...

  3. 网络流(费用流)CodeForces 321B:Ciel and Duel

    Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: ...

  4. HDU-1225 Football Score

    http://acm.hdu.edu.cn/showproblem.php?pid=1225 一道超级简单的题,就因为我忘记写return,就wa好久,拜托我自己细心一点. 学习的地方:不过怎么查找字 ...

  5. HDOJ/HDU 2555 人人都能参加第30届校田径运动会了(判断加排序~)

    Problem Description 杭州师范大学第29届田径运动会圆满的闭幕了,本届运动会是我校规模最大,参赛人数最多的一次运动会.在两天半时间里,由学生.教工组成的61支代表队共2664名运动员 ...

  6. java多线程编程(2)交替输出数字和字母

    mark一下,不停的看看notify和wait的没有理解 class Printer { int index=0; //输出奇数 public synchronized void printA(int ...

  7. win2008 ent r2 开启端口

    你好.win2008r2 ent 可以用以下命令行来实现,当然也可以用防火墙来配置 比如打开8080端口方法如下: netsh firewall add portopening TCP 8080 My ...

  8. redis中各种数据类型对应的jedis操作命令

    redis中各种数据类型对应的jedis操作命令 一.常用数据类型简介: redis常用五种数据类型:string,hash,list,set,zset(sorted set). 1.String类型 ...

  9. Java处理文件小例子--获取全国所有城市的坐标

    需求:前端展示数据,全国城市的坐标

  10. [Oracle] Data Pump 详细使用教程(4)- network_link

    [Oracle] Data Pump 详细使用教程(1)- 总览 [Oracle] Data Pump 详细使用教程(2)- 总览 [Oracle] Data Pump 详细使用教程(3)- 总览 [ ...