UFLDL——Exercise: Linear Decoders 線性解碼器

本實驗是用線性解碼器的sparse autoencoder來訓練stl-10數據庫圖片中8*8大小RGB patch塊的特徵。之前的試驗中,我們的訓練圖像都是灰度圖像,對於RGB圖像可以採用相同的方法,只需把RGB圖像的三個通道向量按照rgb的順序排列更長的向量即可。


1、   線性解碼器簡介

線性解碼器和稀疏自動編碼的整體結構都是類似的,只是線性解碼器的輸出層的激勵函數爲恆等式f(z) = z,而稀疏自動編碼爲非線性函數,比如sigmoid函數、tanh函數等,那爲什麼爲出現兩者不同的激勵函數呢。

我們以三層神經網絡爲例,輸出層的計算公式如下:

其中a(3)是第二層的輸出,在自編碼器中,我們希望a(3)近似重構了輸入x=a(1)

 

我們在稀疏自動編碼採用sigmoid激勵函數,把輸出數據控制在[0,1]範圍,如果輸入數據能夠方便縮放到 [0,1] 中(MNIST手寫數字數據集),那稀疏自動編碼可以滿足要求。但是,有些數據很難把數據縮放到[0,1]之間(比如PCA 白化處理的輸入),如果此時輸入數據的範圍爲[0,10],仍然使用sigmoid激勵函數保證輸出爲[0,1],此時不能得到輸出重構輸入的要求,在這種情況下,線性解碼器的優勢就體現出來了。

 

2、   神經網絡結構

實驗中爲三層神經網絡,輸入層8*8*3個neuron,隱含層爲400個neuron(都不包括bias結點),輸出層爲8*8*3個neuron。


3、   數據

實驗中的數據採用的100,000個8 x 8的RGB patch塊,這些patch是從STL-10圖像集中隨機採樣得到的。STL-10數據中由5000個訓練數據和8000個訓練數據組成,每一個數據是大小爲96x96標註的彩色圖像,這數據屬於airplane, bird, car, cat, deer, dog, horse, monkey, ship, truck十個類中的一類。


4、   預處理(ZCA白化)

本實驗中採用來預處理,降低輸入數據的冗餘性,從而降低特徵之間相關性,以及使所有特徵具有相同的方差。

下圖是把訓練數據中的前100個patch進行顯示。

                                        原始數據                                          ZCA白化處理後

5、   實驗結果

下圖是通過線性解碼器從STL-10數據集中學習到的特徵。



6、   代碼

源代碼下載


sparseAutoencoderLinearCost.m文件

function [cost,grad] = sparseAutoencoderLinearCost(theta, visibleSize, hiddenSize, ...
                                             lambda, sparsityParam, beta, data)

% visibleSize: the number of input units (probably 64) 
% hiddenSize: the number of hidden units (probably 25) 
% lambda: weight decay parameter
% sparsityParam: The desired average activation for the hidden units (denoted in the lecture
%                           notes by the greek alphabet rho, which looks like a lower-case "p").
% beta: weight of sparsity penalty term
% data: Our 64x10000 matrix containing the training data.  So, data(:,i) is the i-th training example. 
  
% The input theta is a vector (because minFunc expects the parameters to be a vector). 
% We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this 
% follows the notation convention of the lecture notes. 

W1 = reshape(theta(1:hiddenSize*visibleSize), hiddenSize, visibleSize);
W2 = reshape(theta(hiddenSize*visibleSize+1:2*hiddenSize*visibleSize), visibleSize, hiddenSize);
b1 = theta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize);
b2 = theta(2*hiddenSize*visibleSize+hiddenSize+1:end);

% Cost and gradient variables (your code needs to compute these values). 
% Here, we initialize them to zeros. 
cost = 0;
W1grad = zeros(size(W1)); 
W2grad = zeros(size(W2));
b1grad = zeros(size(b1)); 
b2grad = zeros(size(b2));

%% ---------- YOUR CODE HERE --------------------------------------
%  Instructions: Compute the cost/optimization objective J_sparse(W,b) for the Sparse Autoencoder,
%                and the corresponding gradients W1grad, W2grad, b1grad, b2grad.
%
% W1grad, W2grad, b1grad and b2grad should be computed using backpropagation.
% Note that W1grad has the same dimensions as W1, b1grad has the same dimensions
% as b1, etc.  Your code should set W1grad to be the partial derivative of J_sparse(W,b) with
% respect to W1.  I.e., W1grad(i,j) should be the partial derivative of J_sparse(W,b) 
% with respect to the input parameter W1(i,j).  Thus, W1grad should be equal to the term 
% [(1/m) \Delta W^{(1)} + \lambda W^{(1)}] in the last block of pseudo-code in Section 2.2 
% of the lecture notes (and similarly for W2grad, b1grad, b2grad).
% 
% Stated differently, if we were using batch gradient descent to optimize the parameters,
% the gradient descent update to W1 would be W1 := W1 - alpha * W1grad, and similarly for W2, b1, b2. 
% 


% data = [ones(1,size(data,2)); data];

[n m] = size(data);

z2 = W1* data + repmat(b1,1,m);
a2 = sigmoid(z2);
z3 = W2 * a2 + repmat(b2,1,m); 
a3 = z3;


%J = trace((data - a3)' * (data - a3)) / (m*2);
J = sum(sum((a3-data).^2)) / (m*2);

regu = (W1(:)'*W1(:) + W2(:)'*W2(:))/2;

phat = mean(a2,2);
p = repmat(sparsityParam, size(phat));
sparse = p .* log(p ./ phat) + (1-p) .* log((1-p) ./ (1-phat));

cost = J + lambda*regu + beta * sum(sparse);

delta3 = -1* (data-a3);
delta2 = (W2'*delta3+beta*repmat(-p./phat+(1-p)./(1-phat),1,size(data,2))).*a2.*(1-a2); 


W2grad = delta3*a2'/m;  
b2grad = mean(delta3,2);  
W1grad = delta2*data'/m;  
b1grad = mean(delta2,2); 

W2grad = W2grad + lambda*W2;  
W1grad = W1grad + lambda*W1;  


%-------------------------------------------------------------------
% After computing the cost and gradient, we will convert the gradients back
% to a vector format (suitable for minFunc).  Specifically, we will unroll
% your gradient matrices into a vector.

grad = [W1grad(:) ; W2grad(:) ; b1grad(:) ; b2grad(:)];

end

%-------------------------------------------------------------------
% Here's an implementation of the sigmoid function, which you may find useful
% in your computation of the costs and the gradients.  This inputs a (row or
% column) vector (say (z1, z2, z3)) and returns (f(z1), f(z2), f(z3)). 

function sigm = sigmoid(x)
  
    sigm = 1 ./ (1 + exp(-x));
end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章