【讀書1】【2017】MATLAB與深度學習——異或問題(1)

梅花香——苦寒來發表於2018-10-15

以下清單為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》

更多精彩文章請關注微訊號:在這裡插入圖片描述

相關文章