clc;
clear
close all
addpath(genpath(pwd))
X = xlsread('风电场预测.xlsx');
X = X(5665:8640,:); %选取3月份数据
num_samples = length(X); % 样本个数
kim = 10; % 延时步长(kim个历史数据作为自变量)
zim = 1; % 跨zim个时间点进行预测
or_dim = size(X,2);
% 重构数据集
for i = 1: num_samples - kim - zim + 1
res(i, :) = [reshape(X(i: i + kim - 1,:), 1, kim*or_dim), X(i + kim + zim - 1,:)];
end
% 训练集和测试集划分
outdim = 1; % 最后一列为输出
num_size = 0.9; % 训练集占数据集比例
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_Attention模型
outputSize = 1; %数据输出y的维度
numFilters = 64;
filterSize = 5;
dropoutFactor = 0.1;
numBlocks = 2;
layer = sequenceInputLayer(f_,Normalization="rescale-symmetric",Name="input");
lgraph = layerGraph(layer);
outputName = layer.Name;
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(10,"Name","gru1");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
FlipLayer("flip3")
gruLayer(10,"Name","gru2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
concatenationLayer(1,2,"Name","concat")
selfAttentionLayer(1,50,"Name","selfattention") %单头注意力Attention机制,把1改为2,3,4……即为多头,后面的50是键值
fullyConnectedLayer(outdim,"Name","fc")
regressionLayer("Name","regressionoutput")];
lgraph = addLayers(lgraph,tempLayers);
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', 60, ... % 最大训练次数
'GradientThreshold', 1, ... % 梯度阈值
'InitialLearnRate', 0.01, ... % 初始学习率
'L2Regularization', 0.0001, ... % 正则化参数
'ExecutionEnvironment', 'cpu',... % 训练环境
'Verbose', 1, ... % 关闭优化过程
'Plots', 'none'); % 画出曲线
% 网络训练
tic
net0 = trainNetwork(vp_train,vt_train,lgraph,options0);
toc
%% 测试与评估
t_sim = net0.predict(vp_test);
analyzeNetwork(net0);% 查看网络结构
% 数据反归一化
T_sim = mapminmax('reverse', t_sim, ps_output);
% 数据格式转换
T_sim = cell2mat(T_sim);
T_sim = T_sim';
disp(' ')
disp('优化TCN_BiGRU_attention神经网络:')
popsize=2; %初始种群规模
maxgen=10; %最大进化代数
fobj = @(x)objectiveFunction(x,f_,vp_train,vt_train,vp_test,T_test,ps_output);
% GWO优化参数设置
lb = [0.001 10 2 0.0001]; %参数的下限。分别是学习率,biGRU的神经元个数,注意力机制的键值, 正则化参数
ub = [0.01 50 50 0.001]; %参数的上限
dim = length(lb);%数量
[GWOt_score,GWOt_pos,GWO_curve]=GWO(popsize,maxgen,lb,ub,dim,fobj);%%修改函数名字即可完成优化算法的替换
setdemorandstream(pi);
%% 绘制进化曲线
figure
plot(GWO_curve,'r-','linewidth',2)
xlabel('进化代数')
ylabel('均方误差')
legend('最佳适应度')
title('优化算法进化曲线')
%% 把最佳参数GWOt_pos回带
[~,optimize_T_sim] = objectiveFunction(GWOt_pos,f_,vp_train,vt_train,vp_test,T_test,ps_output);
setdemorandstream(pi);
%% 比较算法预测值
str={'真实值','TCN-BiGRU-Attention','优化后TCN-BiGRU-Attention'};
figure('Units', 'pixels', ...
'Position', [300 300 860 370]);
plot(T_test,'-','Color',[0.8500 0.3250 0.0980])
hold on
plot(T_sim,'-.','Color',[0.4940 0.1840 0.5560])
hold on
plot(optimize_T_sim,'-','Color',[0.4660 0.6740 0.1880])
legend(str)
set (gca,"FontSize",12,'LineWidth',1.2)
box off
legend Box off
%% 比较算法误差
test_y = T_test;
Test_all = [];
y_test_predict = T_sim;
[test_MAE,test_MAPE,test_MSE,test_RMSE,test_R2]=calc_error(y_test_predict,test_y);
Test_all=[Test_all;test_MAE test_MAPE test_MSE test_RMSE test_R2];
y_test_predict = optimize_T_sim;
[test_MAE,test_MAPE,test_MSE,test_RMSE,test_R2]=calc_error(y_test_predict,test_y);
Test_all=[Test_all;test_MAE test_MAPE test_MSE test_RMSE test_R2];
str={'真实值','TCN-BiGRU-Attention','优化后TCN-BiGRU-Attention'};
str1=str(2:end);
str2={'MAE','MAPE','MSE','RMSE','R2'};
data_out=array2table(Test_all);
data_out.Properties.VariableNames=str2;
data_out.Properties.RowNames=str1;
disp(data_out)
%% 柱状图 MAE MAPE RMSE 柱状图适合量纲差别不大的
color= [0.66669 0.1206 0.108
0.1339 0.7882 0.8588
0.1525 0.6645 0.1290
0.8549 0.9373 0.8275
0.1551 0.2176 0.8627
0.7843 0.1412 0.1373
0.2000 0.9213 0.8176
0.5569 0.8118 0.7882
1.0000 0.5333 0.5176];
figure('Units', 'pixels', ...
'Position', [300 300 660 375]);
plot_data_t=Test_all(:,[1,2,4])';
b=bar(plot_data_t,0.8);
hold on
for i = 1 : size(plot_data_t,2)
x_data(:, i) = b(i).XEndPoints';
end
for i =1:size(plot_data_t,2)
b(i).FaceColor = color(i,:);
b(i).EdgeColor=[0.3353 0.3314 0.6431];
b(i).LineWidth=1.2;
end
for i = 1 : size(plot_data_t,1)-1
xilnk=(x_data(i, end)+ x_data(i+1, 1))/2;
b1=xline(xilnk,'--','LineWidth',1.2);
hold on
end
ax=gca;
legend(b,str1,'Location','GWOt')
ax.XTickLabels ={'MAE', 'MAPE', 'RMSE'};
set(gca,"FontSize",10,"LineWidth",1)
box off
legend box off
%% 二维图
figure
plot_data_t1=Test_all(:,[1,5])';
MarkerType={'s','o','pentagram','^','v'};
没有合适的资源?快使用搜索试试~ 我知道了~
灰狼算法优化时间卷积双向门控循环单元融合注意力机制GWO-TCN-BiGRU-Attention预测附matlab代码.rar
共13个文件
m:9个
png:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 125 浏览量
2024-06-14
20:43:17
上传
评论
收藏 130KB RAR 举报
温馨提示
1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
资源推荐
资源详情
资源评论
收起资源包目录
【TCN回归预测】基于灰狼算法优化时间卷积双向门控循环单元融合注意力机制GWO-TCN-BiGRU-Attention实现光伏多变量时间序列预测附matlab代码.rar (13个子文件)
calc_error.m 719B
Bounds.m 159B
initialization.m 567B
MAIN.m 9KB
3.png 38KB
1.png 38KB
radarChart.m 8KB
untitled.png 41KB
FlipLayer.m 256B
objectiveFunction.m 4KB
GWO.m 4KB
data_process.m 195B
2.png 10KB
共 13 条
- 1
资源评论
matlab科研助手
- 粉丝: 3w+
- 资源: 5962
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 时间序列-白银-5秒数据
- c++练习题目:通讯录管理系统
- java仓库管理系统源码数据库 MySQL源码类型 WebForm
- 同济大学作业之-LPC分析(男声变女声)和PCM编码
- java超市订单管理系统源码数据库 MySQL源码类型 WebForm
- 记录windows安装nvm:nvm-setup-2024-11-16.exe.zip
- 同济大学数字信号处理实验(包含实验报告)
- Kettle 是Kettle E.T.T.L. Envirnonment只取首字母的缩写,这意味着它被设计用来帮助你实现你的
- java微信小程序B2C商城 H5+APP源码 前后端分离数据库 MySQL源码类型 WebForm
- matplotlib 绘制随机漫步图
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功