《MATLAB 神经网络43个案例分析》:第31章 思维进化算法优化BP神经网络——非线性函数拟合

《MATLAB 神经网络43个案例分析》:第31章 思维进化算法优化BP神经网络——非线性函数拟合

1. 前言

《MATLAB 神经网络43个案例分析》是MATLAB技术论坛(www.matlabsky.com)策划,由王小川老师主导,2013年北京航空航天大学出版社出版的关于MATLAB为工具的一本MATLAB实例教学书籍,是在《MATLAB神经网络30个案例分析》的基础上修改、补充而成的,秉承着“理论讲解—案例分析—应用扩展”这一特色,帮助读者更加直观、生动地学习神经网络。

《MATLAB神经网络43个案例分析》共有43章,内容涵盖常见的神经网络(BP、RBF、SOM、Hopfield、Elman、LVQ、Kohonen、GRNN、NARX等)以及相关智能算法(SVM、决策树、随机森林、极限学习机等)。同时,部分章节也涉及了常见的优化算法(遗传算法、蚁群算法等)与神经网络的结合问题。此外,《MATLAB神经网络43个案例分析》还介绍了MATLAB R2012b中神经网络工具箱的新增功能与特性,如神经网络并行计算、定制神经网络、神经网络高效编程等。

近年来随着人工智能研究的兴起,神经网络这个相关方向也迎来了又一阵研究热潮,由于其在信号处理领域中的不俗表现,神经网络方法也在不断深入应用到语音和图像方向的各种应用当中,本文结合书中案例,对其进行仿真实现,也算是进行一次重新学习,希望可以温故知新,加强并提升自己对神经网络这一方法在各领域中应用的理解与实践。自己正好在多抓鱼上入手了这本书,下面开始进行仿真示例,主要以介绍各章节中源码应用示例为主,本文主要基于MATLAB2015b(32位)平台仿真实现,这是本书第三十一章思维进化算法优化BP神经网络实例,话不多说,开始!

2. MATLAB 仿真示例

打开MATLAB,点击“主页”,点击“打开”,找到示例文件
在这里插入图片描述
选中main.m,点击“打开”

main.m源码如下:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:思维进化算法应用于优化BP神经网络的初始权值和阈值
%环境:Win7,Matlab2015b
%Modi: C.S
%时间:2022-06-20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% 思维进化算法应用于优化BP神经网络的初始权值和阈值

%% 清空环境变量
clear all
clc
warning off
tic
%% 导入数据
load data.mat
% 随机生成训练集、测试集
k = randperm(size(input,1));
N = 1900;
% 训练集——1900个样本
P_train=input(k(1:N),:)';
T_train=output(k(1:N));
% 测试集——100个样本
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
    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
    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;
    
end

%% 解码最优个体
x = best_individual;

% 前S1*S2个编码为W1
temp = x(1:S1*S2);
W1 = reshape(temp,S2,S1);

% 接着的S2*S3个编码为W2
temp = x(S1*S2+1:S1*S2+S2*S3);
W2 = reshape(temp,S3,S2);

% 接着的S2个编码为B1
temp = x(S1*S2+S2*S3+1:S1*S2+S2*S3+S2);
B1 = reshape(temp,S2,1);

%接着的S3个编码B2
temp = x(S1*S2+S2*S3+S2+1:end-1);
B2 = reshape(temp,S3,1);

% E_optimized = zeros(1,100);
% for i = 1:100
%% 创建/训练BP神经网络
net_optimized = newff(Pn_train,Tn_train,S2);
% 设置训练参数
net_optimized.trainParam.epochs = 100;
net_optimized.trainParam.show = 10;
net_optimized.trainParam.goal = 1e-4;
net_optimized.trainParam.lr = 0.1;
% 设置网络初始权值和阈值
net_optimized.IW{
    
    1,1} = W1;
net_optimized.LW{
    
    2,1} = W2;
net_optimized.b{
    
    1} = B1;
net_optimized.b{
    
    2} = B2;
% 利用新的权值和阈值进行训练
net_optimized = train(net_optimized,Pn_train,Tn_train);

%% 仿真测试
Tn_sim_optimized = sim(net_optimized,Pn_test);     
% 反归一化
T_sim_optimized = mapminmax('reverse',Tn_sim_optimized,outputps);

%% 结果对比
result_optimized = [T_test' T_sim_optimized'];
% 均方误差
E_optimized = mse(T_sim_optimized - T_test)
% end
%% 未优化的BP神经网络
% E = zeros(1,100);
% for i = 1:100
net = newff(Pn_train,Tn_train,S2);
% 设置训练参数
net.trainParam.epochs = 100;
net.trainParam.show = 10;
net.trainParam.goal = 1e-4;
net.trainParam.lr = 0.1;
% 利用新的权值和阈值进行训练
net = train(net,Pn_train,Tn_train);

%% 仿真测试
Tn_sim = sim(net,Pn_test);    
% 反归一化
T_sim = mapminmax('reverse',Tn_sim,outputps);

%% 结果对比
result = [T_test' T_sim'];
% 均方误差
E = mse(T_sim - T_test)

% end
toc

添加完毕,点击“运行”,开始仿真,输出仿真结果如下:


E_optimized =

    0.0478


E =

    0.0496

时间已过 7.668963 秒。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
依次点击Plots中的Performance,Training State,Regression可得到如下图示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 小结

思维进化算法(Mind Evolutionary Algorithm,MEA)采用趋同和异化操作,通过模仿人类思维进化的过程进行寻优,克服了早熟现象,提高了算法的搜索能力。本章仿真利用思维进化算法优化BP神经网络的权值和阈值。从而得到非线性函数的拟合结果。对本章内容感兴趣或者想充分学习了解的,建议去研习书中第三十一章节的内容。后期会对其中一些知识点在自己理解的基础上进行补充,欢迎大家一起学习交流。

猜你喜欢

转载自blog.csdn.net/sinat_34897952/article/details/125357947
今日推荐