Time series prediction | MATLAB implements PSO-BiLSTM (particle swarm optimization bidirectional long-term and short-term memory neural network) time series prediction

Time series prediction | MATLAB implements PSO-BiLSTM (particle swarm optimization bidirectional long-term and short-term memory neural network) time series prediction

predictive effect

1

2
3
4

basic introduction

Matlab is based on PSO-BiLSTM (particle swarm optimization bidirectional long-term short-term memory neural network), PSO-BiLSTM time series prediction (complete program and data) optimization parameters are learning rate, number of hidden layer nodes, regularization parameters, version 2018b and above are
required , matlab code.
Evaluation indicators include: R2, MAE, MSE, RMSE, and MAPE, etc., the code quality is extremely high, and it is convenient to learn and replace data.

Model introduction

A b is proposed based on PSO-BiLSTM (Particle Swarm Optimization Bidirectional Long Short-Term Memory Neural Network), which is improved and optimized on the basis of the BiLSTM model, so it is good at dealing with complex nonlinear problems with long-term dependencies. The key parameters of the BiLSTM model are optimized through the PSO algorithm of the adaptive learning strategy, so that the data characteristics can be matched with the network topology and the prediction accuracy can be improved.

PSO model

  • The idea of ​​particle swarm optimization comes from the research on the social behavior of birds. The simplest and most effective way for birds to prey is to search for the area where the birds closest to the food are located, and achieve group evolution through assistance and information sharing among individuals.
  • The algorithm regards the individual in the group as a particle in the multidimensional search space, each particle represents a possible solution to the problem, and its characteristic information is described by three indicators: position, speed and fitness value, and the fitness value is calculated by the fitness function Obtained, the size of the fitness value represents the quality of the particle.
  • Particles "fly" at a certain speed, and change the direction and distance of movement according to the movement experience of itself and other particles, that is, the optimal fitness value of itself and the group. Continuously iteratively find a better area to complete the optimization process in the global search space.

BiLSTM model

  • BiLSTM is a special kind of recurrent neural network. By carefully designing the "gate" structure, it avoids the gradient disappearance and gradient explosion problems caused by traditional recurrent neural networks, and can effectively learn
    long-term dependencies. Therefore, the BiLSTM model with memory function shows a strong advantage in dealing with time series prediction and classification problems.

PSO-BiLSTM model

  • The initial learning rate of BiLSTM and the number of hidden layer units are taken as the optimization objects of the PSO algorithm, and the
    position information of each particle is randomly initialized according to the value range of the hyperparameter.
  • Secondly, the BiLSTM model is established according to the hyperparameter value corresponding to the particle position, and the model is trained using the training data. The verification data is substituted into the trained model for prediction, and the
    mean square error of the model on the verification data set is used as the particle fitness value.
    2
  • The algorithm flow of PSO-BiLSTM model is as follows:
  • Step 1 Divide the experimental data into training data, verification data and test data.
  • Step 2. In the BiLSTM model, the learning rate, the number of hidden layer nodes, and the regularization parameters are used as optimization objects to initialize the adaptive PSO algorithm.
  • Step 3 Divide subgroups.
  • Step 4 Calculate the fitness value of each particle. Construct the BiLSTM model with the corresponding parameters of each particle, train with the training data, and predict with the verification data.
  • Step 5 Determine the global optimal particle position pbest and the local optimal particle position gbest according to the particle fitness value and population division results.
  • Step 6 Update the positions of ordinary particles and local optimal particles respectively according to the PSO algorithm.
  • Step 7 Judge the termination condition. If the termination condition is met, return the optimal hyperparameter value; otherwise, return to step 3.
  • Step 8 Construct BiLSTM model with optimal hyperparameters.
  • Step 9: The model is trained with the training data and verification data, the test set is used for prediction, and the prediction result is obtained.

programming

% 1. 寻找最佳参数
NN=5;                   %初始化群体个数
D=2;                    %初始化群体维数,
T=10;                   %初始化群体最迭代次数
c1=2;                   %学习因子1
c2=2;                   %学习因子2
%用线性递减因子粒子群算法
Wmax=1.2; %惯性权重最大值
Wmin=0.8; %惯性权重最小值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%每个变量的取值范围
ParticleScope(1,:)=[10 200];  % 中间层神经元个数
ParticleScope(2,:)=[0.01 0.15]; % 学习率
ParticleScope=ParticleScope';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xv=rand(NN,2*D); %首先,初始化种群个体速度和位置
for d=1:D
    xv(:,d)=xv(:,d)*(ParticleScope(2,d)-ParticleScope(1,d))+ParticleScope(1,d);  
    xv(:,D+d)=(2*xv(:,D+d)-1 )*(ParticleScope(2,d)-ParticleScope(1,d))*0.2;
end
x1=xv(:,1:D);%位置
v1=xv(:,D+1:2*D);%速度
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------初始化个体位置和适应度值-----------------
p1=x1;
pbest1=ones(NN,1);
for i=1:NN
    pbest1(i)=fitness(x1(i,:));
end
%------初始时全局最优位置和最优值---------------
gbest1=min(pbest1);
lab=find(min(pbest1)==pbest1);
g1=x1(lab,:);
gb1=ones(1,T);

References

[1] https://blog.csdn.net/kjm13182345320?spm=1010.2135.3001.5343
[2] https://mianbaoduo.com/o/bread/mbd-YpiamZpq
[3] SI Y W,YIN J. OBST-based segmentation approach to financial time series[J]. Engineering Applications of Artificial Intelligence,2013,26( 10) : 2581-2596.
[4] YUAN X,CHEN C,JIANG M,et al. Prediction Interval of Wind Power Using Parameter Optimized Beta Distribution Based LSTM Model[J]. Applied Soft Computing,2019,82:105550.143

thank you

  • Your support is the driving force for my writing!
  • Thank you for subscribing, remember to comment!

Guess you like

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