代码原理
基于TCN(Temporal Convolutional Network)和BiGRU(Bidirectional Gated Recurrent Unit)的方法可以用于处理单输入单输出的数据时序预测任务。TCN是一种适用于时序数据的卷积神经网络结构,能够有效地捕捉时序数据中的长期依赖关系。BiGRU则是一种双向门控循环单元网络,能够同时考虑序列数据的过去和未来信息。以下是基于TCN-BiGRU的数据时序预测的一般步骤:
1. 数据准备:收集和整理单输入单输出的时序数据,确保数据整齐并且可供模型训练和测试。
2. 数据特征提取:根据时序预测的需求,可能需要对输入数据进行适当的特征提取,以便于模型的学习。
3. 数据划分:将数据集划分为训练集、验证集和测试集,以便于模型训练和评估。
4. 搭建TCN-BiGRU模型:构建一个堆叠了TCN和BiGRU层的模型结构,以便于综合利用卷积神经网络和双向循环神经网络的优势。
5. 模型训练:使用训练集数据对TCN-BiGRU模型进行训练,以学习输入数据和输出数据之间的映射关系。
6. 模型调参:调整模型的超参数,如学习率、层的数量、单元的数量等,以获得更好的性能。
7. 模型验证:使用验证集数据对训练好的模型进行验证,评估模型的泛化能力和性能。
8. 模型评估和预测:使用测试集数据对模型进行最终评估,并利用训练好的TCN-BiGRU模型对未来的时序数据进行预测。
TCN-BiGRU的结合将卷积神经网络和双向循环神经网络的特点结合起来,能够更好地处理时序数据,并且能够更好地捕捉数据中的长期依赖关系和时间特性。
部分代码
%% 清空环境变量来自公众号《KYXB》
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc
%% 导入数据
data = readmatrix('风电场预测.xlsx');
data = data(5665:8640,12); %选取3月份数据
[h1,l1]=data_process(data,24); %步长为24,采用前24个时刻的温度预测第25个时刻的温度
res = [h1,l1];
num_samples = size(res,1); %样本个数
% 训练集和测试集划分
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);
% 格式转换
for i = 1 : M
vp_train{i, 1} = p_train(:, i);
vt_train{i, 1} = t_train(:, i);
end
for i = 1 : N
vp_test{i, 1} = p_test(:, i);
vt_test{i, 1} = t_test(:, i);
end
%% 优化算法优化前,构建优化前的TCN_BiGRU模型
outputSize = 1; %数据输出y的维度
numFilters = 64;
filterSize = 5;
dropoutFactor = 0.005;
numBlocks = 2;
layer = sequenceInputLayer(f_,Normalization="rescale-symmetric",Name="input");
lgraph = layerGraph(layer);
outputName = layer.Name;
% 来自公众号《KYXB》
for i = 1:numBlocks
dilationFactor = 2^(i-1);
layers = [
convolution1dLayer(filterSize,numFilters,DilationFactor=dilationFactor,Padding="causal",Name="conv1_"+i)
layerNormalizationLayer
dropoutLayer(dropoutFactor)
% spatialDropoutLayer(dropoutFactor)
convolution1dLayer(filterSize,numFilters,DilationFactor=dilationFactor,Padding="causal")
layerNormalizationLayer
reluLayer
dropoutLayer(dropoutFactor)
additionLayer(2,Name="add_"+i)];
% Add and connect layers.
lgraph = addLayers(lgraph,layers);
lgraph = connectLayers(lgraph,outputName,"conv1_"+i);
% Skip connection.
if i == 1
% Include convolution in first skip connection.
layer = convolution1dLayer(1,numFilters,Name="convSkip");
lgraph = addLayers(lgraph,layer);
lgraph = connectLayers(lgraph,outputName,"convSkip");
lgraph = connectLayers(lgraph,"convSkip","add_" + i + "/in2");
else
lgraph = connectLayers(lgraph,outputName,"add_" + i + "/in2");
end
% Update layer output name.
outputName = "add_" + i;
end
tempLayers = flattenLayer("Name","flatten");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = gruLayer(35,"Name","gru1");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
FlipLayer("flip3")
gruLayer(35,"Name","gru2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
concatenationLayer(1,2,"Name","concat")
% selfAttentionLayer(1,50,"Name","selfattention") %Attention机制
fullyConnectedLayer(outdim,"Name","fc")
regressionLayer("Name","regressionoutput")];
lgraph = addLayers(lgraph,tempLayers);
% 来自公众号《KYXB》
lgraph = connectLayers(lgraph,outputName,"flatten");
lgraph = connectLayers(lgraph,"flatten","gru1");
lgraph = connectLayers(lgraph,"flatten","flip3");
lgraph = connectLayers(lgraph,"gru1","concat/in1");
lgraph = connectLayers(lgraph,"gru2","concat/in2");
% 参数设置
options0 = trainingOptions('adam', ... % 优化算法Adam
'MaxEpochs', 150, ... % 最大训练次数
'GradientThreshold', 1, ... % 梯度阈值
'InitialLearnRate', 0.01, ... % 初始学习率
'LearnRateSchedule', 'piecewise', ... % 学习率调整
'LearnRateDropPeriod',100, ... % 训练100次后开始调整学习率
'LearnRateDropFactor',0.001, ... % 学习率调整因子
'L2Regularization', 0.001, ... % 正则化参数
'ExecutionEnvironment', 'cpu',... % 训练环境
'Verbose', 1, ... % 关闭优化过程
'Plots', 'none'); % 画出曲线
代码效果图