Deeplearning原文作者Hinton代码注解

[z]Deeplearning原文作者Hinton代码注解

跑Hinton最初代码时看到这篇注释文章,很少细心,待研究。。。
原文地址:>http://www.cnblogs.com/BeDPS/p/3182725.html

    1. Matlab示例代码为两部分,分别对应不同的论文:   
    2. 1. Reducing the Dimensionality of data with neural networks    
    3.   ministdeepauto.m   backprop.m   rbmhidlinear.m   
    4. 2. A fast learing algorithm for deep belief net   
    5.   mnistclassify.m   backpropclassfy.m     
    6.  其余部分代码通用。   
    7. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    8. mnistclassify.m   
    9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    10. clear all   
    11. close all   
    12. maxepoch=50;    %迭代次数   
    13. numhid=500; numpen=500; numpen2=2000;    
    14. fprintf(1,’Converting Raw files into Matlab format  ’);   
    15. converter;     
    16. fprintf(1,’Pretraining a deep autoencoder.  ’);   
    17. fprintf(1,’The Science paper used 50 epochs. This uses %3i  ’, maxepoch);   
    18. makebatches;%分批数据    
    19. [numcases numdims numbatches]=size(batchdata); %获取batchdata数据大小   
    20. %%numcases  每批数据的个数   
    21. %%numdims   数据元组的维度   
    22. %%numbtches 数据批数   
    23. fprintf(1,’Pretraining Layer with RBM: %d-%d  ’,numdims,numhid);%图像输入层到第一个隐藏层   
    24. restart=1;                  %设置初始化参数   
    25. rbm;                %调用RBM训练数据    
    26. hidrecbiases=hidbiases;  %获取隐藏层偏置值   
    27. save mnistvhclassify vishid hidrecbiases visbiases; %   
    28. fprintf(1,’ Pretraining Layer with RBM: %d-%d  ’,numhid,numpen);%第一个隐藏层到第二个隐藏层   
    29. batchdata=batchposhidprobs;     %上一个RBM的隐藏层输出,读入作为这个RBM的输入   
    30. numhid=numpen;%设置隐藏层的节点数,输入的节点数已经由读入数据给出   
    31. restart=1;   
    32. rbm;   
    33. hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases; %同上,提取权值,偏置,   
    34. save mnisthpclassify hidpen penrecbiases hidgenbiases;   
    35. fprintf(1,’ Pretraining Layer with RBM: %d-%d  ’,numpen,numpen2);%第二个隐藏层到第三层隐藏层,其余同上   
    36. batchdata=batchposhidprobs;   
    37. numhid=numpen2;   
    38. restart=1;   
    39. rbm;   
    40. hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases;   
    41. save mnisthp2classify hidpen2 penrecbiases2 hidgenbiases2;   
    42. backpropclassify;    
    43.     
    44.   
    45. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    46.  backpropclassify.m   
    47. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    48. maxepoch=200;   
    49. fprintf(1,’ Training discriminative model on MNIST by minimizing cross entropy error.  ’);%最小化交叉熵   
    50. fprintf(1,’60 batches of 1000 cases each.  ’);   
    51. load mnistvhclassify%加载各层之间的权值,以及偏置   
    52. load mnisthpclassify   
    53. load mnisthp2classify   
    54. makebatches;%分批数据   
    55. [numcases numdims numbatches]=size(batchdata);   
    56. N=numcases; %获取每批数据向量数   
    57. %%%% PREINITIALIZE WEIGHTS OF THE DISCRIMINATIVE MODEL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    58. w1=[vishid; hidrecbiases];%第一层到第二层的权重,以及第二层的偏置   
    59. w2=[hidpen; penrecbiases];%类上   
    60. w3=[hidpen2; penrecbiases2];%类上   
    61. w_class = 0.1*randn(size(w3,2)+1,10);%随机生成第四层列数+1行,10列的矩阵   
    62.     
    63. %%%%%%%%%% END OF PREINITIALIZATIO OF WEIGHTS  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    64. l1=size(w1,1)-1;%获取每层的单元个数   
    65. l2=size(w2,1)-1;   
    66. l3=size(w3,1)-1;   
    67. l4=size(w_class,1)-1;%最高层的单元个数   
    68. l5=10; %label层单元个数   
    69. test_err=[];%   
    70. train_err=[];%   
    71.   
    72. for epoch = 1:maxepoch   
    73. %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    74. err=0;    
    75. err_cr=0;   
    76. counter=0;   
    77. [numcases numdims numbatches]=size(batchdata);   
    78. %%numcases  每批数据的个数   
    79. %%numdims   数据元组的维度   
    80. %%numbtches 数据批数   
    81. N=numcases;%%每批次数据向量个数   
    82.  for batch = 1:numbatches   
    83.   data = [batchdata(:,:,batch)];%读取一批次数据   
    84.   target = [batchtargets(:,:,batch)];%读取当前批次的目标值   
    85.   data = [data ones(N,1)];%在原数据后添加N行1列数据   
    86.   w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs  ones(N,1)];%sigmod计算各层的概率值,参见BP算法   
    87.   w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)];   
    88.   w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs  ones(N,1)];   
    89.      
    90.   targetout = exp(w3probs*w_class);%计算最后的输出值N行10列   
    91.   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    92.   %对最后的label的输出处理过程,见公式6.1,其中w3probs*w_class是label的输入   
    93.   %最后只能有一个单元被激活,激活单元的选择即通过下面计算得出的概率来进行选择   
    94.   %10个单元组成的“softmax”组   
    95.   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    96.   targetout = targetout./repmat(sum(targetout,2),1,10);%计算最后10个label输出除以输出值的总和   
    97.   [I J]=max(targetout,[],2);%取计算结果每行中的最大值,以及其列标   
    98.   [I1 J1]=max(target,[],2);%取原先设定目标值的最大值以及列标   
    99.   counter=counter+length(find(J==J1));%统计正确的条数   
    100.   err_cr = err_cr- sum(sum( target(:,1:end).*log(targetout))) ; %%%%????   
    101.  end  
    102.  train_err(epoch)=(numcases*numbatches-counter);%总的错误条数???   
    103.  train_crerr(epoch)=err_cr/numbatches;%平均每批次错误率???   
    104. %%%%%%%%%%%%%% END OF COMPUTING TRAINING MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    105. %%%%%%%%%%%%%%%%%%%% COMPUTE TEST MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    106. err=0;   
    107. err_cr=0;   
    108. counter=0;   
    109. [testnumcases testnumdims testnumbatches]=size(testbatchdata);   
    110. N=testnumcases;   
    111. for batch = 1:testnumbatches   
    112.   data = [testbatchdata(:,:,batch)];   
    113.   target = [testbatchtargets(:,:,batch)];   
    114.   data = [data ones(N,1)];   
    115.   w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs  ones(N,1)];   
    116.   w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)];   
    117.   w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs  ones(N,1)];   
    118.   targetout = exp(w3probs*w_class);   
    119.   targetout = targetout./repmat(sum(targetout,2),1,10);   
    120.   [I J]=max(targetout,[],2);   
    121.   [I1 J1]=max(target,[],2);   
    122.   counter=counter+length(find(J==J1));   
    123.   err_cr = err_cr- sum(sum( target(:,1:end).*log(targetout))) ;   
    124. end  
    125.  test_err(epoch)=(testnumcases*testnumbatches-counter);   
    126.  test_crerr(epoch)=err_cr/testnumbatches;   
    127.  fprintf(1,’Before epoch %d Train # misclassified: %d (from %d). Test # misclassified: %d (from %d)      ’,…   
    128.             epoch,train_err(epoch),numcases*numbatches,test_err(epoch),testnumcases*testnumbatches);    
    129. %%%%%%%%%%%%%% END OF COMPUTING TEST MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    130.  tt=0;    
    131.  for batch = 1:numbatches/10  
    132.  fprintf(1,’epoch %d batch %d ’,epoch,batch);   
    133. %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    134. %组合10个小批次为1000样例的批次,然后用conjugate gradient来进行微调   
    135.  tt=tt+1;    
    136.  data=[];   
    137.  targets=[];    
    138.  for kk=1:10  
    139.   data=[data    
    140.         batchdata(:,:,(tt-1)*10+kk)]; %10个小批次合成   
    141.   targets=[targets   
    142.         batchtargets(:,:,(tt-1)*10+kk)];   
    143.  end    
    144. %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    145.   max_iter=3;       %设置线性搜索的次数   
    146.   if epoch<6                            % First update top-level weights holding other weights fixed.     
    147.     N = size(data,1);               %获取数据的行数    
    148.     XX = [data ones(N,1)];    %每行数据后面增加1,用来增加偏置   
    149.     w1probs = 1./(1 + exp(-XX*w1)); w1probs = [w1probs  ones(N,1)];   
    150.     w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)];   
    151.     w3probs = 1./(1 + exp(-w2probs*w3)); %w3probs = [w3probs  ones(N,1)];   
    152.     VV = [w_class(:)']‘;    %VV将随机生成的向量w_class展开成一列???为什么展开成一列与minimize的参数有关   
    153.     %   
    154.     Dim = [l4; l5];             %记录最后两层的单元节点数,即2000的隐藏层和10的label层   
    155.     [X, fX] = minimize(VV,’CG_CLASSIFY_INIT’,max_iter,Dim,w3probs,targets);%只训练两层 %%%详细见函数定义   
    156.     %minimize is Cari Rasmusssen’s ”minimize” code   
    157.     %%——————参数含义——————%%   
    158.     %VV         随机权重向量的展开 ,其作为输入参数,列必须为1(D by 1)         
    159.     %X          函数f=”CG_CLASSIFY_INIT”的最优化参数   
    160.     %fX         函数f对X的偏导   
    161.     %max_iter  如果为正,表示线性搜索次数,为负,函数的最大值个数   
    162.     %%————————————————-%   
    163.     w_class = reshape(X,l4+1,l5);%恢复权值矩阵结构   
    164.   else                      %进入整体微调过程   
    165.     VV = [w1(: )' w2(: )' w3(: )' w_class(: )']‘; %将所有权值按列展开成一列   
    166.     Dim = [l1; l2; l3; l4; l5]; %记录各层单元个数传入   
    167.     [X, fX] = minimize(VV,’CG_CLASSIFY’,max_iter,Dim,data,targets);   
    168.     w1 = reshape(X(1: (l1+1)*l2),l1+1,l2);   %恢复W1权值1.0  
    169.     xxx = (l1+1)*l2;  %临时变量,用于恢复权值单元   
    170.     w2 = reshape(X (xxx+1: xxx+ (l2+1)*l3),l2+1,l3);   
    171.     xxx = xxx+(l2+1)*l3;   
    172.     w3 = reshape(X (xxx+1: xxx+ (l3+1)*l4),l3+1,l4);   
    173.     xxx = xxx+(l3+1)*l4;   
    174.     w_class = reshape(X (xxx+1: xxx+ (l4+1)*l5),l4+1,l5);   
    175.   end  
    176. %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    177.  end  
    178.  save mnistclassify_weights w1 w2 w3 w_class   
    179.  save mnistclassify_error test_err test_crerr train_err train_crerr;   
    180. end  
    181.     
    182.   
    183. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    184.  rbm.m   
    185. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    186. epsilonw      = 0.1;   % Learning rate for weights    
    187. epsilonvb     = 0.1;   % Learning rate for biases of visible units    
    188. epsilonhb     = 0.1;   % Learning rate for biases of hidden units    
    189. weightcost  = 0.0002;      
    190. initialmomentum  = 0.5;   
    191. finalmomentum    = 0.9;   
    192. [numcases numdims numbatches]=size(batchdata);   
    193. %%numcases  每批数据的个数   
    194. %%numdims   数据元组的维度   
    195. %%numbtches 数据批数   
    196. if restart ==1,   
    197.   restart=0;   
    198.   epoch=1;   
    199. % Initializing symmetric weights and biases. 初始化对称权值和偏置   
    200.   vishid     = 0.1*randn(numdims, numhid); %初始化生成可视层到隐藏层的权值   
    201.   hidbiases  = zeros(1,numhid);%隐藏单元的偏置值   
    202.   visbiases  = zeros(1,numdims);%可见单元的偏置值   
    203.   poshidprobs = zeros(numcases,numhid); %正向的隐藏单元概率生成   
    204.   neghidprobs = zeros(numcases,numhid);%反向的隐藏单元概率生成   
    205.   posprods    = zeros(numdims,numhid);%正向可见单元概率生成   
    206.   negprods    = zeros(numdims,numhid);%反向可见单元概率生成   
    207.   vishidinc  = zeros(numdims,numhid);%%%%%可视单元和隐藏单元之间的权值增量   
    208.   hidbiasinc = zeros(1,numhid);%%隐藏单元的偏置增量   
    209.   visbiasinc = zeros(1,numdims);%%可视单元的偏置增量   
    210.   batchposhidprobs=zeros(numcases,numhid,numbatches);%存储每次迭代计算好的每层的隐藏层概率,作为下一个RBM的输入   
    211. end  
    212. %%%%%%%%%%%%%%%%简单输出 迭代次数 处理的批次%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    213. for epoch = epoch:maxepoch,  %迭代处理   
    214.  fprintf(1,’epoch %d ’,epoch);    
    215.  errsum=0; %初始化输出错误为0  
    216.  for batch = 1:numbatches, %每次处理一批次的数据   
    217.  fprintf(1,’epoch %d batch %d ’,epoch,batch);    
    218. %%%%%%%%% START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    219.   data = batchdata(:,:,batch); %读取当前批次的全部数据vi   
    220.   poshidprobs = 1./(1 + exp(-data*vishid - repmat(hidbiases,numcases,1))); %计算前向传播的隐藏层概率hi   
    221.   batchposhidprobs(:,:,batch)=poshidprobs;%将计算好的概率赋值给当前批次前向传播的隐藏层最后一次计算好的值作为下一层的输入   
    222.   posprods    = data’ * poshidprobs;%contrastive divergence过程<vi,hi>   
    223.      
    224.   poshidact   = sum(poshidprobs);%average-wise隐藏层激活概率值   
    225.   posvisact = sum(data);%average-wise可视层激活概率值   
    226. %%%%%%%%% END OF POSITIVE PHASE  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    227.   poshidstates = poshidprobs > rand(numcases,numhid);%gibbs抽样,设定状态   
    228. %%%%%%%%% START NEGATIVE PHASE  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    229.   negdata = 1./(1 + exp(-poshidstates*vishid’ - repmat(visbiases,numcases,1)));%根据hi计算vi+1  
    230.   neghidprobs = 1./(1 + exp(-negdata*vishid - repmat(hidbiases,numcases,1)));   %根据vi+1计算hi+1  
    231.   negprods  = negdata’*neghidprobs;%contrastive divergence <vi+1,hi+1>   
    232.      
    233.   neghidact = sum(neghidprobs);   
    234.   negvisact = sum(negdata);    
    235. %%%%%%%%% END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    236.   err= sum(sum( (data-negdata).^2 )); %重新构建数据的方差   
    237.   errsum = err + errsum;%整体方差   
    238.    if epoch>5, %迭代次数不同调整冲量   
    239.      momentum=finalmomentum;   
    240.    else  
    241.      momentum=initialmomentum;   
    242.    end;   
    243. %%%%%%%%% UPDATE WEIGHTS AND BIASES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    244.     vishidinc = momentum*vishidinc + …   
    245.                 epsilonw*( (posprods-negprods)/numcases - weightcost*vishid);%权重增量计算   
    246.     visbiasinc = momentum*visbiasinc + (epsilonvb/numcases)*(posvisact-negvisact);%偏置增量计算   
    247.     hidbiasinc = momentum*hidbiasinc + (epsilonhb/numcases)*(poshidact-neghidact);%隐藏层增量计算   
    248.     vishid = vishid + vishidinc;   
    249.     visbiases = visbiases + visbiasinc;   
    250.     hidbiases = hidbiases + hidbiasinc;   
    251. %%%%%%%%%%%%%%%% END OF UPDATES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    252.   end  
    253.   fprintf(1, ’epoch %4i error %6.1f   ’, epoch, errsum);    
    254. end;   
    255.     
    256.   
    257. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    258. CG_CLASSIFY_INIT.M   
    259. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    260. function [f, df] = CG_CLASSIFY_INIT(VV,Dim,w3probs,target);%CG对最上面两层的训练   
    261. l1 = Dim(1);   
    262. l2 = Dim(2);   
    263. N = size(w3probs,1);    
    264. % Do decomversion.   
    265.   w_class = reshape(VV,l1+1,l2); %恢复权重,   
    266.   w3probs = [w3probs  ones(N,1)];  %一列,偏置   
    267.   targetout = exp(w3probs*w_class);  %计算label层的输出结果为numbercase*lablesnumber的矩阵   
    268.   targetout = targetout./repmat(sum(targetout,2),1,10); %选择最后的激活单元,见backpropclassify.m 的76行   
    269.   f = -sum(sum( target(:,1:end).*log(targetout))) ; %交叉熵  只采用了前边部分   
    270.      
    271. IO = (targetout-target(:,1:end));   % 输入和输出结果之间的差值   
    272. Ix_class=IO; %   
    273. dw_class =  w3probs’*Ix_class;%导数F(x)((1-F(x))乘以输出结果的偏差..其中F为sigmoid函数   
    274.     
    275. df = [dw_class(:)']‘;    
    276.     
    277.     
    278. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    279. CG_CLASSIFY.M   
    280. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    281. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    282. %   该段代码对所有权重进行整体微调   
    283. %   各部分过程见 CG_CLASSIFY_INIT.m注解   
    284. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    285. function [f, df] = CG_CLASSIFY(VV,Dim,XX,target);   
    286.   
    287. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    288. rbmhidlinear.m   
    289. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    290. %除了最后计算单元值采用的是线性单元其余过程全部一样   
    291. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

from: http://jacoxu.com/?p=692

原文地址:https://www.cnblogs.com/GarfieldEr007/p/5342779.html