Time series prediction | MATLAB implements NGO-LSTM northern goshawk algorithm to optimize long short-term memory network time series prediction

Time series prediction | MATLAB implements NGO-LSTM northern goshawk algorithm to optimize long short-term memory network time series prediction

Prediction effect

Insert image description here
Insert image description here

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Insert image description here

basic introduction

MATLAB implements NGO-LSTM northern goshawk algorithm to optimize long short-term memory network time series prediction (complete source code and data)
1. data is a data set, a univariate time series.
2.MainNGOLSTMTS.m is the main program file, and other function files do not need to be run.
3. The command window outputs MAE, MSE, RMSEP, R^2, RPD and MAPE, and the data and program content can be obtained in the download area.
4. The optimization parameters of the Northern Goshawk algorithm are learning rate, number of hidden layer nodes, and regularization parameters.
Note that the program and data are placed in one folder, and the running environment is Matlab2018 and above.

programming

%% --------------LSTM优化----------------------
% 参数设置
SearchAgents = 5;  % 种群数量 
Max_iterations =10; % 迭代次数  

lowerbound = [1e-10 0.0001 10 ];%三个参数的下限
upperbound = [1e-2 0.002 400 ];%三个参数的上限
dim = 3;%数量,即要优化的LSTM超参数个数
 
fobj = @(x)fun(x,inputn_train,outputn_train,outputps);   %调用函数fun计算适应度函数值
%% 赋值; 
[Best_score,Best_pos,Convergence_curve]=NGO(SearchAgents,Max_iterations,lowerbound,upperbound,dim,fobj)    %% 北方苍鹰算法

%得到最优参数
L2Regularization = Best_pos(1,1); % 最佳L2正则化系数
InitialLearnRate = Best_pos(1,2); % 最佳初始学习率
NumOfUnits  =abs(round( Best_pos(1,3)));   % 最佳隐藏层节点数

%% ------------------利用优化参数重新训练LSTM并预测----------------------------
% 数据输入x的特征维度
inputSize  = size(inputn_train,1);
% 数据输出y的维度
outputSize = size(outputn_train,1);

%  设置网络结构
layers = [ ...
    sequenceInputLayer(inputSize)     %输入层,参数是输入特征维数
    lstmLayer(NumOfUnits)        %学习层,隐含层神经元的个数
    dropoutLayer(0.2)                  %权重丢失率
    fullyConnectedLayer(outputSize)   %全连接层,也就是输出的维数
    regressionLayer];    %回归层,该参数说明是在进行回归问题,而不是分类问题

% trainoption(lstm)
opts = trainingOptions('adam', ...      %优化算法
    'MaxEpochs',100, ...                %最大迭代次数
    'GradientThreshold',1,...           %梯度阈值,防止梯度爆炸
    'ExecutionEnvironment','cpu',...   %对于大型数据集合、长序列或大型网络,在 GPU 上进行预测计算通常比在 CPU 上快。其他情况下,在 CPU 上进行预测计算通常更快。
    'InitialLearnRate',InitialLearnRate, ...
    'LearnRateSchedule','piecewise', ...
    'LearnRateDropPeriod',120, ...
    'LearnRateDropFactor',0.2, ...   % 指定初始学习率 0.005,在 100 轮训练后通过乘以因子 0.2 来降低学习率。
    'L2Regularization', L2Regularization, ...       % 正则化参数
    'Verbose',false, ...         %如果将其设置为true,则有关训练进度的信息将被打印到命令窗口中。
    'Plots','training-progress'...   %构建曲线图,   若将'training-progress'替换为'none',则不画出曲线
    );   % 'MiniBatchSize',outputSize*30, ...

%  训练
LSTMnet = trainNetwork(inputn_train ,outputn_train ,layers,opts);    %  网络训练

%  预测
[LSTMnet,LSTMoutputr_train]= predictAndUpdateState(LSTMnet,inputn_train);   % 训练样本拟合值
LSTMoutput_train = mapminmax('reverse',LSTMoutputr_train,outputps);  % 数据反归一化

%网络测试输出
LSTMoutputr_test= [];
end
LSTMoutput_test= mapminmax('reverse',LSTMoutputr_test,outputps);   %反归一化
toc

%% -----------------预测结果-------------------------
%  数据格式转换
LSTM_train =LSTMoutput_train';
LSTM_test = LSTMoutput_test';

train_DATA=output_train';    %训练样本标签
test_DATA= output_test'; %测试样本标签

%%  绘图
%%  均方根误差 RMSE
error1 = sqrt(sum((LSTM_train - train_DATA).^2)./M);
error2 = sqrt(sum((LSTM_test- test_DATA).^2)./N);
%%
%决定系数
R1 = 1 - norm(train_DATA - LSTM_train)^2 / norm(train_DATA - mean(train_DATA))^2;
R2 = 1 - norm(test_DATA -  LSTM_test)^2 / norm(test_DATA -  mean(test_DATA ))^2;

%%
%均方误差 MSE
mse1 = sum((LSTM_train - train_DATA).^2)./M;
mse2 = sum((LSTM_test - test_DATA).^2)./N;
%%
%RPD 剩余预测残差
SE1=std(LSTM_train-train_DATA);
RPD1=std(train_DATA)/SE1;

SE=std(LSTM_test-test_DATA);
RPD2=std(test_DATA)/SE;
%% 平均绝对误差MAE
MAE1 = mean(abs(train_DATA - LSTM_train));
MAE2 = mean(abs(test_DATA - LSTM_test));
%% 平均绝对百分比误差MAPE
MAPE1 = mean(abs((train_DATA - LSTM_train)./train_DATA));
MAPE2 = mean(abs((test_DATA - LSTM_test)./test_DATA));

References

[1] https://blog.csdn.net/article/details/126072792?spm=1001.2014.3001.5502
[2] https://blog.csdn.net/article/details/126044265?spm=1001.2014.3001.5502

Guess you like

Origin blog.csdn.net/kjm13182345320/article/details/132909759