Deep Learning by Andrew Ng --- PCA and whitening

这是UFLDL的编程练习。具体教程参照官网。

PCA

PCA will find the priciple direction and the secodary direction in 2-dimention examples.

then

x~(i)=x(i)rot,1=uT1x(i)∈R.

is big when

x(i)rot,2=uT2x(i)

was small. so PCA drop

x(i)rot,2=uT2x(i)

approximate them with 0’s.

Whitening

白化是一种重要的预处理过程,其目的就是降低输入数据的冗余性,使得经过白化处理的输入数据具有如下性质:(i)特征之间相关性较低;(ii)所有特征具有相同的方差。

白化处理分PCA白化和ZCA白化,PCA白化保证数据各维度的方差为1,而ZCA白化保证数据各维度的方差相同。PCA白化可以用于降维也可以去相关性,而ZCA白化主要用于去相关性,且尽量使白化后的数据接近原始输入数据。

PCA白化ZCA白化都降低了特征之间相关性较低,同时使得所有特征具有相同的方差。

1. PCA白化需要保证数据各维度的方差为1,ZCA白化只需保证方差相等。

2. PCA白化可进行降维也可以去相关性,而ZCA白化主要用于去相关性另外。

3. ZCA白化相比于PCA白化使得处理后的数据更加的接近原始数据。

Regularizaion

When implementing PCA whitening or ZCA whitening in practice, sometimes some of the eigenvalues \textstyle \lambda_i will be numerically close to 0, and thus the scaling step where we divide by \sqrt{\lambda_i} would involve dividing by a value close to zero; this may cause the data to blow up (take on large values) or otherwise be numerically unstable. In practice, we therefore implement this scaling step using a small amount of regularization, and add a small constant \textstyle \epsilon to the eigenvalues before taking their square root and inverse:

xPCAwhite,i=xrot,iλi+??????√.

When x takes values around [-1,1], a value of epsilon approx 10^{-5} might be typical.

编程作业代码(建议作为参考,自己先独立完成):

%% Step 0a: Load data
%  Here we provide the code to load natural image data into x.
%  x will be a 144 * 10000 matrix, where the kth column x(:, k) corresponds to
%  the raw image data from the kth 12x12 image patch sampled.
%  You do not need to change the code below.

x = sampleIMAGESRAW();
figure(‘name‘,‘Raw images‘);
randsel = randi(size(x,2),200,1); % A random selection of samples for visualization
display_network(x(:,randsel));

%%================================================================
%% Step 0b: Zero-mean the data (by row)
%  You can make use of the mean and repmat/bsxfun functions.

% -------------------- YOUR CODE HERE --------------------
avg = mean(x,1);
x = x - repmat(avg,size(x,1),1);
%%================================================================
%% Step 1a: Implement PCA to obtain xRot
%  Implement PCA to obtain xRot, the matrix in which the data is expressed
%  with respect to the eigenbasis of sigma, which is the matrix U.

% -------------------- YOUR CODE HERE --------------------
xRot = zeros(size(x)); % You need to compute this
U = zeros(size(x,1));
sigma = x*x‘/size(x,2);
[U,S,V] = svd(sigma);
xRot = U‘*x;

%%================================================================
%% Step 1b: Check your implementation of PCA
%  The covariance matrix for the data expressed with respect to the basis U
%  should be a diagonal matrix with non-zero entries only along the main
%  diagonal. We will verify this here.
%  Write code to compute the covariance matrix, covar.
%  When visualised as an image, you should see a straight line across the
%  diagonal (non-zero entries) against a blue background (zero entries).

% -------------------- YOUR CODE HERE --------------------
covar = zeros(size(x, 1)); % You need to compute this
%[covar,S,V] = svd(sigma);
covar = xRot*xRot‘/size(xRot,2) ;
% Visualise the covariance matrix. You should see a line across the
% diagonal against a blue background.
figure(‘name‘,‘Visualisation of covariance matrix‘);
imagesc(covar);

%%================================================================
%% Step 2: Find k, the number of components to retain
%  Write code to determine k, the number of components to retain in order
%  to retain at least 99% of the variance.

