基于标准反向传播算法的改进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代码实现

相关文章
|
1天前
|
传感器 算法
基于GA遗传算法的多机无源定位系统GDOP优化matlab仿真
本项目基于遗传算法(GA)优化多机无源定位系统的GDOP,使用MATLAB2022A进行仿真。通过遗传算法的选择、交叉和变异操作,迭代优化传感器配置,最小化GDOP值,提高定位精度。仿真输出包括GDOP优化结果、遗传算法收敛曲线及三维空间坐标点分布图。核心程序实现了染色体编码、适应度评估、遗传操作等关键步骤,最终展示优化后的传感器布局及其性能。
|
2天前
|
机器学习/深度学习 算法 安全
基于深度学习的路面裂缝检测算法matlab仿真
本项目基于YOLOv2算法实现高效的路面裂缝检测,使用Matlab 2022a开发。完整程序运行效果无水印,核心代码配有详细中文注释及操作视频。通过深度学习技术,将目标检测转化为回归问题,直接预测裂缝位置和类别,大幅提升检测效率与准确性。适用于实时检测任务,确保道路安全维护。 简介涵盖了算法理论、数据集准备、网络训练及检测过程,采用Darknet-19卷积神经网络结构,结合随机梯度下降算法进行训练。
|
3天前
|
算法 数据可视化 数据安全/隐私保护
一级倒立摆平衡控制系统MATLAB仿真,可显示倒立摆平衡动画,对比极点配置,线性二次型,PID,PI及PD五种算法
本课题基于MATLAB对一级倒立摆控制系统进行升级仿真,增加了PI、PD控制器,并对比了极点配置、线性二次型、PID、PI及PD五种算法的控制效果。通过GUI界面显示倒立摆动画和控制输出曲线,展示了不同控制器在偏转角和小车位移变化上的性能差异。理论部分介绍了倒立摆系统的力学模型,包括小车和杆的动力学方程。核心程序实现了不同控制算法的选择与仿真结果的可视化。
31 15
|
3天前
|
算法
基于SOA海鸥优化算法的三维曲面最高点搜索matlab仿真
本程序基于海鸥优化算法(SOA)进行三维曲面最高点搜索的MATLAB仿真,输出收敛曲线和搜索结果。使用MATLAB2022A版本运行,核心代码实现种群初始化、适应度计算、交叉变异等操作。SOA模拟海鸥觅食行为,通过搜索飞行、跟随飞行和掠食飞行三种策略高效探索解空间,找到全局最优解。
|
4天前
|
算法 数据安全/隐私保护 计算机视觉
基于FPGA的图像双线性插值算法verilog实现,包括tb测试文件和MATLAB辅助验证
本项目展示了256×256图像通过双线性插值放大至512×512的效果,无水印展示。使用Matlab 2022a和Vivado 2019.2开发,提供完整代码及详细中文注释、操作视频。核心程序实现图像缩放,并在Matlab中验证效果。双线性插值算法通过FPGA高效实现图像缩放,确保质量。
|
5天前
|
机器学习/深度学习 数据采集 算法
基于GWO灰狼优化的CNN-GRU-SAM网络时间序列回归预测算法matlab仿真
本项目基于MATLAB2022a,展示了时间序列预测算法的运行效果(无水印)。核心程序包含详细中文注释和操作视频。算法采用CNN-GRU-SAM网络,结合灰狼优化(GWO),通过卷积层提取局部特征、GRU处理长期依赖、自注意力机制捕捉全局特征,最终实现复杂非线性时间序列的高效预测。
|
5天前
|
传感器 算法 物联网
基于粒子群算法的网络最优节点部署优化matlab仿真
本项目基于粒子群优化(PSO)算法,实现WSN网络节点的最优部署,以最大化节点覆盖范围。使用MATLAB2022A进行开发与测试,展示了优化后的节点分布及其覆盖范围。核心代码通过定义目标函数和约束条件,利用PSO算法迭代搜索最佳节点位置,并绘制优化结果图。PSO算法灵感源于鸟群觅食行为,适用于连续和离散空间的优化问题,在通信网络、物联网等领域有广泛应用。该算法通过模拟粒子群体智慧,高效逼近最优解,提升网络性能。
|
1月前
|
算法 数据安全/隐私保护 计算机视觉
基于Retinex算法的图像去雾matlab仿真
本项目展示了基于Retinex算法的图像去雾技术。完整程序运行效果无水印,使用Matlab2022a开发。核心代码包含详细中文注释和操作步骤视频。Retinex理论由Edwin Land提出,旨在分离图像的光照和反射分量,增强图像对比度、颜色和细节,尤其在雾天条件下表现优异,有效解决图像去雾问题。
|
1月前
|
算法 数据可视化 安全
基于DWA优化算法的机器人路径规划matlab仿真
本项目基于DWA优化算法实现机器人路径规划的MATLAB仿真,适用于动态环境下的自主导航。使用MATLAB2022A版本运行,展示路径规划和预测结果。核心代码通过散点图和轨迹图可视化路径点及预测路径。DWA算法通过定义速度空间、采样候选动作并评估其优劣(目标方向性、障碍物距离、速度一致性),实时调整机器人运动参数,确保安全避障并接近目标。
148 68
|
1月前
|
机器学习/深度学习 算法 计算机视觉
基于CNN卷积神经网络的金融数据预测matlab仿真,对比BP,RBF,LSTM
本项目基于MATLAB2022A,利用CNN卷积神经网络对金融数据进行预测,并与BP、RBF和LSTM网络对比。核心程序通过处理历史价格数据,训练并测试各模型,展示预测结果及误差分析。CNN通过卷积层捕捉局部特征,BP网络学习非线性映射,RBF网络进行局部逼近,LSTM解决长序列预测中的梯度问题。实验结果表明各模型在金融数据预测中的表现差异。
125 10

热门文章

最新文章