UFLDL教程: Exercise:Learning color features with Sparse Autoencoders
Linear Decoders
Deep Learning and Unsupervised Feature Learning Tutorial Solutions
以三層的稀疏編碼神經網絡而言,在sparse autoencoder中的輸出層滿足下面的公式
從公式中可以看出,a3的輸出值是f函數的輸出,而在普通的sparse autoencoder中f函數一般為sigmoid函數,所以其輸出值的范圍為(0,1),所以可以知道a3的輸出值范圍也在0到1之間。
另外我們知道,在稀疏模型中的輸出層應該是盡量和輸入層特征相同,也就是說a3=x1,這樣就可以推導出x1也是在0和1之間,那就是要求我們對輸入到網絡中的數據要先變換到0和1之間,這一條件雖然在有些領域滿足,比如前面實驗中的MINIST數字識別。
但是有些領域,比如說使用了PCA Whitening后的數據,其范圍卻不一定在0和1之間。因此Linear Decoder方法就出現了。Linear Decoder是指在隱含層采用的激發函數是sigmoid函數,而在輸出層的激發函數采用的是線性函數,比如說最特別的線性函數——等值函數。此時,也就是說輸出層滿足下面公式:
一個 S 型或 tanh 隱含層以及線性輸出層構成的自編碼器,我們稱為線性解碼器。
隨著輸出單元的激勵函數的改變,這個輸出單元梯度也相應變化。回顧之前每一個輸出單元誤差項定義為:
其中 y = x 是所期望的輸出, 是自編碼器的輸出, 是激勵函數.因為在輸出層激勵函數為 f(z) = z, 這樣 f’(z) = 1,所以上述公式可以簡化為
當然,若使用反向傳播算法來計算隱含層的誤差項時:
因為隱含層采用一個 S 型(或 tanh)的激勵函數 f,在上述公式中, 依然是 S 型(或 tanh)函數的導數。
這樣在用BP算法進行梯度的求解時,只需要更改誤差的計算公式而已,改成如下公式
實驗步驟
1.初始化參數,編寫計算線性解碼器代價函數及其梯度的函數sparseAutoencoderLinearCost.m,主要是在sparseAutoencoderCost.m的基礎上稍微修改,然后再檢查其梯度實現是否正確。
2.加載數據并原始數據進行ZCA Whitening的預處理。
3.學習特征,即用LBFG算法訓練整個線性解碼器網絡,得到整個網絡權值optTheta。
4.可視化第一層學習到的特征。
linearDecoderExercise.m
%% CS294A/CS294W Linear Decoder Exercise% Instructions % ------------ % % This file contains code that helps you get started on the % linear decoder exericse. For this exercise, you will only need to modify % the code in sparseAutoencoderLinearCost.m. You will not need to modify % any code in this file.%%====================================================================== %% STEP 0: Initialization % Here we initialize some parameters used for the exercise.imageChannels = 3; % number of channels (rgb, so 3)patchDim = 8; % patch dimension numPatches = 100000; % number of patchesvisibleSize = patchDim * patchDim * imageChannels; % number of input units outputSize = visibleSize; % number of output units hiddenSize = 400; % number of hidden units sparsityParam = 0.035; % desired average activation of the hidden units. lambda = 3e-3; % weight decay parameter beta = 5; % weight of sparsity penalty term epsilon = 0.1; % epsilon for ZCA whitening%%====================================================================== %% STEP 1: Create and modify sparseAutoencoderLinearCost.m to use a linear decoder, % and check gradients % You should copy sparseAutoencoderCost.m from your earlier exercise % and rename it to sparseAutoencoderLinearCost.m. % Then you need to rename the function from sparseAutoencoderCost to % sparseAutoencoderLinearCost, and modify it so that the sparse autoencoder % uses a linear decoder instead. Once that is done, you should check % your gradients to verify that they are correct.% NOTE: Modify sparseAutoencoderCost first!% To speed up gradient checking, we will use a reduced network and some % dummy patchesdebugHiddenSize = 5; debugvisibleSize = 8; patches = rand([8 10]);%隨機產生10個樣本,每個樣本為一個8維的列向量,元素值為0~1 theta = initializeParameters(debugHiddenSize, debugvisibleSize); [cost, grad] = sparseAutoencoderLinearCost(theta, debugvisibleSize, debugHiddenSize, ...lambda, sparsityParam, beta, ...patches);% Check gradients numGrad = computeNumericalGradient( @(x) sparseAutoencoderLinearCost(x, debugvisibleSize, debugHiddenSize, ...lambda, sparsityParam, beta, ...patches), theta);% Use this to visually compare the gradients side by side disp([numGrad grad]); diff = norm(numGrad-grad)/norm(numGrad+grad); % Should be small. In our implementation, these values are usually less than 1e-9. disp(diff); assert(diff < 1e-9, 'Difference too large. Check your gradient computation again');% NOTE: Once your gradients check out, you should run step 0 again to % reinitialize the parameters %}%%====================================================================== %% STEP 2: Learn features on small patches從pathes中學習特征 % In this step, you will use your sparse autoencoder (which now uses a % linear decoder) to learn features on small patches sampled from related % images.%% STEP 2a: Load patches 加載數據 % In this step, we load 100k patches sampled from the STL10 dataset and % visualize them. Note that these patches have been scaled to [0,1]load stlSampledPatches.mat %里面自己定義了變量patches的值displayColorNetwork(patches(:, 1:100));%% STEP 2b: Apply preprocessing預處理 % In this sub-step, we preprocess the sampled patches, in particular, % ZCA whitening them. % % In a later exercise on convolution and pooling, you will need to replicate % exactly the preprocessing steps you apply to these patches before % using the autoencoder to learn features on them. Hence, we will save the % ZCA whitening and mean image matrices together with the learned features % later on.% Subtract mean patch (hence zeroing the mean of the patches) meanPatch = mean(patches, 2); %注意這里減掉的是每一維屬性的均值 %為什么是對每行求平均,以前是對每列即每個樣本求平均呀?因為以前是灰度圖,現在是彩色圖,如果現在對每列平均就是對三個通道求平均,這肯定不行 patches = bsxfun(@minus, patches, meanPatch);%每一維都均值化% Apply ZCA whitening sigma = patches * patches' / numPatches;%協方差矩陣 [u, s, v] = svd(sigma); ZCAWhite = u * diag(1 ./ sqrt(diag(s) + epsilon)) * u';%求出ZCAWhitening矩陣 patches = ZCAWhite * patches;displayColorNetwork(patches(:, 1:100));%% STEP 2c: Learn features % You will now use your sparse autoencoder (with linear decoder) to learn % features on the preprocessed patches. This should take around 45 minutes.theta = initializeParameters(hiddenSize, visibleSize);% Use minFunc to minimize the function addpath minFunc/options = struct; options.Method = 'lbfgs'; options.maxIter = 400; options.display = 'on';[optTheta, cost] = minFunc( @(p) sparseAutoencoderLinearCost(p, ...visibleSize, hiddenSize, ...lambda, sparsityParam, ...beta, patches), ...theta, options);% Save the learned features and the preprocessing matrices for use in % the later exercise on convolution and pooling fprintf('Saving learned features and preprocessing matrices...\n'); save('STL10Features.mat', 'optTheta', 'ZCAWhite', 'meanPatch'); fprintf('Saved\n');%% STEP 2d: Visualize learned featuresW = reshape(optTheta(1:visibleSize * hiddenSize), hiddenSize, visibleSize); b = optTheta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize); figure; %這里為什么要用(W*ZCAWhite)'呢?首先,使用W*ZCAWhite是因為每個樣本x輸入網絡, %其輸出等價于W*ZCAWhite*x;另外,由于W*ZCAWhite的每一行才是一個隱含節點的變換值 %而displayColorNetwork函數是把每一列顯示一個小圖像塊的,所以需要對其轉置。displayColorNetwork( (W*ZCAWhite)');sparseAutoencoderLinearCost.m
function [cost,grad,features] = sparseAutoencoderLinearCost(theta, visibleSize, hiddenSize, ...lambda, sparsityParam, beta, data) % -------------------- YOUR CODE HERE -------------------- % Instructions: % Copy sparseAutoencoderCost in sparseAutoencoderCost.m from your % earlier exercise onto this file, renaming the function to % sparseAutoencoderLinearCost, and changing the autoencoder to use a % linear decoder. % -------------------- YOUR CODE HERE -------------------- %計算線性解碼器代價函數及其梯度 % visibleSize:輸入層神經單元節點數 % hiddenSize:隱藏層神經單元節點數 % lambda: 權重衰減系數 % sparsityParam: 稀疏性參數 % beta: 稀疏懲罰項的權重 % data: 訓練集 % theta:參數向量,包含W1、W2、b1、b2W1 = 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);% Loss and gradient variables (your code needs to compute these values) m = size(data, 2); % 樣本數量%% ---------- YOUR CODE HERE -------------------------------------- % Instructions: Compute the loss for the Sparse Autoencoder and gradients % W1grad, W2grad, b1grad, b2grad % % Hint: 1) data(:,i) is the i-th example % 2) your computation of loss and gradients should match the size % above for loss, W1grad, W2grad, b1grad, b2grad% z2 = W1 * x + b1 % a2 = f(z2) % z3 = W2 * a2 + b2 % h_Wb = a3 = f(z3)z2 = W1 * data + repmat(b1, [1, m]); a2 = sigmoid(z2); z3 = W2 * a2 + repmat(b2, [1, m]); a3 = z3;rhohats = mean(a2,2); rho = sparsityParam; KLsum = sum(rho * log(rho ./ rhohats) + (1-rho) * log((1-rho) ./ (1-rhohats)));squares = (a3 - data).^2; squared_err_J = (1/2) * (1/m) * sum(squares(:)); %均方差項 weight_decay_J = (lambda/2) * (sum(W1(:).^2) + sum(W2(:).^2));%權重衰減項 sparsity_J = beta * KLsum; %懲罰項cost = squared_err_J + weight_decay_J + sparsity_J;%損失函數值% delta3 = -(data - a3) .* fprime(z3); % but fprime(z3) = a3 * (1-a3) delta3 = -(data - a3); beta_term = beta * (- rho ./ rhohats + (1-rho) ./ (1-rhohats)); delta2 = ((W2' * delta3) + repmat(beta_term, [1,m]) ) .* a2 .* (1-a2);W2grad = (1/m) * delta3 * a2' + lambda * W2; % W2梯度 b2grad = (1/m) * sum(delta3, 2); % b2梯度 W1grad = (1/m) * delta2 * data' + lambda * W1; % W1梯度 b1grad = (1/m) * sum(delta2, 2); % b1梯度%------------------------------------------------------------------- % Convert weights and bias gradients to a compressed form % This step will concatenate and flatten all your gradients to a vector % which can be used in the optimization method. grad = [W1grad(:) ; W2grad(:) ; b1grad(:) ; b2grad(:)];%------------------------------------------------------------------- % We are giving you the sigmoid function, you may find this function % useful in your computation of the loss and the gradients. function sigm = sigmoid(x)sigm = 1 ./ (1 + exp(-x)); endenddisplayColorNetwork.m
function displayColorNetwork(A)% display receptive field(s) or basis vector(s) for image patches % % A the basis, with patches as column vectors% In case the midpoint is not set at 0, we shift it dynamically if min(A(:)) >= 0 A = A - mean(A(:));%0均值化 endcols = round(sqrt(size(A, 2)));% 每行大圖像中小圖像塊的個數channel_size = size(A,1) / 3; dim = sqrt(channel_size);% 小圖像塊內每行或列像素點個數 dimp = dim+1; rows = ceil(size(A,2)/cols);% 每列大圖像中小圖像塊的個數 B = A(1:channel_size,:);% R通道像素值 C = A(channel_size+1:channel_size*2,:);% G通道像素值 D = A(2*channel_size+1:channel_size*3,:);% B通道像素值 B=B./(ones(size(B,1),1)*max(abs(B)));% 歸一化 C=C./(ones(size(C,1),1)*max(abs(C))); D=D./(ones(size(D,1),1)*max(abs(D))); % Initialization of the image I = ones(dim*rows+rows-1,dim*cols+cols-1,3);%Transfer features to this image matrix for i=0:rows-1for j=0:cols-1if i*cols+j+1 > size(B, 2)breakend% This sets the patchI(i*dimp+1:i*dimp+dim,j*dimp+1:j*dimp+dim,1) = ...reshape(B(:,i*cols+j+1),[dim dim]);I(i*dimp+1:i*dimp+dim,j*dimp+1:j*dimp+dim,2) = ...reshape(C(:,i*cols+j+1),[dim dim]);I(i*dimp+1:i*dimp+dim,j*dimp+1:j*dimp+dim,3) = ...reshape(D(:,i*cols+j+1),[dim dim]);end endI = I + 1;% 使I的范圍從[-1,1]變為[0,2] I = I / 2;% 使I的范圍從[0,2]變為[0, 1] imagesc(I); axis equal% 等比坐標軸:設置屏幕高寬比,使得每個坐標軸的具有均勻的刻度間隔 axis off% 關閉所有的坐標軸標簽、刻度、背景end參考文獻
Exercise:Learning color features with Sparse Autoencoders
Deep learning:十七(Linear Decoders,Convolution和Pooling)
線性解碼器
吳恩達 Andrew Ng 的公開課
總結
以上是生活随笔為你收集整理的UFLDL教程: Exercise:Learning color features with Sparse Autoencoders的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 花旗银行信用卡还款日可以改吗?还款日是每
- 下一篇: 农行燃梦信用卡免息期有多长?一招教你享受