% -------------------- YOUR CODE HERE --------------------
k = 0; % Set k accordingly
POVV = 0;
for k = 1:size(x,1)
  POVV = sum(sum(S(1:k,1:k)))/sum(sum(S));
    if POVV >=0.99
        break
    end
    k
end

%%================================================================
%% Step 3: Implement PCA with dimension reduction
%  Now that you have found k, you can reduce the dimension of the data by
%  discarding the remaining dimensions. In this way, you can represent the
%  data in k dimensions instead of the original 144, which will save you
%  computational time when running learning algorithms on the reduced
%  representation.
%
%  Following the dimension reduction, invert the PCA transformation to produce
%  the matrix xHat, the dimension-reduced data with respect to the original basis.
%  Visualise the data and compare it to the raw data. You will observe that
%  there is little loss due to throwing away the principal components that
%  correspond to dimensions with low variation.

% -------------------- YOUR CODE HERE --------------------
xHat = zeros(size(x));  % You need to compute this
xRot = U(:,1:k)‘*x;
xHat = U(:,1:k)*xRot;

% Visualise the data, and compare it to the raw data
% You should observe that the raw and processed data are of comparable quality.
% For comparison, you may wish to generate a PCA reduced image which
% retains only 90% of the variance.

figure(‘name‘,[‘PCA processed images ‘,sprintf(‘(%d / %d dimensions)‘, k, size(x, 1)),‘‘]);
display_network(xHat(:,randsel));
figure(‘name‘,‘Raw images‘);
display_network(x(:,randsel));

%%================================================================
%% Step 4a: Implement PCA with whitening and regularisation
%  Implement PCA with whitening and regularisation to produce the matrix
%  xPCAWhite. 

epsilon = 0.1;
xPCAWhite = zeros(size(x));
xPCAWhite = diag(1./sqrt(diag(S)+epsilon))*U‘*x;

% -------------------- YOUR CODE HERE -------------------- 

%%================================================================
%% Step 4b: Check your implementation of PCA whitening
%  Check your implementation of PCA whitening with and without regularisation.
%  PCA whitening without regularisation results a covariance matrix
%  that is equal to the identity matrix. PCA whitening with regularisation
%  results in a covariance matrix with diagonal entries starting close to
%  1 and gradually becoming smaller. We will verify these properties here.
%  Write code to compute the covariance matrix, covar.
%
%  Without regularisation (set epsilon to 0 or close to 0),
%  when visualised as an image, you should see a red line across the
%  diagonal (one entries) against a blue background (zero entries).
%  With regularisation, you should see a red line that slowly turns
%  blue across the diagonal, corresponding to the one entries slowly
%  becoming smaller.
% -------------------- YOUR CODE HERE --------------------
covar = xPCAWhite*xPCAWhite‘/size(xPCAWhite,2);
% Visualise the covariance matrix. You should see a red line across the
% diagonal against a blue background.
figure(‘name‘,‘Visualisation of covariance matrix‘);
imagesc(covar);

%%================================================================
%% Step 5: Implement ZCA whitening
%  Now implement ZCA whitening to produce the matrix xZCAWhite.
%  Visualise the data and compare it to the raw data. You should observe
%  that whitening results in, among other things, enhanced edges.
epsilon = 0.1;
xZCAWhite = zeros(size(x));
xZCAWhite = U*diag(1./sqrt(diag(S)+epsilon))*U‘*x;

% -------------------- YOUR CODE HERE -------------------- 

% Visualise the data, and compare it to the raw data.
% You should observe that the whitened images have enhanced edges.
figure(‘name‘,‘ZCA whitened images‘);
display_network(xZCAWhite(:,randsel));
figure(‘name‘,‘Raw images‘);
display_network(x(:,randsel));
时间: 2024-10-13 11:43:51

Deep Learning by Andrew Ng --- PCA and whitening的相关文章

Deep Learning by Andrew Ng --- DNN

