%% gru network
% Programmed by 红松科技工作室,2027771338
% Updated 6 Apr. 2024.
clc
clear
close all
%读取double格式数据
[num,ax,ay] = xlsread('shuju.xlsx',1);
n = randperm(length(num));
m=70;
input_train =num(n(1:m),1:4);%训练数据输出数据
output_train = num(n(1:m),5);%训练数据输入数据
input_test = num((m+1:end),1:4);%测试数据输出数据
output_test = num((m+1:end),5);%测试数据输入数据
[inputn,inputps]=mapminmax(input_train',-1,1);%训练数据的输入数据的归一化
[outputn,outputps]=mapminmax(output_train',-1,1);%训练数据的输出数据的归一化de
inputn_test=mapminmax('apply',input_test',inputps);
%% Define Network Architecture
% Define the network architecture.
numFeatures = 4;%输入层维度
numResponses = 1;%输出维度
% 200 hidden units
numHiddenUnits = 50;%第一层维度
% a fully connected layer of size 50 & a dropout layer with dropout probability 0.5
funbilstm
% Specify the training options.
% Train for 60 epochs with mini-batches of size 20 using the solver 'adam'
maxEpochs = 60;%最大迭代次数
miniBatchSize =2;%最小批量
% the learning rate == 0.01
% set the gradient threshold to 1
% set 'Shuffle' to 'never'
options = trainingOptions('adam', ... %解算器
'MaxEpochs',maxEpochs, ... %最大迭代次数
'MiniBatchSize',miniBatchSize, ... %最小批次
'InitialLearnRate',0.01, ... %初始学习率
'GradientThreshold',0.03, ... %梯度阈值
'Shuffle','every-epoch', ... %打乱顺序
'Plots','training-progress',... %画图
'Verbose',0); %不输出训练过程
%% Train the Network
net = trainNetwork(inputn,outputn,layers,options);%开始训练
% save maydata.mat
%% Test the Network
y_pred = predict(net,inputn_test,'MiniBatchSize',miniBatchSize)';%测试仿真输出
[y_pred]=(mapminmax('reverse',y_pred',outputps));
% y_pred0 = predict(net,inputn,'MiniBatchSize',1)';%训练拟合值
% y_pred0=(mapminmax('reverse',y_pred0',outputps))';
y_pred=(double(y_pred))';
figure%打开一个图像窗口
plot(y_pred(:,1),'k-o')%黑色实线,点的形状为*
hold on%继续画图
plot(output_test(:,1),'r--*')%红色实线,点的形状为o
hold off%停止画图
title('测试图')%标题
ylabel('线损率/%')%Y轴名称
legend('测试输出线损率/%','实际输出线损率/%')%标签
set(gca,'YTick',0:1)
error1 = y_pred-output_test;%误差
figure
plot(error1(:,1),'k-*')
title('测试误差图')
[MSE,RMSE,MBE,MAE ] =MSE_RMSE_MBE_MAE(output_test(:,1),y_pred(:,1));
result_table = table;
result_table.simd = y_pred(:,1);
result_table.trued = output_test(:,1);
writetable(result_table,'./结果.csv')