回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出

回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出

目录

      • 回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出
      • 预测效果
      • 基本介绍
      • 环境介绍
      • 程序设计
      • 参考资料

预测效果

回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出_第1张图片
回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出_第2张图片
回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出_第3张图片
回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出_第4张图片
回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出_第5张图片

基本介绍

注意力机制模仿人脑,更加注重重要信息,而忽略相对无用的信息,已被广泛应用于自然语言处理、图像及语音识别中,近年来也被广泛应用于各类预测问题。传统注意力机制注重不同时间点的权重分布,在每个时间步只含有一个变量时有较好的效果。但对于区域内的多风电机组功率预测,每个时间步都含有多个变量,各个变量之间可能存在复杂的非线性内在联系,且每个变量序列都有自己的特征和周期,难以单独选取某个时间步作为注意重点。而TPA则由多个一维CNN滤波器从BiLSTM隐藏状态行向量抽取特征,使得模型能够从不同时间步学习多变量之间的互相依赖关系。

环境介绍

运行环境,Matlab2020b。

程序设计

  • 完整程序下载:TPA-LSTM
% 数据集 列为特征,行为样本数目
%% 数据导入及处理
load('./Train.mat')
Train.weekend = dummyvar(Train.weekend);
Train.month = dummyvar(Train.month);
Train = movevars(Train,{'weekend','month'},'After','demandLag');
Train.ts = [];

