基于标准反向传播算法的改进BP神经网络算法(Matlab代码实现)

简介: 基于标准反向传播算法的改进BP神经网络算法(Matlab代码实现)

文献来源,然后复现之:


💥1 概述

   SBP算法已成为用于训练多层感知器的标准算法,如图1所示。它是一种广义最小均方 (LMS) 算法,它最小化等于实际输出和所需输出之间误差平方和的标准。这个标准是:


详细文章下载链接:

https://ieeexplore.ieee.org/document/914537

📚2 运行结果

W1W1 =
   -0.1900   -0.7425   -2.9507
   -0.3955    0.2059   -0.8937
   -0.4751    0.5315   -1.2644
   -2.5390   -1.8319    3.2741
   -1.0816   -0.6534    0.7954
   -1.4622   -0.0331   -0.4283
   -0.3125    0.0840   -0.8470
   -0.6496   -0.2922   -0.6272
b1b1 =
    9.4243
    1.5431
    0.1860
    2.4630
    0.1097
    0.5390
    1.6335
   -0.4229
W2W2 =
   -1.6282    0.5796    0.2008    1.0366    0.9238   -0.3099    0.6122   -0.0681
b2b2 =
    0.3461
Mean Error Square at Iter = 2000eSq =
    0.0016
eSq_v =
    0.0022
eSq_t =
    0.0017
Trained
时间已过 1540.739160 秒。
No. of Iterations = 2001
Final Mean Squared Error at Iter = 2001eSq =
    0.0016
>>

部分代码:


%****Load the Input File******
  load ./nnm_train.txt
  redData = nnm_train(:,2);
  nir1Data = [nnm_train(:,3) ./ redData]';  
  nir2Data = [nnm_train(:,4) ./ redData]';
  nir3Data = [nnm_train(:,5) ./ redData]';
  pg = [ nir1Data; nir2Data; nir3Data];
  targetData = nnm_train(:,7) ;
 %*******Validate Data*******
 load ./nnm_validate.txt
  redData_v = nnm_validate(:,2);
  nir1Data_v = [nnm_validate(:,3) ./ redData_v]';  
  nir2Data_v = [nnm_validate(:,4) ./ redData_v]';
  nir3Data_v = [nnm_validate(:,5) ./ redData_v]';
  targetData_v = nnm_validate(:,7) ;
  pValidate = [nir1Data_v; nir2Data_v; nir3Data_v];
 %*******Test Data*******
 load ./nnm_test.txt
  redData_t = nnm_test(:,2);
  nir1Data_t = [nnm_test(:,3) ./ redData_t]';  
  nir2Data_t = [nnm_test(:,4) ./ redData_t]';
  nir3Data_t = [nnm_test(:,5) ./ redData_t]';
  targetData_t = nnm_test(:,7) ;
  pTest = [nir1Data_t; nir2Data_t; nir3Data_t];
