Classification prediction | MATLAB implements GA-BiLSTM genetic algorithm to optimize data multi-input classification prediction of bidirectional long short-term memory network

Classification prediction | MATLAB implements GA-BiLSTM genetic algorithm to optimize data multi-input classification prediction of bidirectional long short-term memory network

List of effects

1
2
3
4
5

basic introduction

MATLAB implements GA-BiLSTM genetic algorithm to optimize data of bidirectional long-term short-term memory network Multi-input classification prediction
GA-BiLSTM genetic algorithm optimizes data of bidirectional long-term short-term memory
network Classification and multi-classification models. The comments in the program are detailed, and it can be used directly by replacing the data. The program language is matlab, and the program can produce classification effect diagrams and confusion matrix diagrams.
The operating environment is Matlab2018 and above.
Optimize the learning rate, the number of hidden layer nodes, and the regularization coefficient.
Just run the main program main, and the rest are function files that do not need to be run. All programs are placed in one folder, and data is a data set;

programming

%%  优化算法参数设置
SearchAgents_no = 5;                   % 种群数量
Max_iteration = 8;                    % 最大迭代次数
dim = 3;                               % 优化参数个数
lb = [1e-4, 10, 1e-4];                 % 参数取值下界(学习率,隐藏层节点,正则化系数)
ub = [1e-2, 30, 1e-1];                 % 参数取值上界(学习率,隐藏层节点,正则化系数)

fitness = @(x)fical(x,p_train,t_train,f_);

[Best_score,Best_pos,Convergence_curve]=GA(SearchAgents_no,Max_iteration,lb ,ub,dim,fitness)

%%  记录最佳参数
Best_pos(1, 2) = round(Best_pos(1, 2));
best_lr = Best_pos(1, 1);
best_hd = Best_pos(1, 2);
best_l2 = Best_pos(1, 3);

%%  建立模型
% ----------------------  修改模型结构时需对应修改fical.m中的模型结构  --------------------------
layers = [
    sequenceInputLayer(f_)            % 输入层
    
    bilstmLayer(best_hd)              % BiLSTM层
    reluLayer                         % Relu激活层
    

%%  参数设置
% ----------------------  修改模型参数时需对应修改fical.m中的模型参数  --------------------------
options = trainingOptions('adam', ...           % Adam 梯度下降算法
         'MaxEpochs', 500, ...                  % 最大训练次数 500
         'InitialLearnRate', best_lr, ...       % 初始学习率 best_lr
         'LearnRateSchedule', 'piecewise', ...  % 学习率下降
         'LearnRateDropFactor', 0.5, ...        % 学习率下降因子 0.1
         'LearnRateDropPeriod', 400, ...        % 经过 400 次训练后 学习率为 best_lr * 0.5
         'Shuffle', 'every-epoch', ...          % 每次训练打乱数据集
         'ValidationPatience', Inf, ...         % 关闭验证
         'L2Regularization', best_l2, ...       % 正则化参数
         'Plots', 'training-progress', ...      % 画出曲线
         'Verbose', false);

%%  训练模型
net = trainNetwork(p_train, t_train, layers, options);

%%  仿真验证
t_sim1 = predict(net, p_train);
t_sim2 = predict(net, p_test );



%本函数完成交叉操作
% pcorss                input  : 交叉概率
% lenchrom              input  : 染色体的长度
% chrom                 input  : 染色体群
% sizepop               input  : 种群规模
% ret                   output : 交叉后的染色体

for i=1:sizepop 
    
    % 随机选择两个染色体进行交叉
    pick=rand(1,2);
    while prod(pick)==0
        pick=rand(1,2);
    end
    index=ceil(pick.*sizepop);
    % 交叉概率决定是否进行交叉
    pick=rand;
    while pick==0
        pick=rand;
    end
    if pick>pcross
        continue;
    end

        % 随机选择交叉位置
        pick=rand;
        while pick==0
            pick=rand;
        end
        pos=ceil(pick.*sum(lenchrom)); %随机选择进行交叉的位置,即选择第几个变量进行交叉,注意:两个染色体交叉的位置相同
        pick=rand; %交叉开始
        v1=chrom(index(1),pos);
        v2=chrom(index(2),pos);
        chrom(index(1),pos)=pick*v2+(1-pick)*v1;
        chrom(index(2),pos)=pick*v1+(1-pick)*v2; %交叉结束
        %检验染色体2的可行性
end

References

[1] https://blog.csdn.net/kjm13182345320/article/details/129215161
[2] https://blog.csdn.net/kjm13182345320/article/details/128105718

Guess you like

Origin blog.csdn.net/kjm13182345320/article/details/131409136
Recommended