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

相关文章
|
11天前
|
机器学习/深度学习 算法 数据安全/隐私保护
基于生物地理算法的MLP多层感知机优化matlab仿真
本程序基于生物地理算法(BBO)优化MLP多层感知机,通过MATLAB2022A实现随机数据点的趋势预测,并输出优化收敛曲线。BBO模拟物种在地理空间上的迁移、竞争与适应过程,以优化MLP的权重和偏置参数,提升预测性能。完整程序无水印,适用于机器学习和数据预测任务。
|
1天前
|
算法 数据安全/隐私保护 异构计算
基于LSB最低有效位的音频水印嵌入提取算法FPGA实现,包含testbench和MATLAB对比
本项目展示了一种基于FPGA的音频水印算法,采用LSB(最低有效位)技术实现版权保护与数据追踪功能。使用Vivado2019.2和Matlab2022a开发,完整代码含中文注释及操作视频。算法通过修改音频采样点的最低有效位嵌入水印,人耳难以察觉变化。然而,面对滤波或压缩等攻击时,水印提取可能受影响。该项目运行效果无水印干扰,适合实时应用场景,核心逻辑简单高效,时间复杂度低。
|
1天前
|
算法 数据安全/隐私保护
基于GA遗传算法的拱桥静载试验车辆最优布载matlab仿真
本程序基于遗传算法(GA)实现拱桥静载试验车辆最优布载的MATLAB仿真,旨在自动化确定车辆位置以满足加载效率要求(0.95≤ηq≤1.05),目标是使ηq尽量接近1,同时减少车辆数量和布载耗时。程序在MATLAB 2022A版本下运行,展示了工况1至工况3的测试结果。通过优化模型,综合考虑车辆重量、位置、类型及车道占用等因素,确保桥梁关键部位承受最大荷载,从而有效评估桥梁性能。核心代码实现了迭代优化过程,并输出最优布载方案及相关参数。
|
6天前
|
机器学习/深度学习 存储 算法
基于MobileNet深度学习网络的活体人脸识别检测算法matlab仿真
本内容主要介绍一种基于MobileNet深度学习网络的活体人脸识别检测技术及MQAM调制类型识别方法。完整程序运行效果无水印,需使用Matlab2022a版本。核心代码包含详细中文注释与操作视频。理论概述中提到,传统人脸识别易受非活体攻击影响,而MobileNet通过轻量化的深度可分离卷积结构,在保证准确性的同时提升检测效率。活体人脸与非活体在纹理和光照上存在显著差异,MobileNet可有效提取人脸高级特征,为无线通信领域提供先进的调制类型识别方案。
|
5天前
|
算法 安全 数据安全/隐私保护
基于BBO生物地理优化的三维路径规划算法MATLAB仿真
本程序基于BBO生物地理优化算法,实现三维空间路径规划的MATLAB仿真(测试版本:MATLAB2022A)。通过起点与终点坐标输入,算法可生成避障最优路径,并输出优化收敛曲线。BBO算法将路径视为栖息地,利用迁移和变异操作迭代寻优。适应度函数综合路径长度与障碍物距离,确保路径最短且安全。程序运行结果完整、无水印,适用于科研与教学场景。
|
10天前
|
资源调度 算法 数据可视化
基于IEKF迭代扩展卡尔曼滤波算法的数据跟踪matlab仿真,对比EKF和UKF
本项目基于MATLAB2022A实现IEKF迭代扩展卡尔曼滤波算法的数据跟踪仿真,对比EKF和UKF的性能。通过仿真输出误差收敛曲线和误差协方差收敛曲线,展示三种滤波器的精度差异。核心程序包括数据处理、误差计算及可视化展示。IEKF通过多次迭代线性化过程,增强非线性处理能力;UKF避免线性化,使用sigma点直接处理非线性问题;EKF则通过一次线性化简化处理。
|
9天前
|
算法 数据安全/隐私保护 计算机视觉
基于sift变换的农田杂草匹配定位算法matlab仿真
本项目基于SIFT算法实现农田杂草精准识别与定位,运行环境为Matlab2022a。完整程序无水印,提供详细中文注释及操作视频。核心步骤包括尺度空间极值检测、关键点定位、方向分配和特征描述符生成。该算法通过特征匹配实现杂草定位,适用于现代农业中的自动化防控。
|
3天前
|
算法 数据可视化 调度
基于NSGAII的的柔性作业调度优化算法MATLAB仿真,仿真输出甘特图
本程序基于NSGA-II算法实现柔性作业调度优化,适用于多目标优化场景(如最小化完工时间、延期、机器负载及能耗)。核心代码完成任务分配与甘特图绘制,支持MATLAB 2022A运行。算法通过初始化种群、遗传操作和选择策略迭代优化调度方案,最终输出包含完工时间、延期、机器负载和能耗等关键指标的可视化结果,为制造业生产计划提供科学依据。
|
8天前
|
机器学习/深度学习 资源调度 算法
基于入侵野草算法的KNN分类优化matlab仿真
本程序基于入侵野草算法(IWO)优化KNN分类器,通过模拟自然界中野草的扩散与竞争过程,寻找最优特征组合和超参数。核心步骤包括初始化、繁殖、变异和选择,以提升KNN分类效果。程序在MATLAB2022A上运行,展示了优化后的分类性能。该方法适用于高维数据和复杂分类任务,显著提高了分类准确性。
|
5天前
|
机器学习/深度学习 数据采集 算法
基于yolov2和googlenet网络的疲劳驾驶检测算法matlab仿真
本内容展示了基于深度学习的疲劳驾驶检测算法,包括算法运行效果预览(无水印)、Matlab 2022a 软件版本说明、部分核心程序(完整版含中文注释与操作视频)。理论部分详细阐述了疲劳检测原理,通过对比疲劳与正常状态下的特征差异,结合深度学习模型提取驾驶员面部特征变化。具体流程包括数据收集、预处理、模型训练与评估,使用数学公式描述损失函数和推理过程。课题基于 YOLOv2 和 GoogleNet,先用 YOLOv2 定位驾驶员面部区域,再由 GoogleNet 分析特征判断疲劳状态,提供高准确率与鲁棒性的检测方法。

热门文章

最新文章