When should we use fine-tuning? It is typically used only if you have a large labeled training set; in this setting, fine-tuning can significantly improve the performance of your classifier. However, if you have a large unlabeled dataset (for unsuper

Deep learning by Andrew Ng --- Linear Decoder

Sparse Autoencoder Recap: Because we used a sigmoid activation function for f(z(3)), we needed to constrain or scale the inputs to be in the range [0,1], since the sigmoid function outputs numbers in the range [0,1].(sparse autoencoder输入层mean 0的原因).

Deep Learning by Andrew Ng --- Softmax regression

这是UFLDL的编程练习. Weight decay(Softmax 回归有一个不寻常的特点:它有一个"冗余"的参数集)后的cost function和梯度函数: cost function: J(θ)=?1m??∑i=1m∑j=1k1{y(i)=j}logeθTjx(i)∑kl=1eθTlx(i)??+λ2∑i=1k∑j=0nθ2ij 梯度函数: ?θjJ(θ)=?1m∑i=1m[x(i)(1{y(i)=j}?p(y(i)=j|x(i);θ))]+λθj p(y(i)=j|x(i);

Deep Learning(1) —— Andrew Ng

Binary Classification 把图像展平成一个列向量x,x作为输入得到输出y,y是一个判断是猫或不是猫的概率. Notation used in this course 如果有m个训练样本,直观的做法可能是用for循环遍历所有的样本.但是在深度学习中应该像上图这样,把m个样本合成一个m列的向量(或矩阵),从而实现并行计算. Logistic Regression Sigmoid函数:\(\displaystyle \sigma(z) = \frac{1}{1+e^{-z}}\) p

(原创)Stanford Machine Learning (by Andrew NG) --- (week 7) Support Vector Machines

本栏目内容来源于Andrew NG老师讲解的SVM部分,包括SVM的优化目标.最大判定边界.核函数.SVM使用方法.多分类问题等,Machine learning课程地址为:https://www.coursera.org/course/ml 大家对于支持向量机(SVM)可能会比较熟悉,是个强大且流行的算法,有时能解决一些复杂的非线性问题.我之前用过它的工具包libsvm来做情感分析的研究,感觉效果还不错.NG在进行SVM的讲解时也同样建议我们使用此类的工具来运用SVM. (一)优化目标(Opt

(原创)Stanford Machine Learning (by Andrew NG) --- (week 10) Large Scale Machine Learning & Application Example

本栏目来源于Andrew NG老师讲解的Machine Learning课程,主要介绍大规模机器学习以及其应用.包括随机梯度下降法.维批量梯度下降法.梯度下降法的收敛.在线学习.map reduce以及应用实例:photo OCR.课程地址为:https://www.coursera.org/course/ml (一)大规模机器学习 从前面的课程我们知道,如果我们的系统是high variance的,那么增加样本数会改善我们的系统,假设现在我们有100万个训练样本,可想而知,如果使用梯度下降法,

Stanford CS229 Machine Learning by Andrew Ng

CS229 Machine Learning Stanford Course by Andrew Ng Course material, problem set Matlab code written by me, my notes about video course: https://github.com/Yao-Yao/CS229-Machine-Learning Contents: supervised learning Lecture 1 application field, pre-

神经网络作业: NN LEARNING Coursera Machine Learning(Andrew Ng) WEEK 5

在WEEK 5中,作业要求完成通过神经网络(NN)实现多分类的逻辑回归(MULTI-CLASS LOGISTIC REGRESSION)的监督学习(SUOERVISED LEARNING)来识别阿拉伯数字.作业主要目的是感受如何在NN中求代价函数(COST FUNCTION)和其假设函数中各个参量(THETA)的求导值(GRADIENT DERIVATIVE)(利用BACKPROPAGGATION). 难度不高,但问题是你要习惯使用MALAB的矩阵QAQ,作为一名蒟蒻,我已经狗带了.以下代核心部

Machine Learning Yearning - Andrew NG

链接(1~12章): https://gallery.mailchimp.com/dc3a7ef4d750c0abfc19202a3/files/Machine_Learning_Yearning_V0.5_01.pdf链接(第13章): https://gallery.mailchimp.com/dc3a7ef4d750c0abfc19202a3/files/Machine_Learning_Yearning_V0.5_02.pdf链接(第14章): https://gallery.mailc