% CNN-LSTM多变量多步
% 数据集 列为特征,行为样本数目
clc
clear
load Train.mat
%
Train(1,:) =[];
y = Train.demand;
x = Train{:,3:end};
[xnorm,xopt] = mapminmax(x',0,1);
[ynorm,yopt] = mapminmax(y',0,1);
x = x';
k = 24; % 滞后长度
% 转换成4-D image
for i = 1:length(ynorm)-k
Train_xNorm{i} = reshape(xnorm(:,i:i+k-1),6,1,1,k);
Train_yNorm(i) = ynorm(i+k-1);
end
Train_yNorm= Train_yNorm';
%--------------------------------------------------------------------------
load Test.mat
Test(1,:) =[];
ytest = Test.demand;
xtest = Test{:,3:end};
[xtestnorm] = mapminmax('apply', xtest',xopt);
[ytestnorm] = mapminmax('apply',ytest',yopt);
xtest = xtest';
for i = 1:length(ytestnorm)-k
Test_xNorm{i} = reshape(xtestnorm(:,i:i+k-1),6,1,1,k);
Test_yNorm(i) = ytestnorm(i+k-1);
Test_y(i) = ytest(i+k-1);
end
Test_yNorm = Test_yNorm';
%%
% BiLSTM 层设置,参数设置
inputSize = size(Train_xNorm{1},1); %数据输入x的特征维度
outputSize = 1; %数据输出y的维度
numhidden_units1=50;
numhidden_units2= 20;
numhidden_units3=100;
opts = trainingOptions('adam', ...
'MaxEpochs',10, ...
'GradientThreshold',1,...
'ExecutionEnvironment','cpu',...
'InitialLearnRate',0.001, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',2, ... %2个epoch后学习率更新
'LearnRateDropFactor',0.5, ...
'Shuffle','once',... % 时间序列长度
'SequenceLength',k,...
'MiniBatchSize',24,...
'Verbose',0);
%% LSTM
layers = [ ...
sequenceInputLayer([inputSize,1,1],'name','input') %输入层设置
sequenceFoldingLayer('name','fold')
convolution2dLayer([2,1],10,'Stride',[1,1],'name','conv1')
batchNormalizationLayer('name','batchnorm1')
reluLayer('name','relu1')
maxPooling2dLayer([1,3],'Stride',1,'Padding','same','name','maxpool')
sequenceUnfoldingLayer('name','unfold')
flattenLayer('name','flatten')
lstmLayer(numhidden_units1,'Outputmode','sequence','name','hidden1')
dropoutLayer(0.3,'name','dropout_1')
bilstmLayer(numhidden_units2,'Outputmode','last','name','hidden2')
dropoutLayer(0.3,'name','drdiopout_2')
fullyConnectedLayer(outputSize,'name','fullconnect') % 全连接层设置(影响输出维度)(cell层出来的输出层) %
tanhLayer('name','softmax')
regressionLayer('name','output')];
lgraph = layerGraph(layers)
lgraph = connectLayers(lgraph,'fold/miniBatchSize','unfold/miniBatchSize');
%
% 网络训练
tic
CNNBiLSTMNET = trainNetwork(Train_xNorm,Train_yNorm,lgraph,opts);
%% 测试
close all
figure
Predict_Ynorm = CNNBiLSTMNET.predict(Test_xNorm);
Predict_Y = mapminmax('reverse',Predict_Ynorm',yopt);
Predict_Y = Predict_Y';
plot(Predict_Y,'-','Color',[0 0 1],'LineWidth',2,'Marker','s','MarkerSize',5,'MarkerFaceColor',[0 0 1])
hold on
plot(Test_y,'-','Color',[0.5 0.5 0.5],'LineWidth',2,'Marker','o','MarkerSize',5,'MarkerFaceColor',[0.5 0.5 0.5]);
legend('预测值','实际值')
figure
hold on
plot(Predict_Y(10:110),'gs--','LineWidth',2.0)
plot(Test_y(10:110),'ko--','LineWidth',2.0)
legend('CNN-LSTM预测值','真实值')
title('测试')
% 预测结果评价
ae= abs(Predict_Y - Test_y);
rmse = (mean(ae.^2)).^0.5;
mse = mean(ae.^2);
mae = mean(ae);
mape = mean(ae./Predict_Y);
disp('机组预测结果评价指标:')
disp(['RMSE = ', num2str(rmse)])
disp(['MSE = ', num2str(mse)])
disp(['MAE = ', num2str(mae)])
disp(['MAPE = ', num2str(mape)])
Matlab领域
- 粉丝: 3w+
- 资源: 3720
最新资源
- TRIZ矛盾矩阵表.pdf
- ssm基于SSM框架的购物商城系统+jsp.zip
- ssm基于SSM框架的个人博客网站的设计与实现+vue.zip
- ssm基于SSM框架的金鱼销售平台的开发和实现+jsp.zip
- ssm基于SSM框架的德云社票务系统的设计与实现+vue.zip
- ssm基于SSM框架的安全教育平台+vue.zip
- ssm基于ssm框架的大学生就业信息平台+jsp.zip
- ssm基于SSM的养老院老人健康监护平台设计与实现+vue.zip
- ssm基于ssm的疫情物质管理系统+jsp.zip
- ssm基于SSM的药房药品采购集中管理系统的设计与实现+vue.zip
- ssm基于SSM的乡镇篮球队管理系统+jsp.zip
- ssm基于ssm的新能源汽车在线租赁管理系统+vue.zip
- ssm基于SSM的校园二手物品交易平台+vue.zip
- ssm基于ssm的人才招聘网站+jsp.zip
- ssm基于SSM的高校疫情管理系统设计与实现+vue.zip
- ssm基于ssm的线上课程管理系统+jsp.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