当前位置 : 主页 > 编程语言 > python >

【BP预测】基于BP神经网络实现数据预测含Matlab源码

来源:互联网 收集:自由互联 发布时间:2022-06-18
1 简介 BP神经网络模型是目前应用最为广泛神经网络之一。它的本质是通过对历史数据的学习找出数据变化趋势之间的非线性关系,并通过输出量与预期值之间的误差不断调整网络中

1 简介


BP神经网络模型是目前应用最为广泛神经网络之一。它的本质是通过对历史数据的学习找出数据变化趋势之间的非线性关系,并通过输出量与预期值之间的误差不断调整网络中各个单元的权重,使整个网络的误差最小。因此,为达到较好的预测精度,需要对网络预测模型自身的结构进行确定。

1)网络层数的设计。本文需要构建的预测模型,主要是用于研究在短时间交通流走势。在这种情况下,不需选择增加网络层数的办法而是选择增加隐含层神经元节点的数目来提高输出结果的精度。因此,本文选用单一隐层的 BP神经网络模型。

2)输入层神经节点的设计。在单因素预测中仅使用交通流作为原始数据.

3)传递函数和学习函数的设计。本文所设计的模型均采用了相同的隐含层传递函数tansig、输出层传递函数logsig和学习函数learngdm。

4)性能函数的确定。网络误差能直观的反映预测效果的好坏程度,是预测精度的具体反映。本文在构建 BP神经网络模型时选择均方误差来确定网络的误差情况。

5)隐含层神经节点的设计。在模型中其它参数值保持不变的情况下,本文通过调整隐含层神经节点的数目进行重复实验,通过对比输出误差,确定最佳隐含层神经元节点的数目。对于单因素 BP神经网络,当隐含层神经元节点的数目为24时,BP神经网络的均方误差最小,即对函数的逼近效果最好,此时的均方误差为1.1609;对于多因素 BP神经网络,隐含层神经元节点数目为5时,BP神经网络的均方误差达到最小,最小值为0.0126。根据以上分析,单因素 BP神经网络预测模型的结构为:单一隐含层和单一输出层;输入层神经节点数目为5 ;隐含层神经节点数目为24;输出层神经节点数目为1;隐含层传递函数、输出层传递函数、学习函数分别为tansig、logsig和learngdm;性能函数为 mse。

2 部分代码

function varargout = bp_demo(varargin)
% BP_DEMO M-file for bp_demo.fig
% BP_DEMO, by itself, creates a new BP_DEMO or raises the existing
% singleton*.
%
% H = BP_DEMO returns the handle to a new BP_DEMO or the handle to
% the existing singleton*.
%
% BP_DEMO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BP_DEMO.M with the given input arguments.
%
% BP_DEMO('Property','Value',...) creates a new BP_DEMO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before bp_demo_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to bp_demo_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help bp_demo
% Last Modified by GUIDE v2.5 19-May-2021 09:29:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @bp_demo_OpeningFcn, ...
'gui_OutputFcn', @bp_demo_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before bp_demo is made visible.
function bp_demo_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to bp_demo (see VARARGIN)
% Choose default command line output for bp_demo
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes bp_demo wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = bp_demo_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function t_sim_Callback(hObject, eventdata, handles)
% hObject handle to t_sim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of t_sim as text
% str2double(get(hObject,'String')) returns contents of t_sim as a double
% --- Executes during object creation, after setting all properties.
function t_sim_CreateFcn(hObject, eventdata, handles)
% hObject handle to t_sim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in train_button.
function train_button_Callback(hObject, eventdata, handles)
% hObject handle to train_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
warning off
global net P_train T_train N Epochs Show Goal Lr fcn_hidden fcn_output fcn_learn
% default value
N=10;
Epochs=1000;
Show=10;
Goal=1e-3;
Lr=0.01;
fcn_hidden='tansig';
fcn_output='purelin';
fcn_learn='trainlm';
% create neural network
net=newff(minmax(P_train),[N size(T_train,1)],{fcn_hidden,fcn_output},fcn_learn);
net.trainParam.epochs=Epochs;
net.trainParam.show=Show;
net.trainParam.goal=Goal;
net.trainParam.lr=Lr;
net.trainParam.showwindow=0;
[net,tr]=train(net,P_train,T_train);
% show waitbar
perf=tr.perf;
perf_t=abs(perf(2:end)-perf(1))/(perf(1)-perf(end));
h=waitbar(0,'please wait.......');
for i=1:length(perf_t)
waitbar(perf_t(i));
end
close(h);

3 运行结果

【BP预测】基于BP神经网络实现数据预测含Matlab源码_神经网络

4 参考文献

[1]高宁. 基于BP神经网络的农作物虫情预测预报及其MATLAB实现. Diss. 安徽农业大学.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

【BP预测】基于BP神经网络实现数据预测含Matlab源码_神经网络模型_02



网友评论