点击上方蓝字关注我们
数据集
用经典鲍鱼数据集为例,最后Rings是需要预测的即鲍鱼的年龄,用性别(1:雄性,M;0:中性l ; -1:雌性,F)和一些体征如长度、高度、重量等进行预测。因变量是鲍鱼的年龄,有多个自变量,是一个典型多元回归问题。
鲍鱼数据形式如下:
目标函数
对于一个固定结构的神经网络,即神经网络层数、神经元个数以及激活函数都一样的情况下,可以通过优化网络结构的初始参数来优化整个网络的参数和结构。
把优化的参数拉平成一条进行优化,优化参数的维度和网络结构有关,我们的优化目标就是优化出一个初始网络结构超参数,能让网络正向传播得到的预测值和真实值最接近。因此我们通用的目标函数可以设置如下:
function fitness_value =objfun(input_pop)
global input_num hidden_num output_num input_data output_data
w1=input_pop(1:input_num*hidden_num); %输入和隐藏层之间的权重参数
B1=input_pop(input_num*hidden_num+1:input_num*hidden_num+hidden_num); %隐藏层神经元的偏置
w2=input_pop(input_num*hidden_num+hidden_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num); %隐藏层和输出层之间的偏置
B2=input_pop(input_num*hidden_num+hidden_num+hidden_num*output_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num+output_num); %输出层神经元的偏置
%网络权值赋值
W1=reshape(w1,hidden_num,input_num);
W2=reshape(w2,output_num,hidden_num);
B1=reshape(B1,hidden_num,1);
B2=reshape(B2,output_num,1);
[~,n]=size(input_data);
A1=logsig(W1*input_data+repmat(B1,1,n)); %需与main函数中激活函数相同
A2=purelin(W2*A1+repmat(B2,1,n)); %需与main函数中激活函数相同
error=sumsqr(output_data-A2);
fitness_value=error; %误差即为适应度
end
数据集划分与超参数设置
clc;clear; close all;
load('abalone_data.mat')%鲍鱼数据
global input_num hidden_num output_num input_data output_data train_num test_num x_train_mu y_train_mu x_train_sigma y_train_sigma
%% 导入数据
%设置训练数据和测试数据
[m,n]=size(data);
train_num=round(0.8*m); %自变量
test_num=m-train_num;
x_train_data=data(1:train_num,1:n-1);
y_train_data=data(1:train_num,n);
%测试数据
x_test_data=data(train_num+1:end,1:n-1);
y_test_data=data(train_num+1:end,n);
%% 标准化
[x_train_regular,x_train_mu,x_train_sigma] = zscore(x_train_data);
[y_train_regular,y_train_mu,y_train_sigma]= zscore(y_train_data);
x_train_regular=x_train_regular';
y_train_regular=y_train_regular';
input_data=x_train_regular;
output_data=y_train_regular;
input_num=size(x_train_regular,1); %输入特征个数
hidden_num=6; %隐藏层神经元个数
output_num=size(y_train_regular,1); %输出特征个数
num_all=input_num*hidden_num+hidden_num+hidden_num*output_num+output_num;%网络总参数,只含一层隐藏层;
%%
%自变量的个数即为网络连接权重以及偏置
popmax=1.5; %自变量即网络权重和偏置的上限
popmin=-1.5; %自变量即网络权重和偏置的下限
SearchAgents_no=50; % Number of search agents 搜索麻雀数量
Max_iteration=300; % Maximum numbef of iterations 最大迭代次数
% Load details of the selected benchmark function
超参数的优化和最终结果的获取
以下展示了,使用智能优化算法优化超参数前后的使用方法,在初始超参数优化完毕后,直接把结果带入到神经网络中,再进行反向传播的训练,
%%
fobj=@objfun;
Time_compare=[]; %算法的运行时间比较
Fival_compare=[]; %算法的最终目标比较
Fival_compare1=[]; %优化过后的初始参数经过反向传播的优化
Fival_compare2=[];
curve_compare=[]; %算法的过程函数比较
name_all=[]; %算法的名称记录
dim=num_all;
lb=popmin;
ub=popmax;
pop_num=SearchAgents_no;
Max_iter=Max_iteration;
%% 不进行优化,随机赋值
iter=1;
bestX=lb+(ub-lb).*rand(1,num_all);
ER_=Fun1(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
ER_1=Fun2(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
Fival_compare1=[Fival_compare1,ER_];
Fival_compare2=[Fival_compare2,ER_1];
name_all{1,iter}='NO-opti';
iter=iter+1;
%% 麻雀搜索算法
t1=clock;
[fMin_SSA,bestX_SSA,SSA_curve]=SSA(pop_num,Max_iter,lb,ub,dim,fobj); %麻雀搜索算法
ER_SSA=Fun1(bestX_SSA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
ER_SSA1=Fun2(bestX_SSA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
t2=clock;
time_SSA=(t2(end)+t2(end-1)*60+t2(end-2)*3600-t1(end)-t1(end-1)*60-t1(end-2)*3600);
Fival_compare=[Fival_compare,fMin_SSA];
Fival_compare1=[Fival_compare1,ER_SSA];
Fival_compare2=[Fival_compare2,ER_SSA1];
Time_compare=[Time_compare,time_SSA(end)];
curve_compare=[curve_compare;SSA_curve];
name_all{1,iter}='SSA';
iter=iter+1;
第一个神经网络计算函数是MATLAB自带的BP工具箱
function [EcRMSE]=Fun1(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data)
global input_num output_num x_train_mu y_train_mu x_train_sigma y_train_sigma
bestchrom=bestX;
net=newff(x_train_regular,y_train_regular,hidden_num,{'logsig','purelin'},'trainlm');
w1=bestchrom(1:input_num*hidden_num); %输入和隐藏层之间的权重参数
B1=bestchrom(input_num*hidden_num+1:input_num*hidden_num+hidden_num); %隐藏层神经元的偏置
w2=bestchrom(input_num*hidden_num+hidden_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num); %隐藏层和输出层之间的偏置
B2=bestchrom(input_num*hidden_num+hidden_num+hidden_num*output_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num+output_num); %输出层神经元的偏置
%网络权值赋值
net.iw{1,1}=reshape(w1,hidden_num,input_num);
net.lw{2,1}=reshape(w2,output_num,hidden_num);
net.b{1}=reshape(B1,hidden_num,1);
net.b{2}=reshape(B2,output_num,1);
net.trainParam.epochs=200; %最大迭代次数
net.trainParam.lr=0.1; %学习率
net.trainParam.goal=0.00001;
[net,~]=train(net,x_train_regular,y_train_regular);
%将输入数据归一化
test_num=size(x_test_data,1);
x_test_regular = (x_test_data-repmat(x_train_mu,test_num,1))./repmat(x_train_sigma,test_num,1);
%放入到网络输出数据
y_test_regular=sim(net,x_test_regular');
%将得到的数据反归一化得到预测数据
test_out_std=y_test_regular;
%反归一化
SSA_BP_predict=test_out_std*y_train_sigma+y_train_mu;
errors_nn=sum(abs(SSA_BP_predict'-y_test_data)./(y_test_data))/length(y_test_data);
EcRMSE=sqrt(sum((errors_nn).^2)/length(errors_nn));
% disp(EcRMSE)
end
第二个神经网络计算函数是小编自己写的BP函数
function [EcRMSE]=Fun2(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data)
global input_num output_num x_train_mu y_train_mu x_train_sigma y_train_sigma
train_num=length(y_train_regular); %自变量
test_num=length(y_test_data);
bestchrom=bestX;
% net=newff(x_train_regular,y_train_regular,hidden_num,{'tansig','purelin'},'trainlm');
w1=bestchrom(1:input_num*hidden_num); %输入和隐藏层之间的权重参数
B1=bestchrom(input_num*hidden_num+1:input_num*hidden_num+hidden_num); %隐藏层神经元的偏置
w2=bestchrom(input_num*hidden_num+hidden_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num); %隐藏层和输出层之间的偏置
B2=bestchrom(input_num*hidden_num+hidden_num+hidden_num*output_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num+output_num); %输出层神经元的偏置
%网络权值赋值
x_train_std=x_train_regular;
y_train_std=y_train_regular;
%
vij = reshape(w1,hidden_num,input_num) ;%输入和隐藏层的权重
theta_u = reshape(B1,hidden_num,1);%输入与隐藏层之间的阈值
Wj = reshape(w2,output_num,hidden_num);%%输出和隐藏层的权重
theta_y =reshape(B2,output_num,1);%输出与隐藏层之间的阈值
%
learn_rate = 0.0001;%学习率
Epochs_max = 10000;%最大迭代次数
error_rate = 0.1;%目标误差
Obj_save = zeros(1,Epochs_max);%损失函数
% 训练网络
epoch_num=0;
while epoch_num <Epochs_max
epoch_num=epoch_num+1;
y_pre_std_u=vij * x_train_std + repmat(theta_u, 1, train_num);
y_pre_std_u1 = logsig(y_pre_std_u);
y_pre_std_y = Wj * y_pre_std_u1 + repmat(theta_y, 1, train_num);
y_pre_std_y1=y_pre_std_y;
obj = y_pre_std_y1-y_train_std ;
Ems = sumsqr(obj);
Obj_save(epoch_num) = Ems;
if Ems < error_rate
break;
end
%梯度下降
%输出采用rule函数,隐藏层采用sigomd激活函数
c_wj= 2*(y_pre_std_y1-y_train_std)* y_pre_std_u1';
c_theta_y=2*(y_pre_std_y1-y_train_std)*ones(train_num, 1);
c_vij=Wj'* 2*(y_pre_std_y1-y_train_std).*(y_pre_std_u1).*(1-y_pre_std_u1)* x_train_std';
c_theta_u=Wj'* 2*(y_pre_std_y1-y_train_std).*(y_pre_std_u1).*(1-y_pre_std_u1)* ones(train_num, 1);
Wj=Wj-learn_rate*c_wj;
theta_y=theta_y-learn_rate*c_theta_y;
vij=vij- learn_rate*c_vij;
theta_u=theta_u-learn_rate*c_theta_u;
end
%
x_test_regular = (x_test_data-repmat(x_train_mu,test_num,1))./repmat(x_train_sigma,test_num,1);
% x_test_regular = mapminmax('apply',x_test_data,x_train_maxmin);
%放入到网络输出数据
x_test_std=x_test_regular';
test_put = logsig(vij * x_test_std + repmat(theta_u, 1, test_num));
test_out_std = Wj * test_put + repmat(theta_y, 1, test_num);
%反归一化
SSA_BP_predict=test_out_std*y_train_sigma+y_train_mu;
errors_nn=sum(abs(SSA_BP_predict'-y_test_data)./(y_test_data))/length(y_test_data);
EcRMSE=sqrt(sum((errors_nn).^2)/length(errors_nn));
disp(EcRMSE)
end
得到不同优化函数优化BP神经网络的适应度曲线如下
将初始参数带入到BP网络模型中,反向传播训练可以得到BP工具箱优化的结果以及自己写的网络结果,两者效果相差不太大
以下是两种实现方式的对比
运行多次记录结果
不可否认的是,因为智能优化算法的存在,每次优化是有一定的随机性的,因此我们可以多次运行取均值和方差去衡量总体的结果
clc;clear; close all;
load('data_test1.mat')
global input_num hidden_num output_num input_data output_data train_num test_num x_train_mu y_train_mu x_train_sigma y_train_sigma
%% 循环5次记录结果 训练集和测试集随机
%% 导入数据
%设置训练数据和测试数据
NUM=5; %随机测试数
for NN=1:NUM
[m,n]=size(data);
train_num=round(0.8*m); %自变量
randlabel=randperm(m); %随机标签
test_num=m-train_num;
x_train_data=data(randlabel(1:train_num),1:n-1);
y_train_data=data(randlabel(1:train_num),n);
%测试数据
x_test_data=data(randlabel(train_num+1:end),1:n-1);
y_test_data=data(randlabel(train_num+1:end),n);
% x_train_data=x_train_data';
% y_train_data=y_train_data';
% x_test_data=x_test_data';
%% 标准化
[x_train_regular,x_train_mu,x_train_sigma] = zscore(x_train_data);
[y_train_regular,y_train_mu,y_train_sigma]= zscore(y_train_data);
x_train_regular=x_train_regular';
y_train_regular=y_train_regular';
input_data=x_train_regular;
output_data=y_train_regular;
input_num=size(x_train_regular,1); %输入特征个数
hidden_num=6; %隐藏层神经元个数
output_num=size(y_train_regular,1); %输出特征个数
num_all=input_num*hidden_num+hidden_num+hidden_num*output_num+output_num;%网络总参数,只含一层隐藏层;
%自变量的个数即为网络连接权重以及偏置
popmax=1.5; %自变量即网络权重和偏置的上限
popmin=-1.5; %自变量即网络权重和偏置的下限
SearchAgents_no=50; % Number of search agents 搜索麻雀数量
Max_iteration=300; % Maximum numbef of iterations 最大迭代次数
% Load details of the selected benchmark function
%%
fobj=@objfun;
Time_compare=[]; %算法的运行时间比较
Fival_compare=[]; %算法的最终目标比较
Fival_compare1=[]; %优化过后的初始参数经过反向传播的优化
Fival_compare2=[];
curve_compare=[]; %算法的过程函数比较
name_all=[]; %算法的名称记录
dim=num_all;
lb=popmax*ones(1,dim);
ub=popmin*ones(1,dim);
pop_num=SearchAgents_no;
Max_iter=Max_iteration;
%% 不进行优化,随机赋值
iter=1;
bestX=lb+(ub-lb).*rand(1,num_all);
ER_=Fun1(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
ER_1=Fun2(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
Fival_compare1=[Fival_compare1,ER_];
Fival_compare2=[Fival_compare2,ER_1];
name_all{1,iter}='ON-opti';
iter=iter+1;
%%
% [ER_1,WW]=Fun2(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
% % [fMin_SSA,bestX_SSA,SSA_curve]=SSA2(pop_num,pop_or,Max_iter,lb,ub,dim,fobj);
% % ER2=fun3(bestX_SSA,x_test_data,x_train_mu,x_train_sigma,y_train_mu,y_train_sigma,y_test_data);
% pop_num11=500;
% [fMin_SSA,bestX_SSA1,SSA_curve]=SSA2(pop_num11,WW,Max_iter,lb,ub,dim,fobj);
% ER_2=fun3(bestX_SSA1,x_test_data,x_train_mu,x_train_sigma,y_train_mu,y_train_sigma,y_test_data);
% Fival_compare1=[Fival_compare1,ER_2];
% Fival_compare2=[Fival_compare2,ER_2];
% name_all{1,iter}='BP-SSA';
% iter=iter+1;
%% 改进麻雀搜索算法
t1=clock;
[fMin_SSA,bestX_SSA,SSA_curve]=G_SSA(pop_num,Max_iter,lb,ub,dim,fobj); %麻雀搜索算法
ER_SSA=Fun1(bestX_SSA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
ER_SSA1=Fun2(bestX_SSA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
t2=clock;
time_SSA=(t2(end)+t2(end-1)*60+t2(end-2)*3600-t1(end)-t1(end-1)*60-t1(end-2)*3600);
ER_SSA2=fun3(bestX_SSA,x_test_data,x_train_mu,x_train_sigma,y_train_mu,y_train_sigma,y_test_data); %不进行BP反向
Fival_compare=[Fival_compare,ER_SSA2];
Fival_compare1=[Fival_compare1,ER_SSA];
Fival_compare2=[Fival_compare2,ER_SSA1];
Time_compare=[Time_compare,time_SSA(end)];
curve_compare=[curve_compare;SSA_curve];
name_all{1,iter}='G-SSA';
iter=iter+1;
%%
%改进鲸鱼优化算法
t1=clock;
[fMin_EWOA,bestX_EWOA,EWOA_curve]=BKA(pop_num,Max_iter,lb,ub,dim,fobj);
ER_EWOA=Fun1(bestX_EWOA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
ER_EWOA1=Fun2(bestX_EWOA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
t2=clock;
time_EWOA=(t2(end)+t2(end-1)*60+t2(end-2)*3600-t1(end)-t1(end-1)*60-t1(end-2)*3600);
ER_EWOA2=fun3(bestX_EWOA,x_test_data,x_train_mu,x_train_sigma,y_train_mu,y_train_sigma,y_test_data); %不进行BP反向
Fival_compare=[Fival_compare,ER_EWOA2];
Fival_compare1=[Fival_compare1,ER_EWOA];
Fival_compare2=[Fival_compare2,ER_EWOA1];
Time_compare=[Time_compare,time_EWOA(end)];
curve_compare=[curve_compare;EWOA_curve];
name_all{1,iter}='BKA';
iter=iter+1;
%%
%正弦余弦优化算法 Sine Cosine Algorithm
t1=clock;
[fMin_SCA,bestX_SCA,SCA_curve]=SCA(pop_num,Max_iter,lb,ub,dim,fobj);
ER_SCA=Fun1(bestX_SCA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
ER_SCA1=Fun2(bestX_SCA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
t2=clock;
time_SCA=(t2(end)+t2(end-1)*60+t2(end-2)*3600-t1(end)-t1(end-1)*60-t1(end-2)*3600);
ER_SCA2=fun3(bestX_EWOA,x_test_data,x_train_mu,x_train_sigma,y_train_mu,y_train_sigma,y_test_data); %不进行BP反向
Fival_compare=[Fival_compare,ER_SCA2];
Fival_compare1=[Fival_compare1,ER_SCA];
Fival_compare2=[Fival_compare2,ER_SCA1];
Time_compare=[Time_compare,time_SCA(end)];
curve_compare=[curve_compare;SCA_curve];
name_all{1,iter}='SCA';
iter=iter+1;
%%
%POA
%IGOA
%IGWO
t1=clock;
[fMin_SCA,bestX_SCA,SCA_curve]=G_DBO(pop_num,Max_iter,lb,ub,dim,fobj);
ER_SCA=Fun1(bestX_SCA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
ER_SCA1=Fun2(bestX_SCA,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data);
t2=clock;
ER_SCA2=fun3(bestX_EWOA,x_test_data,x_train_mu,x_train_sigma,y_train_mu,y_train_sigma,y_test_data); %不进行BP反向
time_SCA=(t2(end)+t2(end-1)*60+t2(end-2)*3600-t1(end)-t1(end-1)*60-t1(end-2)*3600);
Fival_compare=[Fival_compare,ER_SCA2];
Fival_compare1=[Fival_compare1,ER_SCA];
Fival_compare2=[Fival_compare2,ER_SCA1];
Time_compare=[Time_compare,time_SCA(end)];
curve_compare=[curve_compare;SCA_curve];
name_all{1,iter}='G-DBO';
iter=iter+1;
FFival_compare1(NN,:)=Fival_compare1;
FFival_compare2(NN,:)=Fival_compare2;
end
%%
load('color_list.mat')
figure(3)
color=color_list(randperm(length(color_list)),:);
width=0.7; %柱状图宽度
for i=1:length(Fival_compare1)
set(bar(i,Fival_compare1(i),width),'FaceColor',color(i,:),'EdgeColor',[0,0,0],'LineWidth',2)
hold on
%在柱状图 x,y 基础上 绘制误差 ,low为下误差,high为上误差,LineStyle 误差图样式,'Color' 误差图颜色
% 'LineWidth', 线宽,'CapSize',误差标注大小
% errorbar(i, y(i), low(i), high(i), 'LineStyle', 'none', 'Color', color(i+3,:), 'LineWidth', 1.5,'CapSize',18);
end
ylabel('obj-value')
ylim([min(Fival_compare1)-0.01,max(Fival_compare1)+0.01]);
ax=gca;
ax.XTick = 1:1:length(Fival_compare1);
set(gca,'XTickLabel',name_all,"LineWidth",2);
set(gca,"FontSize",12,"LineWidth",2)
title('优化工具箱')
%%
load('color_list.mat')
figure(4)
color=color_list(randperm(length(color_list)),:);
width=0.7; %柱状图宽度
for i=1:length(Fival_compare2)
set(bar(i,Fival_compare2(i),width),'FaceColor',color(i,:),'EdgeColor',[0,0,0],'LineWidth',2)
hold on
%在柱状图 x,y 基础上 绘制误差 ,low为下误差,high为上误差,LineStyle 误差图样式,'Color' 误差图颜色
% 'LineWidth', 线宽,'CapSize',误差标注大小
% errorbar(i, y(i), low(i), high(i), 'LineStyle', 'none', 'Color', color(i+3,:), 'LineWidth', 1.5,'CapSize',18);
end
ylabel('obj-value')
ylim([min(Fival_compare2)-0.01,max(Fival_compare2)+0.01]);
ax=gca;
ax.XTick = 1:1:length(Fival_compare2);
set(gca,'XTickLabel',name_all,"LineWidth",2);
set(gca,"FontSize",12,"LineWidth",2)
title('自写网络')
%%
figure(5)
bar([Fival_compare1;Fival_compare2]')
ylabel('obj-value')
ylim([min(Fival_compare2)-0.01,max(Fival_compare2)+0.01]);
ax=gca;
ax.XTick = 1:1:length(Fival_compare2);
set(gca,'XTickLabel',name_all,"LineWidth",2);
set(gca,"FontSize",12,"LineWidth",2)
legend('工具箱','自写网络')
%%
figure(7)
color=[0.741176470588235,0.729411764705882,0.725490196078431;0.525490196078431,...
0.623529411764706,0.752941176470588;0.631372549019608,0.803921568627451,...
0.835294117647059;0.588235294117647,0.576470588235294,0.576470588235294;...
0.0745098039215686,0.407843137254902,0.607843137254902;0.454901960784314,...
0.737254901960784,0.776470588235294;0.0156862745098039,0.0196078431372549,0.0156862745098039];% 颜色1
mean_compare1=mean(FFival_compare1);
std1_compare1=std(FFival_compare1);
mean_compare2=mean(FFival_compare2);
std1_compare2=std(FFival_compare2);
b=bar([mean_compare1;mean_compare2]');
data=[mean_compare1;mean_compare2]';
hold on
erro_data=[std1_compare1;std1_compare1]';
ax = gca;
for i = 1 : 2
x_data(:, i) = b(i).XEndPoints';
end
for i=1:2
errorbar(x_data(:,i),data(:,i),erro_data(:,i),'LineStyle', 'none','Color',color(i+3,:) ,'LineWidth', 2,'CapSize',18)
end
for i =1:2
b(i).FaceColor = color(i,:);
b(i).EdgeColor= color(i+3,:);
b(i).LineWidth=1.5;
end
ylabel('obj-value')
maxl=max([mean_compare1,mean_compare2]);
minl=min([mean_compare1,mean_compare2]);
ylim([minl-0.01,maxl+0.01]);
ax=gca;
ax.XTick = 1:1:length(Fival_compare2);
set(gca,'XTickLabel',name_all,"LineWidth",2);
set(gca,"FontSize",12,"LineWidth",2)
legend('工具箱','自写网络')
box off
% net=newff(x_train_regular,y_train_regular,hidden_num,{'logsig','purelin'},'trainlm','deviderand');
%%
% function fitness_value =objfun(input_pop,input_num,hidden_num,output_num,input_data,output_data)
function fitness_value =objfun(input_pop)
global input_num hidden_num output_num input_data output_data
w1=input_pop(1:input_num*hidden_num); %输入和隐藏层之间的权重参数
B1=input_pop(input_num*hidden_num+1:input_num*hidden_num+hidden_num); %隐藏层神经元的偏置
w2=input_pop(input_num*hidden_num+hidden_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num); %隐藏层和输出层之间的偏置
B2=input_pop(input_num*hidden_num+hidden_num+hidden_num*output_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num+output_num); %输出层神经元的偏置
%网络权值赋值
W1=reshape(w1,hidden_num,input_num);
W2=reshape(w2,output_num,hidden_num);
B1=reshape(B1,hidden_num,1);
B2=reshape(B2,output_num,1);
[~,n]=size(input_data);
A1=logsig(W1*input_data+repmat(B1,1,n)); %需与main函数中激活函数相同
A2=purelin(W2*A1+repmat(B2,1,n)); %需与main函数中激活函数相同
error=sumsqr(output_data-A2);
fitness_value=error; %误差即为适应度
end
%%
function EcRMSE =fun3(input_pop,x_test_data,x_train_mu,x_train_sigma,y_train_mu,y_train_sigma,y_test_data)
global input_num hidden_num output_num
w1=input_pop(1:input_num*hidden_num); %输入和隐藏层之间的权重参数
B1=input_pop(input_num*hidden_num+1:input_num*hidden_num+hidden_num); %隐藏层神经元的偏置
w2=input_pop(input_num*hidden_num+hidden_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num); %隐藏层和输出层之间的偏置
B2=input_pop(input_num*hidden_num+hidden_num+hidden_num*output_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num+output_num); %输出层神经元的偏置
%网络权值赋值
% W1=reshape(w1,hidden_num,input_num);
% W2=reshape(w2,output_num,hidden_num);
% B1=reshape(B1,hidden_num,1);
% B2=reshape(B2,output_num,1);
vij = reshape(w1,hidden_num,input_num) ;%输入和隐藏层的权重
theta_u = reshape(B1,hidden_num,1);%输入与隐藏层之间的阈值
Wj = reshape(w2,output_num,hidden_num);%%输出和隐藏层的权重
theta_y =reshape(B2,output_num,1);%输出与隐藏层之间的阈值
% [~,n]=size(input_data);
% A1=logsig(W1*input_data+repmat(B1,1,n)); %需与main函数中激活函数相同
% A2=purelin(W2*A1+repmat(B2,1,n)); %需与main函数中激活函数相同
% error=sumsqr(output_data-A2);
% fitness_value=error; %误差即为适应度
test_num=size(x_test_data,1);
x_test_regular = (x_test_data-repmat(x_train_mu,test_num,1))./repmat(x_train_sigma,test_num,1);
% x_test_regular = mapminmax('apply',x_test_data,x_train_maxmin);
%放入到网络输出数据
x_test_std=x_test_regular';
test_put = logsig(vij * x_test_std + repmat(theta_u, 1, test_num));
test_out_std = Wj * test_put + repmat(theta_y, 1, test_num);
%反归一化
SSA_BP_predict=test_out_std*y_train_sigma+y_train_mu;
errors_nn=sum(abs(SSA_BP_predict'-y_test_data)./(y_test_data))/length(y_test_data);
EcRMSE=sqrt(sum((errors_nn).^2)/length(errors_nn));
% disp(EcRMSE)
end
%%
function [EcRMSE,net1]=Fun1(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data)
global input_num output_num x_train_mu y_train_mu x_train_sigma y_train_sigma
bestchrom=bestX;
net=newff(x_train_regular,y_train_regular,hidden_num,{'logsig','purelin'},'trainlm');
w1=bestchrom(1:input_num*hidden_num); %输入和隐藏层之间的权重参数
B1=bestchrom(input_num*hidden_num+1:input_num*hidden_num+hidden_num); %隐藏层神经元的偏置
w2=bestchrom(input_num*hidden_num+hidden_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num); %隐藏层和输出层之间的偏置
B2=bestchrom(input_num*hidden_num+hidden_num+hidden_num*output_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num+output_num); %输出层神经元的偏置
%网络权值赋值
net.iw{1,1}=reshape(w1,hidden_num,input_num);
net.lw{2,1}=reshape(w2,output_num,hidden_num);
net.b{1}=reshape(B1,hidden_num,1);
net.b{2}=reshape(B2,output_num,1);
net.trainParam.epochs=200; %最大迭代次数
net.trainParam.lr=0.1; %学习率
net.trainParam.goal=0.00001;
[net,~]=train(net,x_train_regular,y_train_regular);
%将输入数据归一化
test_num=size(x_test_data,1);
x_test_regular = (x_test_data-repmat(x_train_mu,test_num,1))./repmat(x_train_sigma,test_num,1);
%放入到网络输出数据
y_test_regular=sim(net,x_test_regular');
net1=net;
%将得到的数据反归一化得到预测数据
test_out_std=y_test_regular;
%反归一化
SSA_BP_predict=test_out_std*y_train_sigma+y_train_mu;
errors_nn=sum(abs(SSA_BP_predict'-y_test_data)./(y_test_data))/length(y_test_data);
EcRMSE=sqrt(sum((errors_nn).^2)/length(errors_nn));
disp(EcRMSE)
end
%%
function [EcRMSE,WW]=Fun2(bestX,x_train_regular,y_train_regular,hidden_num,x_test_data,y_test_data)
global input_num output_num x_train_mu y_train_mu x_train_sigma y_train_sigma
train_num=length(y_train_regular); %自变量
test_num=length(y_test_data);
bestchrom=bestX;
% net=newff(x_train_regular,y_train_regular,hidden_num,{'tansig','purelin'},'trainlm');
w1=bestchrom(1:input_num*hidden_num); %输入和隐藏层之间的权重参数
B1=bestchrom(input_num*hidden_num+1:input_num*hidden_num+hidden_num); %隐藏层神经元的偏置
w2=bestchrom(input_num*hidden_num+hidden_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num); %隐藏层和输出层之间的偏置
B2=bestchrom(input_num*hidden_num+hidden_num+hidden_num*output_num+1:input_num*hidden_num+...
hidden_num+hidden_num*output_num+output_num); %输出层神经元的偏置
%网络权值赋值
x_train_std=x_train_regular;
y_train_std=y_train_regular;
%
vij = reshape(w1,hidden_num,input_num) ;%输入和隐藏层的权重
theta_u = reshape(B1,hidden_num,1);%输入与隐藏层之间的阈值
Wj = reshape(w2,output_num,hidden_num);%%输出和隐藏层的权重
theta_y =reshape(B2,output_num,1);%输出与隐藏层之间的阈值
%
learn_rate = 0.0001;%学习率
Epochs_max = 30000;%最大迭代次数
error_rate = 0.001;%目标误差
Obj_save = zeros(1,Epochs_max);%损失函数
% 训练网络
epoch_num=0;
while epoch_num <Epochs_max
epoch_num=epoch_num+1;
y_pre_std_u=vij * x_train_std + repmat(theta_u, 1, train_num);
y_pre_std_u1 = logsig(y_pre_std_u);
y_pre_std_y = Wj * y_pre_std_u1 + repmat(theta_y, 1, train_num);
y_pre_std_y1=y_pre_std_y;
obj = y_pre_std_y1-y_train_std ;
Ems = sumsqr(obj);
Obj_save(epoch_num) = Ems;
if Ems < error_rate
break;
end
%梯度下降
%输出采用rule函数,隐藏层采用sigomd激活函数
c_wj= 2*(y_pre_std_y1-y_train_std)* y_pre_std_u1';
c_theta_y=2*(y_pre_std_y1-y_train_std)*ones(train_num, 1);
c_vij=Wj'* 2*(y_pre_std_y1-y_train_std).*(y_pre_std_u1).*(1-y_pre_std_u1)* x_train_std';
c_theta_u=Wj'* 2*(y_pre_std_y1-y_train_std).*(y_pre_std_u1).*(1-y_pre_std_u1)* ones(train_num, 1);
Wj=Wj-learn_rate*c_wj;
theta_y=theta_y-learn_rate*c_theta_y;
vij=vij- learn_rate*c_vij;
theta_u=theta_u-learn_rate*c_theta_u;
end
WW=[vij(:);theta_u;Wj(:);theta_y];
% W1=[Wj(:),theta_y,vij(:),theta_u];
x_test_regular = (x_test_data-repmat(x_train_mu,test_num,1))./repmat(x_train_sigma,test_num,1);
% x_test_regular = mapminmax('apply',x_test_data,x_train_maxmin);
%放入到网络输出数据
x_test_std=x_test_regular';
test_put = logsig(vij * x_test_std + repmat(theta_u, 1, test_num));
test_out_std = Wj * test_put + repmat(theta_y, 1, test_num);
%反归一化
SSA_BP_predict=test_out_std*y_train_sigma+y_train_mu;
errors_nn=sum(abs(SSA_BP_predict'-y_test_data)./(y_test_data))/length(y_test_data);
EcRMSE=sqrt(sum((errors_nn).^2)/length(errors_nn));
disp(EcRMSE)
end
得到多次运行的平均结果如下
考虑均值和方差的误差棒图如下
【代码详细使用教程在B站】
Lvy-呀
https://www.bilibili.com/video/BV1p1421b7i3/
获取方式:公众号【Lvy的口袋】回复关键词【优化BP】免费领取
绝绝好用的一键式算法工具箱也在更新ing
评价、降维、聚类、回归、分类都有多种优质方法,且都支持一键导出代码
评价工具箱pingjia
聚类降维工具箱DrCluster
分类回归工具箱RCpredict
时序工具箱MTpredict
四个工具打包获取,一码通用只需要激活一次,所有工具箱都可以一键导出代码
可以观看视频的1~26节
【获取方式】扫码获取或者点击链接
https://mbd.pub/o/bread/mbd-ZJabmJ9q
更多开源资料可以看公众号主栏引导
END
长按二维码识别关注
更多竞赛、保研、科研学习资料领取或其他需求咨询可以联系我们~