1. 程式人生 > >【讀書1】【2017】MATLAB與深度學習——交叉熵函式(1)

【讀書1】【2017】MATLAB與深度學習——交叉熵函式(1)

當我們計算輸出節點的增量時,這裡出現了一些不同之處。

The difference arises when we calculate thedelta of the output node as:

e = d - y;

delta = e;

與前面的示例程式碼不同,這裡不再使用sigmoid函式的導數。

Unlike the previous example code, thederivative of the sigmoid function no longer exists.

這是因為,對於交叉熵函式的學習規則,如果輸出節點的啟用函式是sigmoid,那麼增量等於輸出誤差。

This is because, for the learning rule ofthe cross entropy function, if the activation function of the output node isthe sigmoid, the delta equals the output error.

當然,隱藏節點遵循以前的反向傳播演算法中使用的所有相同過程。

Of course, the hidden nodes follow the sameprocess that is used by the previous back-propagation algorithm.

e1 = W2’*delta;

delta1 = y1.*(1-y1).*e1;

以下程式清單為TestBackpropCE.m檔案的原始碼,用於測試BackpropCE函式。

The following program listing shows theTestBackpropCE.m file, which tests the BackpropCE function.

該程式呼叫BackpropCE函式並訓練神經網路10000次。

This program calls the BackpropCE functionand trains the neural network 10,000 times.

訓練後的神經網路將輸出結果顯示在螢幕上。

The trained neural network yields theoutput for the training data input, and the result is displayed on the screen.

我們通過比較訓練輸出與正確的輸出來驗證神經網路是否被有效地訓練。

We verify the proper training of the neuralnetwork by comparing the output to the correct output.

由於這裡的程式碼與以前幾乎一致,因此不再贅述。

Further explanation is omitted, as the codeis almost identical to that from before.

clear all

X = [ 0 0 1;

0 1 1;

1 0 1;

1 1 1;

   ];

D = [ 0 1 1 0];

W1 = 2*rand(4, 3) - 1;

W2 = 2*rand(1, 4) - 1;

for epoch = 1:10000 % 訓練

   [W1W2] = BackpropCE(W1, W2, X, D);

end

N = 4; % 推斷

for k = 1:N

   x = X(k, :)';

   v1 = W1*x;

   y1 = Sigmoid(v1);

   v = W2*y1;

   y = Sigmoid(v)

end

執行以上程式碼輸出以下顯示的值。

Executing this code produces the valuesshown here.

在這裡插入圖片描述

訓練輸出非常接近正確的輸出D。

The output is very close to the correctoutput, D.

從而證明了該神經網路已經被成功訓練。

This proves that the neural network hasbeen trained successfully.

代價函式比較(Comparison of Cost Functions)

上一節中的BackpropCE函式與“XOR問題”一節中BackpropXOR函式之間的唯一區別是輸出節點增量的計算。

The only difference between the BackpropCEfunction from the previous section and the BackpropXOR function from the “XORProblem” section is the calculation of the output node delta.

我們將研究這種微不足道的差異將如何影響學習效能。

We will examine how this insignificantdifference affects the learning performance.

下面的程式清單顯示了CEvsSSE.m中的內容,該程式用於比較兩個函式的平均誤差。

The following listing shows the CEvsSSE.mfile that compares the mean errors of the two functions.

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

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