1. 程式人生 > >【讀書1】【2017】MATLAB與深度學習——示例:MNIST(4)

【讀書1】【2017】MATLAB與深度學習——示例:MNIST(4)

程式碼中的變數y是該網路的最終輸出。

The variable y of this code is the finaloutput of the network.

x = X(:, :, k); %Input, 28x28

y1 = Conv(x, W1);% Convolution, 20x20x20

y2 = ReLU(y1); %

y3 = Pool(y2); %Pool, 10x10x20

y4 = reshape(y3,[], 1); % 2000

v5 = W5*y4; % ReLU,360

y5 = ReLU(v5); %

v = Wo*y5; % Softmax, 10

y = Softmax(v); %

現在我們得到了計算輸出,就可以計算誤差了。

Now that we have the output, the error canbe calculated.

由於網路有10個輸出節點,因此正確的輸出應該是10×1的向量,才能夠計算誤差。

As the network has 10 output nodes, thecorrect output should be in a 10×1 vector in order to calculate the error.

然而,MNIST資料給出正確的輸出作為相應的數字。

However, the MNIST data gives the correctoutput as the respective digit.

例如,如果輸入影象表示4,那麼正確的輸出為4。

For example, if the input image indicates a4, the correct output will be given as a 4.

下面的程式碼將數值的正確輸出轉換為10×1向量。

The following listing converts the numericalcorrect output into a 10×1 vector.

這裡省略了進一步的解釋。

Further explanation is omitted.

d = zeros(10, 1);

d(sub2ind(size(d), D(k), 1)) = 1;

程式碼的最後一部分是誤差的反向傳播。

The last part of the process is theback-propagation of the error.

以下程式碼顯示了從輸出層到池化層的反向傳播過程。

The following listing shows theback-propagation from the output layer to the subsequent layer to the poolinglayer.

由於本例使用交叉熵和softmax函式,所以輸出節點增量與網路輸出誤差相同。

As this example employs cross entropy andsoftmax functions, the output node delta is the same as that of the networkoutput error.

下一個隱藏層採用ReLU啟用函式。

The next hidden layer employs the ReLUactivation function.

這裡沒有什麼特別的地方。

There is nothing particular there.

隱藏層與池化層之間的連線層僅僅是訊號的重新排列。

The connecting layer between the hidden andpooling layers is just a rearrangement of the signal.

e = d - y;

delta = e;

e5 = Wo’ * delta;

delta5 = e5 .*(y5> 0);

e4 = W5’ * delta5;

e3 = reshape(e4, size(y3));

下面還有池化層和卷積層。

We have two more layers to go: the poolingand convolution layers.

以下程式碼為池化層-ReLU-卷積層之間的反向傳播。

The following listing shows theback-propagation that passes through the pooling layer-ReLU-convolution layer.

這部分的解釋超出了本書講述的範圍。

The explanation of this part is beyond thescope of this book.

當你將來需要的時候,該段程式碼可以作為參考。

Just refer to the code when you need it inthe future.

e2 = zeros(size(y2)); % Pooling

W3 = ones(size(y2)) / (2*2);

for c = 1:20

   e2(:,:, c) = kron(e3(:, :, c), ones([2 2])) .* W3(:, :, c);

end

delta2 = (y2 > 0) .* e2;

delta1_x = zeros(size(W1));

for c = 1:20

   delta1_x(:,:, c) = conv2(x(:, :), rot90(delta2(:, :, c), 2), 'valid');

end

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

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