则有:
????????????? p?? yi , ?bi????????a ji
? pqi?? x j
据此编写 MATLAB 程序如下(标准 BP 算法加动量法优化):
clear x0 x1 x2; beta = 0.1; miu = 0;
[n m] = size(x); for i = 1 : 1 : n
x0(1, i) = 1; x1(1, i) = x(i, 1); x2(1, i) = x(i, 2); y0(1, i) = 1; t(1, i) = x(i, 3); end
a01 = rand(); a11 = rand(); a21 = rand(); a02 = rand(); a12 = rand(); a22 = rand(); b0 = rand(); b1 = rand(); b2 = rand(); delta_a01 = 0; delta_a11 = 0; delta_a21 = 0; delta_a02 = 0; delta_a12 = 0; delta_a22 = 0; delta_b0 = 0; delta_b1 = 0; delta_b2 = 0; l = 1; while 1
u1 = a01 * x0 + a11 * x1 + a21 * x2; u2 = a02 * x0 + a12 * x1 + a22 * x2; y1 = 2 ./ (1 + exp(-u1)) - 1; y2 = 2 ./ (1 + exp(-u2)) - 1; v = b0 * y0 + b1 * y1 + b2 * y2; z = 2 ./ (1 + exp(-v)) - 1;
Page 17 of 25
error = 0;
for i = 1 : 1 : n
if (z(1, i) > 0 && t(1, i) == 1) || (z(1, i) < 0 && t(1, i) == -1) % else
error = error + 1; end end error
temp0 = -(t - z) .* exp(-v) ./ (1 + exp(-v)) .^ 2; temp1 = b1 .* exp(-u1) ./ (1 + exp(-u1)) .^ 2; temp2 = b2 .* exp(-u2) ./ (1 + exp(-u2)) .^ 2;
delta_b0 = miu * delta_b0 + (1 - miu) * beta * sum(-temp0 .* y0); delta_b1 = miu * delta_b1 + (1 - miu) * beta * sum(-temp0 .* y1); delta_b2 = miu * delta_b2 + (1 - miu) * beta * sum(-temp0 .* y2); delta_a01 = miu * delta_a01 + (1 - miu) * beta * sum(-temp0 .* temp1 .* x0);
delta_a11 = miu * delta_a11 + (1 - miu) * beta * sum(-temp0 .* temp1 .* x1);
delta_a21 = miu * delta_a21 + (1 - miu) * beta * sum(-temp0 .* temp1 .* x2);
delta_a02 = miu * delta_a02 + (1 - miu) * beta * sum(-temp0 .* temp2 .* x0);
delta_a12 = miu * delta_a12 + (1 - miu) * beta * sum(-temp0 .* temp2 .* x1);
delta_a22 = miu * delta_a22 + (1 - miu) * beta * sum(-temp0 .* temp2 .* x2);
b0 = b0 + delta_b0; b1 = b1 + delta_b1; b2 = b2 + delta_b2; a01 = a01 + delta_a01; a11 = a11 + delta_a11; a21 = a21 + delta_a21; a02 = a02 + delta_a02; a12 = a12 + delta_a12; a22 = a22 + delta_a22; l = l + 1; if l == 1000
break; end
Page 18 of 25
end j1 = 1; j2 = 1; k1 = 1; k2 = 1;
for i = 1 : 1 : n
if x(i, 3) == -1
if z(1, i) < 0
x11(j1, :) = x(i, :); j1 = j1 + 1; else
x12(j2, :) = x(i, :); j2 = j2 + 1; end else
if z(1, i) > 0
x21(k1, :) = x(i, :); k1 = k1 + 1; else
x22(k2, :) = x(i, :); k2 = k2 + 1; end end end hold on;
plot(x11(:, 1), x11(:, 2), 'g*'); plot(x12(:, 1), x12(:, 2), 'r+'); plot(x21(:, 1), x21(:, 2), 'bo'); plot(x22(:, 1), x22(:, 2), 'r+'); hold off;
axis([0 1 0 1]);
程序运行结果,经过训练,该神经网络对 200 个样本的分类正确 率达到了 96.5%,分类效果较好,具体分类情况如下图所示,其中被
错误分类的样本已用红色标示出,其它正确分类的样本仍用原类别对
应的颜色进行标示。
Page 19 of 25
1 0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0 0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1 之前的所有程序都是完整地按照多层前向神经网络的标准 BP 学 习算法过程进行编写的,程序较为复杂,而且如果出现差错,也不易
发现,因此应用有一定难度,下面介绍使用 MATLAB 的神经网络工具
箱进行人工神经网络的设计与训练的方法。
关于神经网络工具箱的帮助信息,可以在 Product Help 中搜索
“Neural Network Toolbox”获得,包含有较为详细的使用方法,可以
在需要时进行查阅。下面以一个函数拟合的问题为例,演示如何使用
神经网络工具箱。
首先介绍几个将要用到的函数:
premnmx:用于将网络的输入数据或输出数据进行归一化,归一
化后的数据将分布在 [?1,1]区间内。其语法格式为: Page 20 of 25