回归预测 | Matlab实现CPO-BiTCN-BiGRU冠豪猪算法优化双向时间卷积门控循环单元多变量回归预测
目录
效果一览
基本介绍
1.Matlab实现CPO-BiTCN-BiGRU双向时间卷积门控循环单元多变量回归预测(完整源码和数据);
2.输入多个特征,输出单个变量,回归预测,运行环境matlab2023及以上;
3.基于冠豪猪算法CPO优化的BiTCN-BiGRU模型。通过优化学习率,BiGRU的神经元个数,滤波器个数,正则化参数四个参数;命令窗口输出R2、MAE、MAPE、 RMSE多指标评价;
4.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。
5.适用对象:大学生课程设计、期末大作业和毕业设计。
程序设计
- 完整源码和数据获取方式(资源出下载):Matlab实现CPO-BiTCN-BiGRU冠豪猪算法优化双向时间卷积门控循环单元多变量回归预测。
clike
%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 导入数据
res =xlsread('data.xlsx','sheet1','A2:H104');
%% 数据分析
num_size = 0.7; % 训练集占数据集比例
outdim = 1; % 最后一列为输出
num_samples = size(res, 1); % 样本个数
res = res(randperm(num_samples), :); % 打乱数据集(不希望打乱时,注释该行)
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);
f_ = size(P_train, 1); % 输入特征维度
%% 数据归一化
[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);
% 创建输入层
layer = sequenceInputLayer(f_, Normalization = "rescale-symmetric", Name = "input");
% 创建网络图
lgraph = layerGraph(layer);
outputName = layer.Name;
% 建立网络结构 -- 残差块
for i = 1 : numBlocks
% 膨胀因子
dilationFactor = 2^(i-1);
% 创建TCN正向支路
layers = [
convolution1dLayer(filterSize, numFilters, DilationFactor = dilationFactor, Padding = "causal", Name="conv1_" + i) % 一维卷积层
layerNormalizationLayer % 层归一化
spatialDropoutLayer(dropoutFactor) % 空间丢弃层
convolution1dLayer(filterSize, numFilters, DilationFactor = dilationFactor, Padding = "causal") % 一维卷积层
layerNormalizationLayer % 层归一化
reluLayer % 激活层
spatialDropoutLayer(dropoutFactor) % 空间丢弃层
additionLayer(4, Name = "add_" + i)
];
% 添加残差块到网络
lgraph = addLayers(lgraph, layers);
% 连接卷积层到残差块
lgraph = connectLayers(lgraph, outputName, "conv1_" + i);
% 创建 TCN反向支路flip网络结构
%% 相关指标计算
% R2
R1 = 1 - norm(T_train - T_sim1')^2 / norm(T_train - mean(T_train))^2;
R2 = 1 - norm(T_test - T_sim2')^2 / norm(T_test - mean(T_test ))^2;
disp(['训练集数据的R2为:', num2str(R1)])
disp(['测试集数据的R2为:', num2str(R2)])
% MAE
mae1 = sum(abs(T_sim1' - T_train)) ./ M ;
mae2 = sum(abs(T_sim2' - T_test )) ./ N ;
disp(['训练集数据的MAE为:', num2str(mae1)])
disp(['测试集数据的MAE为:', num2str(mae2)])
% RMSE
RMSE1 = sqrt(sum((T_sim1' - T_train).^2)./M);
RMSE2 = sqrt(sum((T_test' - T_sim2).^2)./N);
disp(['训练集数据的RMSE为:', num2str(RMSE1)])
disp(['测试集数据的RMSE为:', num2str(RMSE2)])
%MAPE
MAPE1 = mean(abs((T_train - T_sim1')./T_train));
MAPE2 = mean(abs((T_test - T_sim2')./T_test));
disp(['训练集数据的MAPE为:', num2str(MAPE1)])
disp(['测试集数据的MAPE为:', num2str(MAPE2)])
参考资料
[1] https://blog.csdn.net/kjm13182345320/article/details/129215161
[2] https://blog.csdn.net/kjm13182345320/article/details/128105718