% Train.hour = dummyvar(Train.hour);
%自己主动观察右侧工作区变量格式,对前面数据进行更改替换
Train(1,:) =[];
y = Train.demand;
x = Train{:,2:5};
[xnorm,xopt] = mapminmax(x',0,1);
[ynorm,yopt] = mapminmax(y',0,1);
%
% xnorm = [xnorm;Train.weekend';Train.month'];
%%
% x = x';
xnorm = xnorm(:,1:1000);
ynorm = ynorm(1:1000);

k = 24;           % 滞后长度

% 转换成2-D image
for i = 1:length(ynorm)-k

    Train_xNorm(:,i,:) = xnorm(:,i:i+k-1);
    Train_yNorm(i) = ynorm(i+k-1);
    Train_y(i) = y(i+k-1);
end
Train_yNorm= Train_yNorm';



ytest = Train.demand(1001:1170);
xtest = Train{1001:1170,2:5};
[xtestnorm] = mapminmax('apply', xtest',xopt);
[ytestnorm] = mapminmax('apply',ytest',yopt);
% xtestnorm = [xtestnorm; Train.weekend(1001:1170,:)'; Train.month(1001:1170,:)'];
xtest = xtest';
for i = 1:length(ytestnorm)-k
    Test_xNorm(:,i,:) = xtestnorm(:,i:i+k-1);
    Test_yNorm(i) = ytestnorm(i+k-1);
    Test_y(i) = ytest(i+k-1);
end
Test_yNorm = Test_yNorm';

clear k i x y
%
Train_xNorm = dlarray(Train_xNorm,'CBT');
Train_yNorm = dlarray(Train_yNorm,'BC');
Test_xNorm = dlarray(Test_xNorm,'CBT');
Test_yNorm = dlarray(Test_yNorm,'BC');
%% 训练集和验证集划分
TrainSampleLength = length(Train_yNorm);
validatasize = floor(TrainSampleLength * 0.1);
Validata_xNorm = Train_xNorm(:,end - validatasize:end,:);
Validata_yNorm = Train_yNorm(:,TrainSampleLength-validatasize:end);
Validata_y = Train_y(TrainSampleLength-validatasize:end);

%参数设置
inputSize = size(Train_xNorm,1);   %数据输入x的特征维度
outputSize = 1;                    %数据输出y的维度  
numhidden_units1=50;

[params,~] = paramsInit(numhidden_units1,inputSize,outputSize);     % 导入初始化参数

[~,validatastate] = paramsInit(numhidden_units1,inputSize,outputSize);     % 导入初始化参数
[~,TestState] = paramsInit(numhidden_units1,inputSize,outputSize);     % 导入初始化参数
% 训练相关参数
TrainOptions;
numIterationsPerEpoch = floor((TrainSampleLength-validatasize)/minibatchsize);
LearnRate = 0.01;
%% Loop over epochs.
figure
start = tic;
lineLossTrain = animatedline('color','r');
validationLoss = animatedline('color',[0 0 0]./255,'Marker','o','MarkerFaceColor',[150 150 150]./255);
xlabel("Iteration")
ylabel("Loss")

% epoch 更新 
iteration = 0;
for epoch = 1 : numEpochs
   
    [~,state] = paramsInit(numhidden_units1,inputSize,outputSize);       % 每轮epoch,state初始化
    disp(['Epoch: ', int2str(epoch)])  
      % 作图(训练过程损失图)--------------------------********————————————————————————————————————————————————
        D = duration(0,0,toc(start),'Format','hh:mm:ss');
        addpoints(lineLossTrain,iteration,double(gather(extractdata(loss))))
        if iteration == 1 || mod(iteration,validationFrequency) == 0
            addpoints(validationLoss,iteration,double(gather(extractdata(lossValidation))))
        end
        title("Epoch: " + epoch + ", Elapsed: " + string(D))
        legend('训练集','验证集')
        drawnow
        
    end
    
    % 每轮epoch 更新学习率
    if mod(epoch,5) == 0
        LearnRate = LearnRate * LearnRateDropFactor;
    end
end


%% 训练集
Predict_yNorm = TPAModelPredict(gpuArray(Train_xNorm),params,TestState);
Predict_yNorm = extractdata(Predict_yNorm);

Predict_y = mapminmax('reverse',Predict_yNorm,yopt);
%
figure
plot(Predict_y,'-.','Color',[50 100 180]./255,'linewidth',1.5,'Markersize',3,'MarkerFaceColor',[50 100 180]./255);
hold on 
plot(Train_y,'-.','Color',[150 150 150]./255,'linewidth',1.5,'Markersize',3,'MarkerFaceColor',[150 150 150]./255)
legend('训练集预测值','训练集实际值')
%% 验证集
Predict_yNorm = TPAModelPredict(gpuArray(Validata_xNorm),params,TestState);
Predict_yNorm = extractdata(Predict_yNorm);

Predict_y = mapminmax('reverse',Predict_yNorm,yopt);
%
figure
plot(Predict_y,'-.','Color',[255 0 0]./255,'linewidth',1.5,'Markersize',3,'MarkerFaceColor',[255 0 0]./255);
hold on 
plot(Validata_y,'--','Color',[150 150 150]./255,'linewidth',1.5,'Markersize',3,'MarkerFaceColor',[0 0 0]./255)
legend('验证集预测值','验证集实际值')

%%  predict(传统递归测试集)
% clear Predict_yNorm
% % Test_xNorm = extractdata(Test_xNorm);
% for i = 1:size(Test_xNorm,2)
%     if i ==1
%         [a,Teststate] = TPAModelPredict(dlarray(Test_xNorm(:,i,:),'CBT'),params,TestState);

%% predict(直接测试集)
Predict_yNorm = TPAModelPredict(dlarray(Test_xNorm,'CBT'),params,TestState);
Predict_YNorm = extractdata(Predict_yNorm);
Predict_y = mapminmax('reverse',Predict_YNorm,yopt);

figure
plot(Predict_y,'-.','Color',[0 0 255]./255,'linewidth',1.5,'Markersize',3,'MarkerFaceColor',[0 0 255]./255);
hold on 
plot(Test_y,'--','Color',[150 150 150]./255,'linewidth',1.5,'Markersize',3,'MarkerFaceColor',[0 0 0]./255)
legend('测试集预测值','测试集实际值')

参考资料

[1] https://blog.csdn.net/kjm13182345320/article/details/125644313?spm=1001.2014.3001.5501
[2] https://blog.csdn.net/kjm13182345320/article/details/125637228?spm=1001.2014.3001.5501
[3] https://download.csdn.net/download/kjm13182345320/85661169?spm=1001.2014.3001.5501

你可能感兴趣的