融合正余弦和柯西变异的麻雀优化算法SCSSA-CNN-BiLSTM双向长短期记忆网络预测模型
通过融合正余弦和柯西变异改进麻雀搜索算法,对CNN-BiLSTM的学习率、正则化参数以及BiLSTM隐含层神经元个数等进行优化
预测效果图如下
代码如下:
%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc
%% 导入数据
data = xlsread('data.xls');
[h1,l1]=data_process(data,8);
data = [h1,l1];
[m,n]=size(data);
input = data(:,1:n);
output = data(:,n);
numTimeStepsTrain = floor(0.7*numel(data(:,1))); %取70%的数据作为训练集
XTrain = input(1:numTimeStepsTrain,:);
YTrain = output(1:numTimeStepsTrain,:);
XTest = input(numTimeStepsTrain+1:end,:);
YTest = output(numTimeStepsTrain+1:end,:);
x = XTrain;
y = YTrain;
[xnorm,xopt] = mapminmax(x',0,1);
[ynorm,yopt] = mapminmax(y',0,1);
% 转换成2-D image
for i = 1:length(ynorm)
Train_xNorm{i} = reshape(xnorm(:,i),n,1,1);
Train_yNorm(:,i) = ynorm(:,i);
Train_y(i,:) = y(i,:);
end
Train_yNorm= Train_yNorm';
xtest = XTest;
ytest = YTest;
[xtestnorm] = mapminmax('apply', xtest',xopt);
[ytestnorm] = mapminmax('apply',ytest',yopt);
xtest = xtest';
for i = 1:length(ytestnorm)
Test_xNorm{i} = reshape(xtestnorm(:,i),n,1,1);
Test_yNorm(:,i) = ytestnorm(:,i);
Test_y(i,:) = ytest(i,:);
end
Test_yNorm = Test_yNorm';
%% 优化算法优化前,构建优化前的CNN-BILSTM模型
inputSize = n;
outputSize = 1; %数据输出y的维度
layers0 = [ ...
sequenceInputLayer([inputSize,1,1],'name','input') %输入层设置
sequenceFoldingLayer('name','fold') %使用序列折叠层对图像序列的时间步长进行独立的卷积运算。
convolution2dLayer([2,1],10,'Stride',[1,1],'name','conv1') %添加卷积层,2,1表示过滤器大小,10过滤器个数,Stride是垂直和水平过滤的步长
batchNormalizationLayer('name','batchnorm1') % BN层,用于加速训练过程,防止梯度消失或梯度爆炸
reluLayer('name','relu1') % ReLU激活层,用于保持输出的非线性性及修正梯度的问题
convolution2dLayer([1,1],10,'Stride',[1,1],'name','conv2') %添加卷积层,2,1表示过滤器大小,10过滤器个数,Stride是垂直和水平过滤的步长
batchNormalizationLayer('name','batchnorm2') % BN层,用于加速训练过程,防止梯度消失或梯度爆炸
reluLayer('name','relu2') % ReLU激活层,用于保持输出的非线性性及修正梯度的问题
maxPooling2dLayer([1,5],'Stride',1,'Padding','same','name','maxpool') % 第一层池化层,包括3x3大小的池化窗口,步长为1,same填充方式
sequenceUnfoldingLayer('name','unfold') %独立的卷积运行结束后,要将序列恢复
flattenLayer('name','flatten')
bilstmLayer(3,'Outputmode','last','name','hidden1')
dropoutLayer(0.2,'name','dropout_1') % Dropout层,以概率为0.3丢弃输入
bilstmLayer(5,'Outputmode','sequence','name','hidden3')
dropoutLayer(0.3,'name','dropout_3') % Dropout层,以概率为0.3丢弃输入
bilstmLayer(5,'Outputmode','last','name','hidden2')
dropoutLayer(0.3,'name','drdiopout_2')
fullyConnectedLayer(outputSize,'name','fullconnect') % 全连接层设置(影响输出维度)(cell层出来的输出层) %
tanhLayer('name','softmax')
regressionLayer('name','output')];
lgraph0 = layerGraph(layers0);
lgraph0 = connectLayers(lgraph0,'fold/miniBatchSize','unfold/miniBatchSize');
% 参数设置
options0 = trainingOptions('adam', ... % 优化算法Adam
'MaxEpochs', 300, ... % 最大训练次数
'GradientThreshold', 1, ... % 梯度阈值
'InitialLearnRate', 0.01, ... % 初始学习率
'LearnRateSchedule', 'piecewise', ... % 学习率调整
'LearnRateDropPeriod',100, ... % 训练100次后开始调整学习率
'LearnRateDropFactor',0.01, ... % 学习率调整因子
'L2Regularization', 0.002, ... % 正则化参数
'ExecutionEnvironment', 'cpu',... % 训练环境
'Verbose', 1, ... % 关闭优化过程
'Plots', 'none'); % 画出曲线
% 网络训练
net0 = trainNetwork(Train_xNorm,Train_yNorm,lgraph0,options0 );
Predict_Ynorm_Test = net0.predict(Test_xNorm);
Predict_Y_Test = mapminmax('reverse',Predict_Ynorm_Test',yopt);
Predict_Y_Test = Predict_Y_Test';
rmse = sqrt(mean((Predict_Y_Test(1,:)-(Test_y(1,:))).^2,'ALL'));
R2 = 1 - norm(Test_y - Predict_Y_Test)^2 / norm(Test_y - mean(Test_y ))^2;
disp(['优化前的RMSE:',num2str(rmse)])
% 预测集拟合效果图
figure
hold on
plot(Predict_Y_Test,'r-*','LineWidth',1.0)
plot(Test_y,'b-o','LineWidth',1.0)
legend('CNN-BiLSTM预测值','实际值')
ylabel('预测结果')
xlabel('预测样本')
title(['优化前测试集预测结果对比RMSE:',num2str(rmse)])
box off
set(gcf,'color','w')
%% 调用SSA优化CNN-BILSTM
disp('调用SSA优化CNN-BiLSTM......')
% SSA优化参数设置
SearchAgents = 20; % 种群数量 30
Max_iterations = 30; % 迭代次数 20
lowerbound = [0.0001 0.0001 10 10 10]; %五个参数的下限分别是正则化参数,学习率,BiLSTM的三个隐含层个数
upperbound = [0.01 0.01 300 30 30]; %五个参数的上限
dimension = length(lowerbound);%数量,即要优化的LSTM参数个数
[fMin,Best_pos,Convergence_curve,bestnet] = SCSSAforCNNBILSTM(SearchAgents,Max_iterations,lowerbound,upperbound,dimension,Train_xNorm,Train_yNorm,Test_xNorm,Test_y,yopt,n);
L2Regularization = Best_pos(1,1); % 最佳L2正则化系数
InitialLearnRate = Best_pos(1,2) ;% 最佳初始学习率
NumOfUnits1= fix(Best_pos(1,3)); % 最佳神经元个数
NumOfUnits2= fix(Best_pos(1,4)); % 最佳神经元个数
NumOfUnits3= fix(Best_pos(1,5)); % 最佳神经元个数
disp('优化结束,将最佳net导出并用于测试......')
%% 对训练集的测试
setdemorandstream(pi);
Predict_Ynorm_Train = bestnet.predict(Train_xNorm); %对训练集的测试
Predict_Y_Train = mapminmax('reverse',Predict_Ynorm_Train',yopt); %反归一化
Predict_Y_Train = Predict_Y_Train';
% 适应度曲线
figure
plot(Convergence_curve,'r-', 'LineWidth', 1.5);
title('SCSSA-CNN-BiLSTM', 'FontSize', 10);
legend('适应度值')
xlabel('迭代次数', 'FontSize', 10);
ylabel('适应度值', 'FontSize', 10);
box off
set(gcf,'color','w')
%训练集拟合效果图
figure
hold on
plot(Predict_Y_Train,'r-*','LineWidth',1.0)
plot(Train_y,'b-o','LineWidth',1.0);
ylabel('预测结果')
xlabel('预测样本')
legend('SCSSA-CNN-BiLSTM预测值','实际值')
title('优化后训练集预测结果对比')
box off
set(gcf,'color','w')
%% 对测试集的测试
Predict_Ynorm = bestnet.predict(Test_xNorm);
Predict_Y = mapminmax('reverse',Predict_Ynorm',yopt);
Predict_Y = Predict_Y';
figure
hold on
plot(Predict_Y,'r-*','LineWidth',1.0)
plot(Test_y,'b-o','LineWidth',1.0)
legend('SCSSA-CNN-BiLSTM预测值','实际值')
ylabel('预测结果')
xlabel('预测样本')
title('优化后测试集预测结果对比')
box off
set(gcf,'color','w')
%% 回归图与误差直方图
figure;
plotregression(Test_y,Predict_Y,['优化后回归图']);
set(gcf,'color','w')
figure;
ploterrhist(Test_y-Predict_Y,['误差直方图']);
set(gcf,'color','w')
%% 打印出评价指标
% 预测结果评价
ae= abs(Predict_Y - Test_y);
rmse = (mean(ae.^2)).^0.5;
mse = mean(ae.^2);
mae = mean(ae);
mape = mean(ae./Predict_Y);
R = corr(Test_y,Predict_Y);
R2 = 1 - norm(Test_y - Predict_Y)^2 / norm(Test_y-mean(Test_y ))^2;
disp('预测结果评价指标:')
disp(['RMSE = ', num2str(rmse)])
disp(['MSE = ', num2str(mse)])
disp(['MAE = ', num2str(mae)])
disp(['MAPE = ', num2str(mape)])
disp(['相关系数R = ', num2str(R)])
disp(['决定系数R^2为: ',num2str(R2)])
disp(['BiLSTM的最佳神经元个数为:', num2str(NumOfUnits1), ',',num2str(NumOfUnits2), ',',num2str(NumOfUnits3)])
disp(['最佳初始学习率为:', num2str(InitialLearnRate)])
disp(['最佳L2正则化系数为:', num2str(L2Regularization)])