1. 程式人生 > >【讀書1】【2017】MATLAB與深度學習——Dropout(3)

【讀書1】【2017】MATLAB與深度學習——Dropout(3)

由於向量ym已經初始化為零向量,因此未被選定的元素都是零。

The other elements are already filled withzeros, as the vector ym has been a zero matrix from the beginning.

以下程式碼為TestDeepDropout.m檔案的程式清單,該程式碼的作用是測試DeepDropout函式。

The following listing shows theTestDeepDropout.m file, which tests the DeepDropout function.

該程式呼叫DeepDropout函式,並訓練神經網路20000次。

This program calls DeepDropout and trainsthe neural network 20,000 times.

它將訓練資料輸入到訓練網路並顯示輸出。

It enters the training data into thetrained network and displays the output.

我們通過比較訓練輸出和正確的輸出來驗證神經網路訓練的充分性。

We verify the adequacy of the training bycomparing the output and correct output.

clear all

X = zeros(5, 5, 5);

X(:, :, 1) = [0 1 1 0 0;

            0 0 1 0 0;

            0 0 1 0 0;

            0 0 1 0 0;

            0 1 1 1 0

           ];

X(:, :, 2) = [1 1 1 1 0;

            0 0 0 0 1;

            0 1 1 1 0;

            1 0 0 0 0;

            1 1 1 1 1

           ];

X(:, :, 3) = [1 1 1 1 0;

            0 0 0 0 1;

            0 1 1 1 0;

            0 0 0 0 1;

            1 1 1 1 0

           ];

X(:, :, 4) = [0 0 0 1 0;

            0 0 1 1 0;

            0 1 0 1 0;

            1 1 1 1 1;

            0 0 0 1 0

         ];

X(:, :, 5) = [ 11 1 1 1;

            1 0 0 0 0;

            1 1 1 1 0;

            0 0 0 0 1;

            1 1 1 1 0

          ];

D = [ 1 0 0 00;

   01 0 0 0;

   00 1 0 0;

   00 0 1 0;

   00 0 0 1

   ];

W1 = 2*rand(20, 25) - 1;

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

W3 = 2*rand(20, 20) - 1;

W4 = 2*rand( 5, 20) - 1;

for epoch = 1:20000 % train

   [W1,W2, W3, W4] = DeepDropout(W1, W2, W3, W4, X, D);

end

N = 5; % inference

for k = 1:N

   x= reshape(X(:, :, k), 25, 1);

   v1= W1*x;

   y1 = Sigmoid(v1);

   v2 = W2*y1;

   y2 = Sigmoid(v2);

   v3 = W3*y2;

   y3 = Sigmoid(v3);

   v = W4*y3;

   y= Softmax(v)

end

此程式碼與其它測試程式碼幾乎相同。

This code is almost identical to the othertest codes.

唯一的不同是此程式碼在計算訓練網路的輸出時呼叫DeepDropout函式。

The only difference is that it calls theDeepDropout function when it calculates the output of the trained network.

其它就不再贅述了。

Further explanation is omitted.

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

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