bshudijenfoc
2022-03-23 14:54
采纳率: 50%
浏览 236

如何使用pso-lstm实现对未来数据的预测(语言-matlab)

问题遇到的现象和发生背景 我是将6个影响因素作为输入层,一个因变量作为输出层,但现有的pso-lstm模型代码只能对现有数据划分训练集和测试集,可以理解为对现有数据的拟合,不是真正意义上的预测。
问题相关代码,请勿粘贴截图

clear
close  all
%% 数据读取
geshu=30;%训练集的个数
%读取数据
shuru=xlsread('数据的输入.xlsx');
shuchu=xlsread('数据的输出.xlsx');
%nn = randperm(size(shuru,1));%随机排序
nn=1:size(shuru,1);%正常排序
input_train =shuru(nn(1:geshu),:);
input_train=input_train';
output_train=shuchu(nn(1:geshu),:);
output_train=output_train';
input_test =shuru(nn((geshu+1):end),:);
input_test=input_test';
output_test=shuchu(nn((geshu+1):end),:);
output_test=output_test';
%样本输入输出数据归一化
[aa,bb]=mapminmax([input_train input_test]);
[cc,dd]=mapminmax([output_train output_test]);
global inputn outputn shuru_num shuchu_num
[inputn,inputps]=mapminmax('apply',input_train,bb);
[outputn,outputps]=mapminmax('apply',output_train,dd);
shuru_num = size(input_train,1); % 输入维度
shuchu_num = 1;  % 输出维度
%%
% 1. 寻找最佳参数
NN=5;                   %初始化群体个数
D=2;                   %初始化群体维数,
T=10;                 %初始化群体最迭代次数
c1=2;                   %学习因子1
c2=2;                   %学习因子2
%用线性递减因子粒子群算法
Wmax=1.2; %惯性权重最大值
Wmin=0.8; %惯性权重最小值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%每个变量的取值范围
ParticleScope(1,:)=[10 200];  % 中间层神经元个数
ParticleScope(2,:)=[0.01 0.15]; % 学习率
ParticleScope=ParticleScope';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xv=rand(NN,2*D); %首先,初始化种群个体速度和位置
for d=1:D
    xv(:,d)=xv(:,d)*(ParticleScope(2,d)-ParticleScope(1,d))+ParticleScope(1,d);  
    xv(:,D+d)=(2*xv(:,D+d)-1 )*(ParticleScope(2,d)-ParticleScope(1,d))*0.2;
end
x1=xv(:,1:D);%位置
v1=xv(:,D+1:2*D);%速度
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------初始化个体位置和适应度值-----------------
p1=x1;
pbest1=ones(NN,1);
for i=1:NN
    pbest1(i)=fitness(x1(i,:));
end
%------初始时全局最优位置和最优值---------------
gbest1=min(pbest1);
lab=find(min(pbest1)==pbest1);
g1=x1(lab,:);
gb1=ones(1,T);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-----浸入主循环,按照公式依次迭代直到迭代次数---
% N=40;                   %初始化群体个数
% D=10;                   %初始化群体维数
% T=100;                 %初始化群体最迭代次数
for i=1:T
    for j=1:NN
        if (fitness(x1(j,:))<pbest1(j))
            p1(j,:)=x1(j,:);%变量
            pbest1(j)=fitness(x1(j,:));
        end
        if(pbest1(j)<gbest1)
            g1=p1(j,:);%变量
            gbest1=pbest1(j);
        end
         w=Wmax-(Wmax-Wmin)*i/T;          
         v1(j,:)=w*v1(j,:)+c1*rand*(p1(j,:)-x1(j,:))+c2*rand*(g1-x1(j,:));
         x1(j,:)=x1(j,:)+v1(j,:); 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%位置约束
        label=find(x1(j,:)>ParticleScope(2,:));
        x1(j,label)=ParticleScope(2,label);        
        label2=find(x1(j,:)<ParticleScope(1,:));
        x1(j,label2)=ParticleScope(1,label2);
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%速度约束                
        labe3=find(v1(j,:)>ParticleScope(2,:)*0.2);
        v1(j,labe3)=ParticleScope(2,labe3)*0.2;        
        label4=find(v1(j,:)<-ParticleScope(2,:)*0.2);
        v1(j,label4)=-ParticleScope(2,label4)*0.2;              
    end
        gb1(i)=min(pbest1);
end
zhongjian1_num = round(g1(1));  
xue = g1(2);
%% 模型建立与训练
layers = [ ...
    sequenceInputLayer(shuru_num)    % 输入层
    lstmLayer(zhongjian1_num)        % LSTM层
    fullyConnectedLayer(shuchu_num)  % 全连接层
    regressionLayer];
 
options = trainingOptions('adam', ...   % 梯度下降
    'MaxEpochs',50, ...                % 最大迭代次数
    'GradientThreshold',1, ...         % 梯度阈值 
    'InitialLearnRate',xue,...
    'Verbose',0, ...
    'Plots','training-progress');            % 学习率
%% 训练LSTM
net = trainNetwork(inputn,outputn,layers,options);
%% 预测
net = resetState(net);% 网络的更新状态可能对分类产生了负面影响。重置网络状态并再次预测序列。
[~,Ytrain]= predictAndUpdateState(net,inputn);
test_simu=mapminmax('reverse',Ytrain,dd);%反归一化
rmse = sqrt(mean((test_simu-output_train).^2));   % 训练集
%测试集样本输入输出数据归一化
inputn_test=mapminmax('apply',input_test,bb);
[net,an]= predictAndUpdateState(net,inputn_test);
test_simu1=mapminmax('reverse',an,dd);%反归一化
error1=test_simu1-output_test;%测试集预测-真实
%计算均方根误差 (RMSE)。
rmse1 = sqrt(mean((test_simu1-output_test).^2));  % 测试集
运行结果及报错内容 通过以上代码无法实现未来数据的预测
我的解答思路和尝试过的方法 看到过网上和文献中都提到可以使用滑动窗格法实现预测,但无奈非计算机专业的学生不知道具体操作步骤
我想要达到的结果 能否对以上代码进行修改,使之可以实现对未来数据的预测呢

3条回答 默认 最新

相关推荐 更多相似问题