当前位置:   article > 正文

多维时序 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出_matlab lstm 多维变量输入

matlab lstm 多维变量输入

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

预测效果

1
2
3
4
5

基本介绍

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

环境介绍

运行环境,Matlab2020b。

程序设计

% 数据集 列为特征,行为样本数目
%% 数据导入及处理
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147

参考资料

[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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/525543
推荐阅读
相关标签
  

闽ICP备14008679号