%% CNN-LSTM多变量回归预测
%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
% restoredefaultpath
tic
%% 加载数据与数据集划分
% load 1.mat
% load 2.mat
% load 3.mat
% load 4.mat
%% 导入数据
P_train = xlsread('data','training set','B2:G191')';
T_train= xlsread('data','training set','H2:H191')';
% 测试集——44个样本
P_test=xlsread('data','test set','B2:G45')';
T_test=xlsread('data','test set','H2:H45')';
%% 划分训练集和测试集
M = size(P_train, 2);
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);
%% 创建元胞或向量,长度为训练集大小;
XrTrain = cell(size(p_train,2),1);
YrTrain = zeros(size(t_train,2),1);
for i=1:size(p_train,2)
XrTrain{i,1} = p_train(:,i);
YrTrain(i,1) = t_train(:,i);
end
% 创建元胞或向量,长度为测试集大小;
XrTest = cell(size(p_test,2),1);
YrTest = zeros(size(t_test ,2),1);
for i=1:size(p_test,2)
XrTest{i,1} = p_test(:,i);
YrTest(i,1) = t_test (:,i);
end
%% 创建混合CNN-LSTM网络架构
% 输入特征维度
numFeatures = size(p_train,1);
% 输出特征维度
numResponses = 1;
FiltZise = 10;
% 创建"CNN-LSTM"模型
layers = [...
% 输入特征
sequenceInputLayer([numFeatures 1 1],'Name','input')
sequenceFoldingLayer('Name','fold')
% CNN特征提取
convolution2dLayer([FiltZise 1],32,'Padding','same','WeightsInitializer','he','Name','conv','DilationFactor',1);
batchNormalizationLayer('Name','bn')
eluLayer('Name','elu')
averagePooling2dLayer(1,'Stride',FiltZise,'Name','pool1')
% 展开层
sequenceUnfoldingLayer('Name','unfold')
% 平滑层
flattenLayer('Name','flatten')
% LSTM特征学习