• 【BP回归预测】基于matlab思维进化算法优化BP神经网络回归预测【含Matlab源码 2031期】


    一、思维进化算法及BP神经网络简介

    1 思维进化算法的选择与改进
    1.1 算法选择

    深度学习与人工智能发展迅速,在疾病预测方面也起到至关重要的作用。通过对比Logistic回归模型和BP神经网络两种模型,并将模型预测结果与传统的Logistic回归模型预测结果进行分析发现,基于BP神经网络建立2型糖尿病预测模型较好,最终选择BP神经网络作为糖尿病并发症的预测模型[14]。

    1.2 算法基本思路
    在思维进化算法的解空间内,由于思维进化算法(mind evolutionary algorithm,MEA)可以记忆不止一代的进化信息,因此会随机产生不同规模大小的个体,根据这一特点可以在解空间中搜索出得分最高的、性能最好的临时个体和优胜个体。将得分最高的临时个体与优胜个体作为整个解空间的中心点,并在其周围产生和包裹一层层的新的个体。可以在整个解空间内进行子群体内部执行趋同操作,直至解空间的所有个体基本成熟后,并以该子群体中最优个体(即中心)的得分作为该子群体的得分。将整个解空间分为临时个体和优胜个体两个部分,因此其趋同与异化操作可以同时进行,互不影响和干扰,并可分别提高准确率,从而为改进BP神经网络的权值和阈值起到至关重要的作用。

    2 思维进化算法优化BP神经网络
    BP神经网络具有多层前馈神经网络,信号向前传播,误差反向传递,根据预测的误差实时调整网络的权值和阈值,进而使得预测值不断接近期望值。其训练过程如图1所示,主要包括以下步骤,首先对网格初始化,确定计算的初始权值和阈值,将样本数据输入至模型后,会计算出隐层各神经元的输入以及输出,接着计算输出层神经元的输入与输出,获取计算输出层和隐层的误差,根据误差值,对输出层进行修正,并且修改隐层的权值和阈值,判断迭代步数以及误差值是否满足设定值,进而决定是否终止训练。
    在这里插入图片描述
    图1 BP神经网络训练流程图

    由以上步骤的分析,在算法中有两个关键的参数是神经网络初始的权值和阈值,这两个值可以决定算法的效率和预测精度,为了更好地确定BP神经网络中的权值和阈值,故引入思维进化算法对其展开研究。

    思维进化算法的核心类似于人类思维的进化过程,是在遗传算法基础上衍生出来的新型进化算法,在保留遗传算法的基础啊上,提出新的“趋同”和“异化”两种新的操作算子,通过趋同、异化等操作,不断迭代使得预测值与期望值逐渐逼近。其基本流程如图2所示,在获取样本数据之后,算法会产生初始种群、优胜子种群以及临时子种群,之后子种群执行趋同操作,获取成熟的子群体,之后进行异化操作进而获取全局最优的个体,将此个体作为训练BP神经网络的初始权值和阈值,使得BP神经网络以更快的速率和精度获取预测值。

    二、部分源代码

    %% 清空环境变量
    clear
    clc
    close all
    warning off
    %% 导入数据
    load data
    data=[attributes’ strength’];
    % 输入数据
    input =data(:,1:4)‘;
    output=data(:,5)’;
    % 随机生成训练集、测试集
    k = randperm(size(input,2));
    N = 24;
    % 训练集
    P_train=input(:,k(1:N));
    T_train=output(k(1:N));
    % 测试集
    P_test=input(:,k(N+1:end));
    T_test=output(k(N+1:end));

    %% 归一化
    % 训练集
    [Pn_train,inputps] = mapminmax(P_train);
    Pn_test = mapminmax(‘apply’,P_test,inputps);
    % 测试集
    [Tn_train,outputps] = mapminmax(T_train);
    Tn_test = mapminmax(‘apply’,T_test,outputps);

    %% 参数设置
    popsize = 200; % 种群大小
    bestsize = 5; % 优胜子种群个数
    tempsize = 5; % 临时子种群个数
    SG = popsize / (bestsize+tempsize); % 子群体大小
    S1 = size(Pn_train,1); % 输入层神经元个数
    S2 = 5; % 隐含层神经元个数
    S3 = size(Tn_train,1); % 输出层神经元个数
    iter = 10; % 迭代次数

    %% 随机产生初始种群
    initpop = initpop_generate(popsize,S1,S2,S3,Pn_train,Tn_train);

    %% 产生优胜子群体和临时子群体
    % 得分排序
    [sort_val,index_val] = sort(initpop(:,end),‘descend’);
    % 产生优胜子种群和临时子种群的中心
    bestcenter = initpop(index_val(1:bestsize)😅;
    tempcenter = initpop(index_val(bestsize+1:bestsize+tempsize)😅;
    % 产生优胜子种群
    bestpop = cell(bestsize,1);
    for i = 1:bestsize
    center = bestcenter(i,:);
    bestpop{i} = subpop_generate(center,SG,S1,S2,S3,Pn_train,Tn_train);
    end
    % 产生临时子种群
    temppop = cell(tempsize,1);
    for i = 1:tempsize
    center = tempcenter(i,:);
    temppop{i} = subpop_generate(center,SG,S1,S2,S3,Pn_train,Tn_train);
    end

    while iter > 0
    %% 优胜子群体趋同操作并计算各子群体得分
    best_score = zeros(1,bestsize);
    best_mature = cell(bestsize,1);
    for i = 1:bestsize
    best_mature{i} = bestpop{i}(1,:);
    best_flag = 0; % 优胜子群体成熟标志(1表示成熟,0表示未成熟)
    while best_flag == 0
    % 判断优胜子群体是否成熟
    [best_flag,best_index] = ismature(bestpop{i});
    % 若优胜子群体尚未成熟,则以新的中心产生子种群
    if best_flag == 0
    best_newcenter = bestpop{i}(best_index,:);
    best_mature{i} = [best_mature{i};best_newcenter];
    bestpop{i} = subpop_generate(best_newcenter,SG,S1,S2,S3,Pn_train,Tn_train);
    end
    end
    % 计算成熟优胜子群体的得分
    best_score(i) = max(bestpop{i}(:,end));
    end
    % 绘图(优胜子群体趋同过程)
    figure(1)
    temp_x = 1:length(best_mature{1}(:,end))+5;
    temp_y = [best_mature{1}(:,end);repmat(best_mature{1}(end),5,1)];
    plot(temp_x,temp_y,‘b-o’)
    hold on
    temp_x = 1:length(best_mature{2}(:,end))+5;
    temp_y = [best_mature{2}(:,end);repmat(best_mature{2}(end),5,1)];
    plot(temp_x,temp_y,‘r-^’)
    hold on
    temp_x = 1:length(best_mature{3}(:,end))+5;
    temp_y = [best_mature{3}(:,end);repmat(best_mature{3}(end),5,1)];
    plot(temp_x,temp_y,‘k-s’)
    hold on
    temp_x = 1:length(best_mature{4}(:,end))+5;
    temp_y = [best_mature{4}(:,end);repmat(best_mature{4}(end),5,1)];
    plot(temp_x,temp_y,‘g-d’)
    hold on
    temp_x = 1:length(best_mature{5}(:,end))+5;
    temp_y = [best_mature{5}(:,end);repmat(best_mature{5}(end),5,1)];
    plot(temp_x,temp_y,‘m-*’)
    legend(‘子种群1’,‘子种群2’,‘子种群3’,‘子种群4’,‘子种群5’)
    xlim([1 10])
    xlabel(‘趋同次数’)
    ylabel(‘得分’)
    title(‘优胜子种群趋同过程’)

    %% 临时子群体趋同操作并计算各子群体得分
    temp_score = zeros(1,tempsize);
    temp_mature = cell(tempsize,1);
    for i = 1:tempsize
        temp_mature{i} = temppop{i}(1,:);
        temp_flag = 0;                % 临时子群体成熟标志(1表示成熟,0表示未成熟)
        while temp_flag == 0
            % 判断临时子群体是否成熟
            [temp_flag,temp_index] = ismature(temppop{i});
            % 若临时子群体尚未成熟,则以新的中心产生子种群
            if temp_flag == 0
                temp_newcenter = temppop{i}(temp_index,:);
                temp_mature{i} = [temp_mature{i};temp_newcenter];
                temppop{i} = subpop_generate(temp_newcenter,SG,S1,S2,S3,Pn_train,Tn_train);
            end
        end
        % 计算成熟临时子群体的得分
        temp_score(i) = max(temppop{i}(:,end));
    end
     % 绘图(临时子群体趋同过程)
    figure(2)
    temp_x = 1:length(temp_mature{1}(:,end))+5;
    temp_y = [temp_mature{1}(:,end);repmat(temp_mature{1}(end),5,1)];
    plot(temp_x,temp_y,'b-o')
    hold on
    temp_x = 1:length(temp_mature{2}(:,end))+5;
    temp_y = [temp_mature{2}(:,end);repmat(temp_mature{2}(end),5,1)];
    plot(temp_x,temp_y,'r-^')
    hold on
    temp_x = 1:length(temp_mature{3}(:,end))+5;
    temp_y = [temp_mature{3}(:,end);repmat(temp_mature{3}(end),5,1)];
    plot(temp_x,temp_y,'k-s')
    hold on
    temp_x = 1:length(temp_mature{4}(:,end))+5;
    temp_y = [temp_mature{4}(:,end);repmat(temp_mature{4}(end),5,1)];
    plot(temp_x,temp_y,'g-d')
    hold on
    temp_x = 1:length(temp_mature{5}(:,end))+5;
    temp_y = [temp_mature{5}(:,end);repmat(temp_mature{5}(end),5,1)];
    plot(temp_x,temp_y,'m-*')
    legend('子种群1','子种群2','子种群3','子种群4','子种群5')
    xlim([1 10])
    xlabel('趋同次数')
    ylabel('得分')
    title('临时子种群趋同过程')
    
    %% 异化操作
    [score_all,index] = sort([best_score temp_score],'descend');
    % 寻找临时子群体得分高于优胜子群体的编号
    rep_temp = index(find(index(1:bestsize) > bestsize)) - bestsize;
    % 寻找优胜子群体得分低于临时子群体的编号
    rep_best = index(find(index(bestsize+1:end) < bestsize+1) + bestsize);
    
    % 若满足替换条件
    if ~isempty(rep_temp)
        % 得分高的临时子群体替换优胜子群体
        for i = 1:length(rep_best)
            bestpop{rep_best(i)} = temppop{rep_temp(i)};
        end
        % 补充临时子群体,以保证临时子群体的个数不变
        for i = 1:length(rep_temp)
            temppop{rep_temp(i)} = initpop_generate(SG,S1,S2,S3,Pn_train,Tn_train);
        end
    else
        break;
    end
    
    %% 输出当前迭代获得的最佳个体及其得分
    if index(1) < 6
        best_individual = bestpop{index(1)}(1,:);
    else
        best_individual = temppop{index(1) - 5}(1,:);
    end
    
    iter = iter - 1;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    end

    三、运行结果

    在这里插入图片描述

    四、matlab版本及参考文献

    1 matlab版本
    2014a

    2 参考文献
    [1]韦哲,石栋栋,王能才,赵刚,王玉珍,石恒兵.基于思维进化算法优化的BP神经网络对糖尿病并发症的预测研究[J].中国医学装备. 2020,17(10)

    [2]冯培存,魏正英,张育斌,张千,张磊,贾维兵.基于思维进化算法优化BP神经网络的温室甜瓜ET0预测研究[J].节水灌溉. 2019,(09)

    3 备注
    简介此部分摘自互联网,仅供参考,若侵权,联系删除

  • 相关阅读:
    shell脚本入门——函数
    电子设备内幕:RAM和ROM小百科
    uniapp实现简单的九宫格抽奖(附源码)
    MySql安装教程
    监控、无人机摄像头RTSP协议对接腾讯云直播
    最新科技喜报!统一图像和文字生成的MiniGPT-5来了!
    C语言指向数组元素的指针变量的定义和赋值
    https SSL证书使用 git bash 解密
    墨云科技 web漏洞研究岗一面复盘
    火花塞工作原理
  • 原文地址:https://blog.csdn.net/TIQCmatlab/article/details/126292078