深度学习 6. MatConvNet 相关函数解释说明,MatConvNet 代码理解(二)cnn_mnist_init.m 的注释

深度学习 6. MatConvNet 相关函数解释说明,MatConvNet 代码理解(二)cnn_mnist_init.m 的注释

 

本文为原创文章转载必须注明本文出处以及附上 本文地址超链接  以及 博主博客地址:http://blog.csdn.net/qq_20259459  和 作者邮箱( jinweizhi93@gmai.com )。

(如果喜欢本文,欢迎大家关注我的博客或者动手点个赞,有需要可以邮件联系我)


 

接上一篇文章(阅读上一篇文章:http://blog.csdn.net/qq_20259459/article/details/54411178 )

 

 

(二)cnn_mnist_init.m

 

[plain] view plain copy
 
  1. function net = cnn_mnist_init(varargin)  
  2. %% --------------------------------------------------------------  
  3. %   主函数: cnn_mnist_init  
  4. %   功能:   1.初始化CNN结构为LeNet  
  5. % ------------------------------------------------------------------------  
  6. % CNN_MNIST_LENET Initialize a CNN similar for MNIST  
  7.   
  8.   
  9. opts.batchNormalization = true ;            %选择batchNormalization为true  
  10. opts.networkType = 'simplenn' ;             %选择CNN结构为simplenn  
  11. opts = vl_argparse(opts, varargin) ;        %调用vl_argparse(通过外部参数修改初始值)  
  12.   
  13. rng('default');                             %设置随机数发生器,重现每次运行结果  
  14. rng(0) ;                                      
  15.   
  16. % 开始构建网络结构,这里是LeNet5  
  17. f=1/100 ;                                     
  18. net.layers = {} ;  
  19. net.layers{end+1} = struct('type', 'conv', ...          %卷积层C1,randn函数产生4维标准正态分布矩阵,设置偏置有20个  
  20.                            'weights', {{f*randn(5,5,1,20, 'single'), zeros(1, 20, 'single')}}, ...  %filter大小是5*5*1  
  21.                            'stride', 1, ...             %stride = 1  
  22.                            'pad', 0) ;                  %pad = 0  
  23. net.layers{end+1} = struct('type', 'pool', ...          %池化层P1  
  24.                            'method', 'max', ...  
  25.                            'pool', [2 2], ...           %池化核大小为2*2  
  26.                            'stride', 2, ...  
  27.                            'pad', 0) ;  
  28. net.layers{end+1} = struct('type', 'conv', ...          %卷积层C2  
  29.                            'weights', {{f*randn(5,5,20,50, 'single'),zeros(1,50,'single')}}, ...  
  30.                            'stride', 1, ...  
  31.                            'pad', 0) ;  
  32. net.layers{end+1} = struct('type', 'pool', ...          %池化层P2  
  33.                            'method', 'max', ...  
  34.                            'pool', [2 2], ...  
  35.                            'stride', 2, ...  
  36.                            'pad', 0) ;  
  37. net.layers{end+1} = struct('type', 'conv', ...          %卷积层C3  
  38.                            'weights', {{f*randn(4,4,50,500, 'single'),  zeros(1,500,'single')}}, ...  
  39.                            'stride', 1, ...  
  40.                            'pad', 0) ;  
  41. net.layers{end+1} = struct('type', 'relu') ;            %ReLu层  
  42. net.layers{end+1} = struct('type', 'conv', ...          %FC层  
  43.                            'weights', {{f*randn(1,1,500,10, 'single'), zeros(1,10,'single')}}, ...  
  44.                            'stride', 1, ...  
  45.                            'pad', 0) ;  
  46. net.layers{end+1} = struct('type', 'softmaxloss') ;     %softmax层  
  47.   
  48. % optionally switch to batch normalization  
  49. if opts.batchNormalization                  %如果opts.batchNormalization为真:  
  50.   net = insertBnorm(net, 1) ;               %在原网络第一层后添加Bnorm  
  51.   net = insertBnorm(net, 4) ;               %在原网络第四层后添加Bnorm  
  52.   net = insertBnorm(net, 7) ;               %在原网络第七层后添加Bnorm  
  53. end  
  54.   
  55. % Meta parameters 结构元参数  
  56. net.meta.inputSize = [28 28 1] ;            %大小为28*28*1的input data  
  57. net.meta.trainOpts.learningRate = 0.001 ;   %学习率为0.001  
  58. net.meta.trainOpts.numEpochs = 100 ;        %Epoch为100  
  59. net.meta.trainOpts.batchSize = 100 ;        %批的大小为100  
  60.   
  61. % Fill in defaul values  
  62. net = vl_simplenn_tidy(net) ;               %添加默认的属性值  
  63.   
  64. % Switch to DagNN if requested  
  65. switch lower(opts.networkType)              %选择网络结构  
  66.   case 'simplenn'                           %simplenn结构  
  67.     % done  
  68.   case 'dagnn'                              %dagnn结构  
  69.     net = dagnn.DagNN.fromSimpleNN(net, 'canonicalNames', true) ;  
  70.     net.addLayer('top1err', dagnn.Loss('loss', 'classerror'), ...  
  71.       {'prediction', 'label'}, 'error') ;  
  72.     net.addLayer('top5err', dagnn.Loss('loss', 'topkerror', ...  
  73.       'opts', {'topk', 5}), {'prediction', 'label'}, 'top5err') ;  
  74.     otherwise                               %  
  75.     assert(false) ;  
  76. end  
  77.   
  78. % --------------------------------------------------------------------  
  79. function net = insertBnorm(net, l)                      %  
  80. %% --------------------------------------------------------------  
  81. %   函数名:insertBnorm  
  82. %   功能:  1.在第l层和第l+1层之间插入Bnorm层  
  83. % ------------------------------------------------------------------------  
  84. assert(isfield(net.layers{l}, 'weights'));                              %断言以确保第l层有权重项  
  85. ndim = size(net.layers{l}.weights{1}, 4);                               %第l层的神经元的个数  
  86. layer = struct('type', 'bnorm', ...                                     %初始化Bnorm层  
  87.                'weights', {{ones(ndim, 1, 'single'), zeros(ndim, 1, 'single')}}, ...  
  88.                'learningRate', [1 1 0.05], ...                          %Bnorm层的权值=上一层的神经元个数  
  89.                'weightDecay', [0 0]) ;  
  90. net.layers{l}.biases = [] ;  
  91. net.layers = horzcat(net.layers(1:l), layer, net.layers(l+1:end)) ;     %添加Bnorm层  


 

后面会持续更新MatConvNet的其他代码的注释。

 

 

 

本文为原创文章转载必须注明本文出处以及附上 本文地址超链接  以及 博主博客地址:http://blog.csdn.net/qq_20259459  和 作者邮箱( jinweizhi93@gmai.com )。

(如果喜欢本文,欢迎大家关注我的博客或者动手点个赞,有需要可以邮件联系我)

版权声明:本文为博主原创文章,转载必须注明是转载文章和原文超链接以及作者信息。 https://blog.csdn.net/qq_20259459/article/details/54586864
posted @ 2018-04-01 21:19  菜鸡一枚  阅读(514)  评论(0)    收藏  举报