多层神经网络

来源:互联网 发布:c语言中memset函数 编辑:程序博客网 时间:2024/04/28 23:19

多层神经网络

分类: 图像音频 104人阅读 评论(0) 收藏 举报
神经网络

本文简单整理自《模式分类》第二版的第六章,先上一张图,描述了三层神经网络的基本概念(图片看不清的请在图片上“右键》新标签页中打开”)。


多层神经网络的理论基础参见《模式分类》第六章,这里没有做相关讨论。下面将简单分析一个stochasic backpropagation的matlab代码

[plain] view plaincopyprint?
  1. function [test_targets, Wh, Wo, J] = Backpropagation_Stochastic(train_patterns, train_targets, test_patterns, params)  
  2.   
  3. % Classify using a backpropagation network with stochastic learning algorithm  
  4. % Inputs:  
  5. %   training_patterns   - Train patterns  
  6. %   training_targets    - Train targets  
  7. %   test_patterns       - Test  patterns  
  8. %   params              - Number of hidden units, Convergence criterion, Convergence rate  
  9. %  
  10. % Outputs  
  11. %   test_targets        - Predicted targets  
  12. %   Wh                  - Hidden unit weights  
  13. %   Wo                  - Output unit weights  
  14. %   J                   - Error throughout the training  
  15.   
  16. [Nh, Theta, eta] = process_params(params);  
  17. iter             = 1;  
  18.   
  19. [Ni, M]          = size(train_patterns);  
  20. No               = 1;  
  21.   
  22. Uc               = length(unique(train_targets));  
  23. %If there are only two classes, remap to {-1,1}  
  24. if (Uc == 2)  
  25.     train_targets    = (train_targets>0)*2-1;  
  26. end  
  27.   
  28. %Initialize the net: In this implementation there is only one output unit, so there  
  29. %will be a weight vector from the hidden units to the output units, and a weight matrix  
  30. %from the input units to the hidden units.  
  31. %The matrices are defined with one more weight so that there will be a bias  
  32. w0      = max(abs(std(train_patterns')'));  
  33. Wh      = rand(Nh, Ni+1).*w0*2-w0; %Hidden weights  
  34. Wo      = rand(No, Nh+1).*w0*2-w0; %Output weights  
  35.   
  36. Wo    = Wo/mean(std(Wo'))*(Nh+1)^(-0.5);  
  37. Wh    = Wh/mean(std(Wh'))*(Ni+1)^(-0.5);  
  38.   
  39. rate    = 10*Theta;  
  40. J(1)    = 1e3;  
  41.   
  42. while (rate > Theta),  
  43.     %Randomally choose an example  
  44.     i   = randperm(M);  
  45.     m   = i(1);  
  46.     Xm = train_patterns(:,m);  
  47.     tk = train_targets(m);  
  48.       
  49.     %Forward propagate the input:  
  50.     %First to the hidden units  
  51.     gh              = Wh*[Xm; 1];  
  52.     [y, dfh]        = activation(gh);  
  53.     %Now to the output unit  
  54.     go              = Wo*[y; 1];  
  55.     [zk, dfo]   = activation(go);  
  56.       
  57.     %Now, evaluate delta_k at the output: delta_k = (tk-zk)*f'(net)  
  58.     delta_k     = (tk - zk).*dfo;  
  59.       
  60.     %...and delta_j: delta_j = f'(net)*w_j*delta_k  
  61.     delta_j     = dfh'.*Wo(1:end-1).*delta_k;  
  62.       
  63.     %w_kj <- w_kj + eta*delta_k*y_j  
  64.     Wo              = Wo + eta*delta_k*[y;1]';  
  65.       
  66.     %w_ji <- w_ji + eta*delta_j*[Xm;1]  
  67.     Wh              = Wh + eta*delta_j'*[Xm;1]';  
  68.       
  69.     iter            = iter + 1;  
  70.   
  71.     %Calculate total error  
  72.     J(iter)    = 0;  
  73.     for i = 1:M,  
  74.         J(iter) = J(iter) + (train_targets(i) - activation(Wo*[activation(Wh*[train_patterns(:,i); 1]); 1])).^2;  
  75.     end  
  76.     J(iter) = J(iter)/M;   
  77.     rate  = abs(J(iter) - J(iter-1))/J(iter-1)*100;  
  78.       
  79.     if (iter/100 == floor(iter/100)),  
  80.         disp(['Iteration ' num2str(iter) ': Total error is ' num2str(J(iter))])  
  81.     end  
  82.       
  83. end  
  84.   
  85. disp(['Backpropagation converged after ' num2str(iter) ' iterations.'])  
  86.   
  87. %Classify the test patterns  
  88. test_targets = zeros(1, size(test_patterns,2));  
  89. for i = 1:size(test_patterns,2),  
  90.     test_targets(i) = activation(Wo*[activation(Wh*[test_patterns(:,i); 1]); 1]);  
  91. end  
  92.   
  93. if (Uc == 2)  
  94.     test_targets  = test_targets >0;  
  95. end  
  96.   
  97.   
  98.   
  99. function [f, df] = activation(x)  
  100.   
  101. a = 1.716;  
  102. b = 2/3;  
  103. f   = a*tanh(b*x);  
  104. df  = a*b*sech(b*x).^2;  

算法本身是梯度下降算法的一种扩展。迭代地按一定规则逐步更新w值使算法达到局部最优,w更新的规则是

w(m+1) = w(m) + Δw(m)

因为是三层网络,所以要对Wkj和Wji分别进行更新,这就是

[plain] view plaincopyprint?
  1. <span style="font-size:14px;">    Wo                = Wo + eta*delta_k*[y;1]';  
  2.     Wh              = Wh + eta*delta_j'*[Xm;1]';</span>  
代码中的
[plain] view plaincopyprint?
  1. <span style="font-size:14px;">[f, df] = activation(x)</span>  

实现上图中提到的activation函数,f为节点输出端的值,df为f(net)的差分即f'(net).

我们没对

[plain] view plaincopyprint?
  1. Nh, Theta, eta  

这三个参数进行特定的选择,默认依次为5, 0.1, 0.1,表示隐节点个数为5,dJ<0.1时结束循环,算法中的η更新速度为0.1,使用其的分了结果如下图,由此可知效果不是很好。


用于对比的SVM效果如下,SVM的分类效果很好。


以上只是最简单的神经网络的一种训练方式,要获得好的效果还需要做大量的改进。

SVM的出现比神经网络晚3~4年,SVM的出现就是为了与神经网络竞争而产生的,2006年,神经网络一族为了打败SVM,提出了深度学习(Deep Learning)算法,最近这个算法非常火,有机器学习志向的应该好好研究。


Refrences:

[1] To C. A. Rosen and C. W. Stork, patten classfication, edition 2.

原创粉丝点击