基于粒子群优化算法的BP神经网络预测模型(Matlab代码实现)

 

目录

1 概述

2 粒子群优化算法

3 BP神经网络

4 PSO优化 BP网络算法

5 运行结果

6 参考文献 

7 Matlab代码实现


1 概述

在工程应用中经常会遇到一些复杂的非线性系统,这些系统的状态方程复杂,难以准确的用数学方法建模,而BP神经网络实质上实现了一个从输入到输出的映射功能,数学理论证明三层的神经网络就能够以任意精度逼近任何非线性连续函数,具有非常强的非线性映射能力,使得其特别适合于求解内部机制复杂的问题.该方法是把未知系统看成是一个黑箱,首先用系统输入输出数据训练BP网络,使得网络能够表达该未知非线性函数,然后用训练好的网络预测系统输出".但是传统的BP网络算法具有收敛速度慢、容易陷入局部极值和误差比较大等缺点,因此优化这种网络成了必要.粒子群优化算法作为一种智能算法,是一种仿生算法和随机搜索算法,参数少,寻优能力较好,将其和BP网络结合起来,优化BP网络的权值和阈值,弥补了BP网络的一些缺点,提高了BP网络拟合函数的能力.

2 粒子群优化算法

粒子群优化算法是一种群体智能的优化算法,它是源于对鸟类捕食行为的研究,鸟类捕食时,每只鸟找到食物最简单最有效的方法就是搜寻当前距离食物最近的鸟的周围区域. PSO算法是从这种生物种群行为特征中得到启发并求解优化问题的.算法中每个粒子都代表问题的一个潜在解,每个粒子对应一个由适应度函数决定的适应度值,粒子的速度决定了其移动的方向和距离,速度随自身及其他粒子的移动经验进行动态调整,从而实现个体在可解空间中的寻优.

粒子群算法(带约束处理)——Python&Matlab实现

3 BP神经网络

BP神经网络是一种多层前馈神经网络,拓扑结构包括:输入层、隐层、输出层,它的主要特点是信号前向传递,误差反向传播.在前向传递中,输人信号从输人层经隐含层逐层处理,直至输出层,每--层的神经元状态只影响下一层神经元状态.如果输出层得不到期望输出,则转入反向传播,根据预测误差调整网络权值和阈值,从而使网络预测输出不断逼近期望输出. BP网络训练的步骤如下:

4 PSO优化 BP网络算法

BP网络的非线性泛化能力很强,但是其收敛速度慢,容易陷人局部极值,误差比较大,为了弥补BP网络的这种缺点,将PSO算法与BP网络结合,用PSO算法优化BP网络的权值和阈值,提高了网络的非线性拟合能力. PSO-BP算法如下:
(1)初始化网络的训练样本数m,测试样本数n ,隐层节点数hidden _.num,粒子群数目particle_num,迭代次数epoch ,惯性权重w ,加速度因子c,C2.
(2)根据目标函数获得训练样本和测试样本的输人和理想输出,并画出理想曲线和测试样本点.(3)随机初始化粒子群的位置和速度,并计算各粒子的适应度值.
(4)根据上述公式更新各个粒子的位置和速度,并记录每个粒子的最佳位置.(5)记录全局最优位置.
(6)计算测试样本的输出,并画出预测曲线及误差曲线.

部分代码:

%% 清空环境变量
tic
clc
clear
close all
format compact
%% 导入数据
load data1
input=In';
output=U3;
%%
% 随机生成训练集、测试集
rand('seed',0)

k = randperm(size(input,1));
m=7100;
P_train=input(k(1:m),:)';
T_train=output(k(1:m));

P_test=input(k(m+1:end),:)';
T_test=output(k(m+1:end));

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

%% 节点个数
inputnum=size(Pn_train,1);
hiddennum=5;
outputnum=1;
%% 没有优化的bp
net=newff(Pn_train,Tn_train,hiddennum);
net.trainParam.epochs=200;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00000001;
net.trainParam.max_fail = 200;

%网络训练
[net,per2]=train(net,Pn_train,Tn_train);
an=sim(net,Pn_test);
error=an-Tn_test;

test_simu=mapminmax('reverse',an,outputps);
disp('优化前')
E1=norm(error);
E2=mse(error)
MAPE=mean(abs(error)./Tn_test);

figure
plot(test_simu)
hold on
plot(T_test)
legend('实际输出','期望输出')

%% 粒子群优化bp


% [bestchrom,trace]=psoforbp(inputnum,hiddennum,outputnum,Pn_train,Tn_train);%粒子群算法
% x=bestchrom;
% save result x
load result%直接调用训练好的
% 用pso优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);

net.iw{1,1}=reshape(w1,hiddennum,inputnum);
net.lw{2,1}=reshape(w2,outputnum,hiddennum);
net.b{1}=reshape(B1,hiddennum,1);
net.b{2}=B2';

%% BP网络训练
%网络进化参数
net.trainParam.epochs=200;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00000001;
net.trainParam.max_fail = 200;

%网络训练
[net,per2]=train(net,Pn_train,Tn_train);

%% BP网络预测
%数据归一化
an=sim(net,Pn_test);
error=an-Tn_test;

test_simu=mapminmax('reverse',an,outputps);
disp('优化后')
E1=norm(error);
E2=mse(error)
MAPE=mean(abs(error)./Tn_test);
toc
%%
figure
plot(test_simu)
hold on
plot(T_test)
legend('实际输出','期望输出')

