BP神经网络实例含源码.doc
《BP神经网络实例含源码.doc》由会员分享,可在线阅读,更多相关《BP神经网络实例含源码.doc(18页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-作者xxxx-日期xxxxBP神经网络实例含源码【精品文档】BP神经网络算法实现一:关于BP网络BP (Back Propagation)神经网络,即误差反传误差反向传播算法的学习过程,由信息的正向传播和误差的反向传播两个过程组成。输入层各神经元负责接收来自外界的输入信息,并传递给中间层各神经元;中间层是内部信息处理层,负责信息变换,根据信息变化能力的需求,中间层可以设计为单隐层或者多隐层结构;最后一个隐层传递到输出层各神经元的信息,经进一步处理后,完成一次学习的正向传播处理过程,由输出层向外界输出信息处理结果。当实际输出与期望输出不符时,进入误差的反向传播阶段。误差通过输出层,按误差梯度下
2、降的方式修正各层权值,向隐层、输入层逐层反传。周而复始的信息正向传播和误差反向传播过程,是各层权值不断调整的过程,也是神经网络学习训练的过程,此过程一直进行到网络输出的误差减少到可以接受的程度,或者预先设定的学习次数为止。BP网络主要应用于以下方面:函数逼近、模式识别和分类、数据压缩。BP神经网络有较强的泛化性能,使网络平滑的逼近函数,能合理的响应被训练以外的输入。同时,BP网络又有自己的限制与不足,主要表现在:需要较长的训练时间、网络训练的结果可能使得权值逼近局部最优、训练数据范围外的数据泛化能力较差。为了避免训练陷入局部最优解,本程序采用改进的BP网络训练,既加入动量因子,使得网络在最优解
3、附近有一定的震荡,跳出局部最优的范围。BP网络训练中学习速率与动量因子的选择很重要,在后面的内容中将进行详细的讨论。二:训练的函数程序中训练的函数为一个三输入一输出的非线性函数,如下所示:,网络结构为:351三:程序及相关界面(VB)1 主界面代码:Private Sub Command1_Click()form2.Visible = FalseForm3.Visible = TrueEnd SubPrivate Sub Command2_Click()form2.Visible = FalseForm1.Visible = TrueEnd SubPrivate Sub Command3_Cl
4、ick()form2.Visible = FalseForm4.Visible = TrueEnd SubPrivate Sub Command4_Click()form2.Visible = FalseForm5.Visible = TrueEnd SubPrivate Sub Command5_Click()EndEnd SubPrivate Sub Form_Load()Command3.Enabled = FalseCommand4.Enabled = FalseEnd Sub2 查看网络结构代码:Private Sub Command1_Click()Form3.Visible =
5、Falseform2.Visible = TrueEnd Sub3 网络训练 代码:Private Sub Command1_Click()Form1.Visible = Falseform2.Visible = TrueEnd SubPrivate Sub Command2_Click()Label2.Caption = 样本训练中Dim i As Integer, j As Integer, k As Integer, p As Integer, s As SingleDim Maxx(1 To 3) As Single, Minx(1 To 3) As Single, Meanx(1 T
6、o 3) As SingleDim x(1 To 3, 1 To 100) As Single, sumx(1 To 3) As Single, Temp As SingleDim Datex(1 To 3, 1 To 100) As Single, inputx(1 To 3) As Single, outputx(1 To 100) As SingleDim Ex(1 To 100) As SingleDim time(1 To 5000) As Integer, cishu(1 To 100) As IntegerDim Dv_1(1 To 5, 1 To 3) As Single, D
7、w_1(1 To 5) As SingleDim R As SingleDim Maxd As Single, Mind As SingleDim s1(1 To 5) As Single, y1(1 To 5, 1 To 100) As Single, s2 As Single, y2(1 To 100) As SingleDim deltW(1 To 100) As Single, deltV(1 To 5, 1 To 100) As SingleDim Dw(1 To 5) As Single, Dv(1 To 5, 1 To 3) As SingleDim MyIn(1 To 3) A
8、s SingleDim Errorx(1 To 5000) As SingleRandomizeFor i = 1 To 3 Maxx(i) = 0 Minx(i) = 0 Meanx(i) = 0Next iTemp = 0Maxd = 0Mind = 0For i = 1 To 5 For j = 1 To 3 Dv_1(i, j) = 0 v(i, j) = 2 * Rnd - 1 Next j Dw_1(i) = 0 w(i) = 2 * Rnd - 1Next iFor j = 1 To 3 For i = 1 To 100 x(j, i) = 4 * (2 * Rnd - 1) N
9、ext i sumx(j) = 0Next j求最值For j = 1 To 3 For i = 1 To 100 If x(j, i) = Maxx(j) Then Maxx(j) = x(j, i) End If If x(j, i) = x(j, i) - Minx(j) Then R = Maxx(j) - x(j, i) Else R = x(j, i) - Minx(j) End If Datex(j, i) = (x(j, i) - Meanx(j) / R Next iNext j期望输出For i = 1 To 100 For j = 1 To 3 inputx(j) = D
10、atex(j, i) Next j outputx(i) = 2 * (inputx(1) + Sin(inputx(2) + Exp(inputx(3)Next i输出归一化For i = 1 To 100 If Maxd = outputx(i) Then Mind = outputx(i) End IfNext iFor i = 1 To 100 Ex(i) = (outputx(i) - Mind) / (Maxd - Mind)Next i训练For s = 1 To 5000 Step 1 time(s) = s For p = 1 To 100 cishu(p) = p For
11、i = 1 To 3 MyIn(i) = Datex(i, p) Next i For i = 1 To 5 For j = 1 To 3 Temp = Temp + v(i, j) * MyIn(j) Next j s1(i) = Temp Temp = 0 Next i For i = 1 To 5 y1(i, p) = 1 / (1 + Exp(-s1(i) Next i For i = 1 To 3 Temp = y1(i, p) * w(i) + Temp Next i s2 = Temp Temp = 0 y2(p) = 1 / (1 + Exp(-s2) deltW(p) = (
12、Ex(p) - y2(p) * y2(p) * (1 - y2(p) For i = 1 To 5 deltV(i, p) = deltW(p) * w(i) * y1(i, p) * (1 - y1(i, p) Next i Next p误差 For i = 1 To 100 Temp = Temp + (Ex(i) - y2(i) 2 Next i Errorx(s) = Temp Temp = 0调整权值 For i = 1 To 5 Dw_1(i) = Dw(i) Next i For i = 1 To 5 For j = 1 To 100 Temp = Temp + deltW(j)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BP 神经网络 实例 源码
限制150内