【DeepLearning】Exercise:PCA and Whitening
Exercise:PCA and Whitening
习题链接:Exercise:PCA and Whitening
pca_gen.m
%%================================================================
%% Step 0a: Load data
% Here we provide the code to load natural image data into x.
% x will be a * 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,),,); % 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 --------------------
x = x-repmat(mean(x,),size(x,),); %%================================================================
%% 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
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, )); % You need to compute this
covar = xRot*xRot' ./ size(x,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 : 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 % of the variance. % -------------------- YOUR CODE HERE --------------------
%k = ; % Set k accordingly
eigenvalue = diag(covar);
total = sum(eigenvalue);
tmpSum = ;
for k=:size(x,)
tmpSum = tmpSum+eigenvalue(k);
if(tmpSum / total >= 0.9)
break;
end
end
%%================================================================
%% Step : 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 , 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(k+:size(x,), :) = ;
xHat = u * 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 % of the variance. figure('name',['PCA processed images ',sprintf('(%d / %d dimensions)', k, size(x, )),'']);
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 = ;
epsilon = 0.1;
%xPCAWhite = zeros(size(x)); % -------------------- YOUR CODE HERE --------------------
xPCAWhite = diag( ./ sqrt(diag(s)+epsilon)) * u' * x; %%================================================================
%% 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
% and gradually becoming smaller. We will verify these properties here.
% Write code to compute the covariance matrix, covar.
%
% Without regularisation (set epsilon to or close to ),
% 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(x,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 : 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. %xZCAWhite = zeros(size(x));
xZCAWhite = u * xPCAWhite; % -------------------- 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));
【DeepLearning】Exercise:PCA and Whitening的更多相关文章
- 【DeepLearning】Exercise:PCA in 2D
Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%=================================== ...
- 【DeepLearning】Exercise:Convolution and Pooling
Exercise:Convolution and Pooling 习题链接:Exercise:Convolution and Pooling cnnExercise.m %% CS294A/CS294 ...
- 【DeepLearning】Exercise:Sparse Autoencoder
Exercise:Sparse Autoencoder 习题的链接:Exercise:Sparse Autoencoder 注意点: 1.训练样本像素值需要归一化. 因为输出层的激活函数是logist ...
- 【DeepLearning】Exercise:Softmax Regression
Exercise:Softmax Regression 习题的链接:Exercise:Softmax Regression softmaxCost.m function [cost, grad] = ...
- 【DeepLearning】Exercise:Learning color features with Sparse Autoencoders
Exercise:Learning color features with Sparse Autoencoders 习题链接:Exercise:Learning color features with ...
- 【DeepLearning】Exercise: Implement deep networks for digit classification
Exercise: Implement deep networks for digit classification 习题链接:Exercise: Implement deep networks fo ...
- 【DeepLearning】Exercise:Self-Taught Learning
Exercise:Self-Taught Learning 习题链接:Exercise:Self-Taught Learning feedForwardAutoencoder.m function [ ...
- 【DeepLearning】Exercise:Vectorization
Exercise:Vectorization 习题的链接:Exercise:Vectorization 注意点: MNIST图片的像素点已经经过归一化. 如果再使用Exercise:Sparse Au ...
- 【UFLDL】Exercise: Convolutional Neural Network
这个exercise需要完成cnn中的forward pass,cost,error和gradient的计算.需要弄清楚每一层的以上四个步骤的原理,并且要充分利用matlab的矩阵运算.大概把过程总结 ...
随机推荐
- thrift系列 - 快速入门
1.简介 Thrift是当前流行的RPC框架之一,它有强大的代码生成引擎,可以跨语言,轻松解决程序间的通信问题. 本文旨在帮助大家快速入门,若想深入原理,请参见thrift官网:h ...
- NSLayoutConstraint的简单应用
UIView *topView = [[UIView alloc] init]; topView.backgroundColor = [UIColor redColor]; [self.view ad ...
- vi入门到精通
VI是在Linux命令行下常用的文本编辑工具,在服务配置管理过程中经常用到:vi的常见的使用指南,互联网上随处可见,但仅能满足初学者对文档编辑的需求.这里就我自己在使用过程中通常用到的一些技巧操作方法 ...
- 一家VC支持企业的发展轨迹——了解每次融资后股权和期权的变化,以及股份是如何被稀释的【转载】
来源:ReachVc 该文是 ReachVC 上一篇文章,是某个投资公司副总裁的独立博客,文章写得不错.如果你是一个不太了解融资的创业者,那么本文将对你很有用.通过武林外传同福客栈的例子,了解每次融资 ...
- oracle 对表赋权限
grant select,insert,delete,update on yizhen123.tpp_t_dz_yinglian to wangyd;
- Hadoop2.7.4 在 Windows 10(64位) 详细配置
网上查询资料,实际配置后整理记录. 一.下载安装配置 1.8.0 以上版本 java 环境 完成后,使用 java -version 命令查看是否成功. 二.下载 hadoop-2.7.4.tar.g ...
- vue循环中的v-show
v-show如果使用循环对象的属性来时控制, 这个属性必须是加载时就存在的 <div class="list-group col-sm-12" v-for="(is ...
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
近期更新了一下HttpClientUtil工具类代码,主要是加入了一个參数HttpContext,这个是用来干嘛的呢?事实上是用来保存和传递Cookie所须要的. 由于我们有非常多时候都须要登录.然后 ...
- SpringMVC对日期类型的转换@ResponseBody返回的DateTime是long类型
目前,多数web开发这都在使用Spring的框架.但是这个框架有个 @ResponseBody 注解返回json时,日期格式默认显示为时间戳. 而我们页面展示的时候一般都是以下格式: yyyy-MM- ...
- 为准确生成执行计划更新统计信息-analyze与dbms_stats
如果我们想让CBO利用合理利用数据的统计信息,正确判断执行任何SQL查询时的最快途径,需要及时的使用analyze命令或者dbms_stats重新统计数据的统计信息. 例如索引跳跃式扫描(INDEX ...