SCI一区级 | Matlab实现BES-CNN-GRU-Mutilhead-Attention多变量时间序列预测
layers0 = [ ...
% 输入特征
sequenceInputLayer([numFeatures,1,1],'name','input') %输入层设置
sequenceFoldingLayer('name','fold') %使用序列折叠层对图像序列的时间步长进行独立的卷积运算。
% CNN特征提取
convolution2dLayer([3,1],16,'Stride',[1,1],'name','conv1') %添加卷积层,64,1表示过滤器大小,10过滤器个数,Stride是垂直和水平过滤的步长
batchNormalizationLayer('name','batchnorm1') % BN层,用于加速训练过程,防止梯度消失或梯度爆炸
reluLayer('name','relu1') % ReLU激活层,用于保持输出的非线性性及修正梯度的问题
% 池化层
maxPooling2dLayer([2,1],'Stride',2,'Padding','same','name','maxpool') % 第一层池化层,包括3x3大小的池化窗口,步长为1,same填充方式
% 展开层
sequenceUnfoldingLayer('name','unfold') %独立的卷积运行结束后,要将序列恢复
%平滑层
flattenLayer('name','flatten')
selfAttentionLayer(2,2) %创建2个头,2个键和查询通道的自注意力层
dropoutLayer(0.1,'name','dropout_1') % Dropout层,以概率为0.2丢弃输入
fullyConnectedLayer(1,'name','fullconnect') % 全连接层设置(影响输出维度)(cell层出来的输出层) %
regressionLayer('Name','output') ];
lgraph0 = layerGraph(layers0);
lgraph0 = connectLayers(lgraph0,'fold/miniBatchSize','unfold/miniBatchSize');
pNum = round( pop * P_percent ); % The population size of the producers
for t=1:MaxIt
%% 1- select_space
[pop BestSol s1(t)]=select_space(fobj,pop,nPop,BestSol,low,high,dim);
%% 2- search in space
[pop BestSol s2(t)]=search_space(fobj,pop,BestSol,nPop,low,high);
%% 3- swoop
[pop BestSol s3(t)]=swoop(fobj,pop,BestSol,nPop,low,high);
Convergence_curve(t)=BestSol.cost;
disp(num2str([t BestSol.cost]))
ed=cputime;
timep=ed-st;
end
function [pop BestSol s1]=select_space(fobj,pop,npop,BestSol,low,high,dim)
Mean=mean(pop.pos);
% Empty Structure for Individuals
empty_individual.pos = [];
empty_individual.cost = [];
lm= 2;
s1=0;
for i=1:npop
newsol=empty_individual;
newsol.pos= BestSol.pos+ lm*rand(1,dim).*(Mean - pop.pos(i,:));
newsol.pos = max(newsol.pos, low);
newsol.pos = min(newsol.pos, high);
newsol.cost=fobj(newsol.pos);
if newsol.cost<pop.cost(i)
pop.pos(i,:) = newsol.pos;
pop.cost(i)= newsol.cost;
s1=s1+1;
if pop.cost(i) < BestSol.cost
BestSol.pos= pop.pos(i,:);
BestSol.cost=pop.cost(i);
end
end
end