%---Plot the Original Function----
    pa = 1 : length(targetData_t);
    actLine = 0:0.1:0.8; 
    subplot(2,1,2), plot (actLine, actLine); legend('Actual');%scatter(targetData_t, targetData_t,'^b');
    hold on
    %-----Randomized First Layer Weights & Bias-------
    fprintf( 'Initial Weights and Biases');
  %****3-8-1******
     W1 = [ -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand;  -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand;...
              -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand]'; %Uniform distribution [-0.5 0.5]
     b1 = [ -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand]';  
     W2 = [ -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand -0.5+rand];
  %-----Randomized Second Layer Bias------
     b2 = [ -0.5+rand ];
    if (lambda == 0) % Save the Weights and Bias on SBP
        W1_initial = W1;
        b1_initial = b1;
        W2_initial = W2;
        b2_initial = b2;
    else      % Reuse the Weights and Bias on MBP
        W1 = W1_initial;
        b1 = b1_initial;
        W2 = W2_initial;
        b2 = b2_initial;
    end
    %-----RandPermutation of Input Training Set-------
    j = randperm(length(targetData)); 
    j_v = randperm(length(targetData_v)); 
    j_t = randperm(length(targetData_t)); 
    %--Set Max. Iterations---
    maxIter = 2000;
    tic
    for train = 1 : maxIter +1   
      eSq = 0; eSq_v = 0; eSq_t = 0;
      % **** Mean Square Error ****
      %if ( train <= maxIter )
          for p = 1 : length(targetData)
             n1 = W1*pg(:,p)+ b1 ;
             a1 = logsig(n1); 
             a2 = poslin( W2 * a1  + b2 ); 
             e = targetData(p) - a2 ;
             eSq = eSq + (e^2);
          end
           eSq = eSq/length(targetData);
           %*******Validate Error**********
           for p = 1 : length(targetData_v)
             n1 = W1*pValidate(:,p)+ b1 ;
             a1 = logsig(n1); 
             a2 = poslin( W2 * a1  + b2 ); 
             e = targetData_v(p) - a2 ;
             eSq_v = eSq_v + (e^2);
           end
            eSq_v = eSq_v/length(targetData_v);
          %********Use Validate Error for Early Stopping********
           if ( train > 200 )
               earlyStopCount = earlyStopCount + 1;
              % fprintf('EarlyStop = %d', earlyStopCount);
               if (earlyStopCount == 50)
                       if ( (prev_eSq_v - eSq_v) < 0 ) 
                            W2 = W2_25;
                            b2 = b2_25;
                            W1 = W1_25;
                            b1 = b1_25;
                          break;
                       end
                  prev_eSq_v = eSq_v;   % Store previous validation error
                  earlyStopCount = 0;  % Reset Early Stopping
                  %----Save the weights and biases-------
                    disp('Saving'); eSq
                  W2_25 = W2;
                  b2_25 = b2;
                  W1_25 = W1;
                  b1_25 = b1;
               end
           else
                % ----Initialize the Weights----
              if ( train == 200 )
                  W2_25 = W2;
                  b2_25 = b2;
                  W1_25 = W1;
                  b1_25 = b1;
               end
               prev_eSq_v = eSq_v;   % Store previous validation error
           end
           %*******Test Error**********
           for p = 1 : length(targetData_t)
             n1 = W1*pTest(:,p)+ b1 ;
             a1 = logsig(n1); 
             a2 = poslin( W2 * a1  + b2 ); 
             e = targetData_t(p) - a2 ;
             eSq_t = eSq_t + (e^2);
           end
            eSq_t = eSq_t/length(targetData_t);
             if (train == 1 || mod (train, 100) == 0   )
               fprintf( 'Weights and Biases at Iter = %d\n',train);
                fprintf('W1');
                 (W1)
                fprintf('b1')
                 b1
                fprintf('W2')
                 W2
                fprintf('b2')
                 b2
               fprintf( 'Mean Error Square at Iter = %d',train);
               eSq
               eSq_v
               eSq_t
           end
           subplot(2,1,1), 
           xlabel('No. of Iterations');
           ylabel('Mean Square Error');
           title('Convergence Characteristics ');
           loglog(train, eSq, '*r'); hold on
           loglog(train, eSq_v, '*g'); hold on
           loglog(train, eSq_t, '*c'); hold on
           legend('Training Error', 'Validation Error', 'Testing Error');
           %************Train Data**********************
           %  Update only when the error is decreasing
      %    if ( earlyStopCount == 0 ) 
              for p = 1 : length(targetData)
                %----Output of the 1st Layer-----------
                n1 = W1*pg(:,j(p))+ b1 ;
                a1 = logsig(n1)  ; 
                %-----Output of the 2nd Layer----------
                n2 = W2 * a1  + b2;
                a2 =  (poslin( n2 )); 
                %a2 =  (logsig( n2 ));
                 %-----Error-----
                    t = targetData(j(p));
                    e = t - a2;
                 %******CALCULATE THE SENSITIVITIES************
                    %-----Derivative of logsig function----
                    %f1 = dlogsig(n1,a1)  
                   % f1 =  [(1-a1(1))*a1(1) 0; 0 (1-a1(2))*a1(2)] ; 
                    f1 = diag((1-a1).*a1); 
                    %-----Derivative of purelin function---
                    f2 = 1;
                    %f2 = diag((1-a2).*a2);
                    %------Last Layer (2nd) Sensitivity----
                    S2 = -2 * f2 * e;
                    S2mbp = ((t)-n2);
                    %------First Layer Sensitivity---------
                    S1 =   f1 *(W2' * S2);
                    S1mbp = f1 * (W2' * S2mbp);
                %******UPDATE THE WEIGHTS**********************
                    %-----Second Layer Weights & Bias------
                    W2 = W2 - (alpha * S2*(a1)') - (alpha * lambda * S2mbp *(a1)');
                    b2 = b2 - alpha * S2 - (alpha * lambda * S2mbp);
                    %-----First Layer Weights & Bias-------
                    W1 = W1 - alpha * S1*(pg(:,j(p)))' - (alpha * lambda * S1mbp *(pg(:,j(p)))');
                    b1 = b1 - alpha * S1 - (alpha * lambda * S1mbp );
               % end
              end
          %end 
          % End of 21 Input Training Sets
           % ********** Function Apporx. *****************
          if (train == 1 || mod (train, 100) == 0 || train == maxIter )
               disp('Trained');
               subplot(2,1,2), 
               xlabel('Actual Fraction of Weeds in 3 sq feet of grass area');
               ylabel('Estimated Fraction of Weeds in 3 sq feet of grass area');
               title('Correlation of Estimated Value with respect to the Actual Function using Standard Backpropagation');
               legend('Estimated');
            for p = 1 : length(targetData_t)
               n1 = W1*pTest(:,p)+ b1 ;       % Test Data
               a1 = logsig(n1)  ; 
               a2(p) =   (poslin( W2 * a1  + b2 )); 
            end %end for
            %scatter(targetData_t, a2); hold on;
          end 
    end
    toc
    %-------End of Iterations------------
    %***Plot of Final Function******
     subplot(2,1,2),
     for p = 1 : length(targetData_t)
               n1 = W1*pTest(:,p)+ b1 ;  % Test Data
               a1 = logsig(n1)  ; 
               a2(p) =   (poslin( W2 * a1  + b2 )); 
     end %end for


🎉3 参考文献

[1]S. Abid, F. Fnaiech and M. Najim, "A fast feedforward training algorithm using a modified form of the standard backpropagation algorithm," in IEEE Transactions on Neural Networks, vol. 12, no. 2, pp. 424-430, March 2001, doi: 10.1109/72.914537.


🌈4 Matlab代码实现

相关文章
|
3月前
|
传感器 机器学习/深度学习 算法
【UASNs、AUV】无人机自主水下传感网络中遗传算法的路径规划问题研究(Matlab代码实现)
【UASNs、AUV】无人机自主水下传感网络中遗传算法的路径规划问题研究(Matlab代码实现)
103 0
|
2月前
|
机器学习/深度学习 算法
采用蚁群算法对BP神经网络进行优化
使用蚁群算法来优化BP神经网络的权重和偏置,克服传统BP算法容易陷入局部极小值、收敛速度慢、对初始权重敏感等问题。
268 5
|
3月前
|
机器学习/深度学习 传感器 算法
【无人车路径跟踪】基于神经网络的数据驱动迭代学习控制(ILC)算法,用于具有未知模型和重复任务的非线性单输入单输出(SISO)离散时间系统的无人车的路径跟踪(Matlab代码实现)
【无人车路径跟踪】基于神经网络的数据驱动迭代学习控制(ILC)算法,用于具有未知模型和重复任务的非线性单输入单输出(SISO)离散时间系统的无人车的路径跟踪(Matlab代码实现)
208 2
|
2月前
|
机器学习/深度学习 数据采集 存储
概率神经网络的分类预测--基于PNN的变压器故障诊断(Matlab代码实现)
概率神经网络的分类预测--基于PNN的变压器故障诊断(Matlab代码实现)
273 0
|
2月前
|
机器学习/深度学习 人工智能 算法
【基于TTNRBO优化DBN回归预测】基于瞬态三角牛顿-拉夫逊优化算法(TTNRBO)优化深度信念网络(DBN)数据回归预测研究(Matlab代码实现)
【基于TTNRBO优化DBN回归预测】基于瞬态三角牛顿-拉夫逊优化算法(TTNRBO)优化深度信念网络(DBN)数据回归预测研究(Matlab代码实现)
120 0
|
3月前
|
机器学习/深度学习 并行计算 算法
【CPOBP-NSWOA】基于豪冠猪优化BP神经网络模型的多目标鲸鱼寻优算法研究(Matlab代码实现)
【CPOBP-NSWOA】基于豪冠猪优化BP神经网络模型的多目标鲸鱼寻优算法研究(Matlab代码实现)
|
3月前
|
机器学习/深度学习 缓存 算法
2025年华为杯A题|通用神经网络处理器下的核内调度问题研究生数学建模|思路、代码、论文|持续更新中....
2025年华为杯A题|通用神经网络处理器下的核内调度问题研究生数学建模|思路、代码、论文|持续更新中....
400 1
|
3月前
|
算法 数据挖掘 区块链
基于遗传算法的多式联运车辆路径网络优优化研究(Matlab代码实现)
基于遗传算法的多式联运车辆路径网络优优化研究(Matlab代码实现)
116 2
|
2月前
|
传感器 机器学习/深度学习 数据采集
【航空发动机寿命预测】基于SE-ResNet网络的发动机寿命预测,C-MAPSS航空发动机寿命预测研究(Matlab代码实现)
【航空发动机寿命预测】基于SE-ResNet网络的发动机寿命预测,C-MAPSS航空发动机寿命预测研究(Matlab代码实现)
160 0
|
2月前
|
机器学习/深度学习 算法 机器人
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
204 0