Technology Sharing

Transformer-LSTM Forecasting | Matlab implementation of Transformer-LSTM multivariate time series forecasting

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Transformer-LSTM prediction | Matlab implementation of Transformer-LSTMMultivariate Time Series Forecasting

Results at a Glance

insert image description here

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

basic introduction

1. Matlab implements Transformer-LSTM multivariate time series prediction, Transformer combined with LSTM long short-term memoryNeural NetworksMultivariate time series forecasting;

2. The operating environment is Matlab2023b and above;

3.data isdata set, input multiple features, output a single variable, consider the influence of historical features, multivariate time series prediction, main.m is the main program, just run it, all files are placed in one folder;

4. The command window outputs multiple index evaluations such as R2, MSE, RMSE, MAE, MAPE, MBE, etc.;

insert image description here

programming

  • Download the complete program and data. Send a private message to the blogger for reply.Matlab implementation of Transformer-LSTM multivariate time series prediction


%%  清空环境变量
warning off             % 关闭报警信息
close all               % 关闭开启的图窗
clear                   % 清空变量
clc                     % 清空命令行

%%  导入数据
result = xlsread('data.xlsx');

%%  数据分析
num_samples = length(result);  % 样本个数
or_dim = size(result, 2);      % 原始特征+输出数目
kim =  2;                      % 延时步长(kim个历史数据作为自变量)
zim =  1;                      % 跨zim个时间点进行预测



%%  数据集分析
outdim = 1;                                  % 最后一列为输出
num_size = 0.7;                              % 训练集占数据集比例
num_train_s = round(num_size * num_samples); % 训练集样本个数
f_ = size(res, 2) - outdim;                  % 输入特征维度


%%  划分训练集和测试集
P_train = res(1: num_train_s, 1: f_)';
T_train = res(1: num_train_s, f_ + 1: end)';
M = size(P_train, 2);

P_test = res(num_train_s + 1: end, 1: f_)';
T_test = res(num_train_s + 1: end, f_ + 1: end)';
N = size(P_test, 2);

%%  数据归一化
[P_train, ps_input] = mapminmax(P_train, 0, 1);
P_test = mapminmax('apply', P_test, ps_input);

[t_train, ps_output] = mapminmax(T_train, 0, 1);
t_test = mapminmax('apply', T_test, ps_output);

%%  数据平铺
P_train =  double(reshape(P_train, f_, 1, 1, M));
P_test  =  double(reshape(P_test , f_, 1, 1, N));

t_train = t_train';
t_test  = t_test' ;

%%  数据格式转换
for i = 1 : M
    p_train{i, 1} = P_train(:, :, 1, i);
end

for i = 1 : N
    p_test{i, 1}  = P_test( :, :, 1, i);
end


  • 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

References

[1] https://blog.csdn.net/kjm13182345320/article/details/128163536?spm=1001.2014.3001.5502
[2] https://blog.csdn.net/kjm13182345320/article/details/128151206?spm=1001.2014.3001.5502