5 运行结果


 

 

 

 

6 参考文献 

部分理论引用网络文献,如有侵权请联系删除。

[1]郝海霞.用粒子群算法优化BP神经网络进行函数拟合[J].山西师范大学学报(自然科学版),2017,31(01):14-16.DOI:10.16207/j.cnki.1009-4490.2017.01.004.

7 Matlab代码实现

粒子群优化算法是一种新颖的仿生、群智能优化算法。该算法原理简单、需调整的参数少、收敛速度快而且易于实现,因此近年来粒子群算法引起了广大学者的关注。然而到目前为止粒子群算法的在理论分析和实践应用方面尚未成熟,仍有大量的问题需进一步研究。本文针对粒子群算法易出现“早熟”陷入局部极小值问题对标准粒子群算法进行改进并将改进的粒子群算法应用于BP神经网络中。本文的主要工作如下:本文首先介绍了粒子群算法的国内外的研究现状与发展概况,较系统地分析了粒子群优化算法的基本理论,总结常见的改进的粒子群优化算法。其次介绍了Hooke-Jeeves模式搜索法的算法分析、基本流程及应用领域。针对标准粒子群优化算法存在“早熟”问题,易陷入局部极小值的缺点,本文对标准粒子群算法进行改进。首先将原始定义的初始种群划分为两个相同的子种群,采用基于适应度支配的思想分别将每个子种群划分为两个子集,Pareto子集和N_Pareto子集;然后将两个子群中的适应度较优的两个Pareto子集合为新种群。Griewank和Rastrigin由于新种群的参数设置区别于标准粒子群算法的参数设置,新的粒子与标准种群中的粒子飞行轨迹不同,种群的探索范围扩大,从而使算法的全局搜索能力有所提高。 为平衡粒子群算法的全局寻优能力和局部寻优能力,提高粒子群算法的求解精度和效率,本文在新种群寻优过程中引入具有强收敛能力Hooke-Jeeves搜索法,提出了IMPSO算法。雅文网www.lunwendingzhi.com,并用IMPSO算法对标准基准测试函数进行实验,将得到的实验结果并与标准粒子群算法对基准函数的实验结果进行对比,仿真结果证明了该改进的粒子群算法的有效性。 最后本文研究改进的粒子群算法BP神经网络中的应用。首先介绍人工神经网络的原理及基于BP算法的多层前馈神经网络,其次用IMPSO算法训练BP神经网络并给出训练流程图。 将IMPSO算法训练的BP神经网络分别应用于齿轮热处理中硬化层深的预测以及用于柴油机的缸盖与缸壁的故障诊断中,并将预测结果、诊断结果与BP神经网络、标准粒子群优化算法训练的BP神经网络的实验结果进行对比,实验结果证明了改进的粒子群算法训练BP网络具有更强的优化性能和学习能力。 英文简介: Particle swarm optimization algorithm is a novel bionic, swarm intelligence optimization algorithm. The algorithm principle is simple, less need to adjust the parameters and convergence speed is fast and easy to implement, so in recent years, particle swarm optimization (pso) to cause the attention of many scholars. So far, however, the particle swarm algorithm are not mature in theory analysis and practice applications, there are still a lot of problems need further research. Based on particle swarm algorithm is prone to "premature" into a local minimum value problem to improve the standard particle swarm algorithm and improved particle swarm optimization (pso) algorithm was applied to BP neural network. This paper's main work is as follows: at first, this paper introduces the particle swarm algorithm in the general situation of the research status and development at home and abroad, systematically analyzes the basic theory of particle swarm optimization algorithm, summarizes the common improved particle swarm optimization algorithm. Secondly introduces the analysis method of Hooke - Jeeves pattern search algorithm, the basic process and application fields. In view of the standard particle swarm optimization algorithm "precocious" problems, easy to fall into local minimum value, in this paper, the standard particle swarm algorithm was improved. First of all, the original definition of the initial population is divided into two identical sub populations, based on the fitness of thought respectively each child population is divided into two subsets, and Pareto subset N_Pareto subset; And then has a better fitness of two subgroups of two Pareto set for the new population. Griewank and Rastrigin because of the new population parameter setting differs from the standard particle swarm algorithm of the parameter is set, the new particles and particle trajectories in the different standard population, population expanding, which makes the algorithm's global search ability have improved. To balance the global search capability of the particle swarm algorithm and local optimization ability, and improve the precision and efficiency of particle swarm optimization (pso) algorithm, introduced in this article in the new population optimization process has a strong convergence ability to search method of Hooke - Jeeves, IMPSO algorithm is proposed. And standard benchmark test functions with IMPSO algorithm experiment, will receive the results with the standard particle swarm algorithm, comparing the experimental results of benchmark functions, the simulation results prove the validity of the improved particle swarm algorithm. At the end of the paper research the improved particle swarm algorithm in the application of the BP neural network. First this paper introduces the principle of artificial neural network and based on the multi-layer feed-forward neural network BP algorithm, secondly by IMPSO algorithm training the BP neural network and training flow chart is given. IMPSO algorithm training the BP neural network respectively used in the gear heat treatment hardening layer depth prediction and used for fault diagnosis of diesel engine cylinder head and cylinder wall, and the predicted results, the diagnostic results, the standard particle swarm optimization algorithm with BP neural network of training BP neural network, comparing the experimental results of the experimental results show that the improved particle swarm optimization (pso) training BP network has better optimization performance and learning ability.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝科研社

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值