【读书1】【2017】MATLAB与深度学习——异或问题(1)

以下清单为BackpropXOR.m文件的详细代码,实现了反向传播的XOR训练。

The following listing shows the BackpropXOR.mfile, which implements the BackpropXOR function.

function [W1, W2] = BackpropXOR(W1, W2, X,D)

alpha = 0.9;
N = 4;
for k = 1:N

x = X(k, ?’;
d = D(k);
v1 = W1x;
y1 = Sigmoid(v1);
v = W2
y1;
y = Sigmoid(v);
e = d - y;
delta = y.*(1-y).e;
e1 = W2’delta;
delta1 = y1.
(1-y1).e1;
dW1 = alpha
delta1
x’;
W1 = W1 + dW1;

dW2 =alphadeltay1’;
W2 = W2 + dW2;

end
end

该代码从训练数据集取点,使用增量规则计算权重更新dW,并调整权重。

The code takes point from the trainingdataset, calculates the weight update, dW, using the delta rule, and adjuststhe weights.

到目前为止,该过程与第2章的示例代码几乎相同。

So far, the process is almost identical tothat of the example code of Chapter 2.

细微的差别是用于输出计算的函数Sigmoid的两次调用以及使用输出增量反向传播实现delta (delta1)计算,即:

The slight differences are the two calls ofthe function Sigmoid for the output calculation and the addition of the delta(delta1) calculation using the back-propagation of the output delta as follows:

e1 = W2’delta;
delta1 = y1.
(1-y1).*e1;

其中误差e1的计算根据方程3.6实现。

where the calculation of the error, e1, isthe implementation of Equation 3.6.

由于这里涉及到增量的反向传播,所以我们使用转置矩阵W2’。(注意:W2’在MATLAB中表示共轭转置,因此严谨地说,应该使用W2.’才是表示矩阵转置)

As this involves the back-propagation ofthe delta, we use the transpose matrix, W2’.

delta (delta1)计算中的.*表示两个向量的点乘,即向量中相同位置的对应元素相乘。

The delta (delta1) calculation has anelement-wise product operator, .*, because the variables are vectors.Theelement-wise operator of MATLAB has a dot (period) in front of the normaloperator and performs an operation on each element of the vector.

通过运用该操作符能够同时计算来自许多节点的增量。

This operator enables simultaneouscalculations of deltas from many nodes.

考虑到向量运算形式,函数Sigmoid(由BackpropXOR代码调用)也用点除./代替了除法。

The function Sigmoid, which the BackpropXORcode calls, also replaced the division with the element-wise division ./ toaccount for the vector.

function y = Sigmoid(x)

y = 1 ./ (1 +exp(-x));
end

以上修改的Sigmoid函数可以使用向量来操作,如下面的例子所示:

The modified Sigmoid function can operateusing vectors as shown by the following example:

下面的程序列表显示了TestBackpropXOR.m文件,用于测试BackpropXOR代码。

The program listing that follows shows theTestBackpropXOR.m file, which tests the function BackpropXOR.

该程序调用BackpropXOR函数并训练神经网络10000次。

This program calls in the BackpropXORfunction and trains the neural network 10,000 times.

将数据输入被训练的网络,相应的训练输出会显示在屏幕上。

The input is given to the trained network,and its output is shown on the screen.

当我们将网络输出与训练数据的正确输出进行比较时,可以验证该神经网络的训练性能。

The training performance can be verified aswe compare the output to the correct outputs of the training data.

更多的细节被省略,因为程序几乎与第2章相同。

Further details are omitted, as the programis almost identical to that of Chapter 2.

clear all
X = [ 0 0 1;
0 1 1;
1 0 1;
1 1 1;
];
D = [ 0 1 1 0];
W1 = 2rand(4, 3) - 1;
W2 = 2
rand(1, 4) - 1;

for epoch = 1:10000 % train

[W1 W2] =BackpropXOR(W1, W2, X, D);

end

N = 4; %inference

for k = 1:N

x = X(k, ?’;

v1 = W1*x;

y1 = Sigmoid(v1);

v = W2*y1;

y = Sigmoid(v)

end

执行以上代码,并在屏幕上找到以下输出值

Execute the code, and find the followingvalues on the screen.

这些值非常接近正确的输出D,表明神经网络已经被正确地训练。

These values are very close to the correctoutput, D, indicating that the neural network has been properly trained.

现在我们已经证实,多层神经网络解决了单层网络无法正确建模的异或问题。

Now we have confirmed that the multi-layerneural network solves the XOR problem, which the single-layer network hadfailed to model properly.

——本文译自Phil Kim所著的《Matlab Deep Learning》

更多精彩文章请关注微信号:【读书1】【2017】MATLAB与深度学习——异或问题(1)_第1张图片

你可能感兴趣的:(【读书1】【2017】MATLAB与深度学习——异或问题(1))