【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的矩阵运算.大概把过程总结 ...
随机推荐
- Laravel 5 中使用 JWT(Json Web Token) 实现基于API的用户认证
在JavaScript前端技术大行其道的今天,我们通常只需在后台构建API提供给前端调用,并且后端仅仅设计为给前端移动App调用.用户认证是Web应用的重要组成部分,基于API的用户认证有两个最佳解决 ...
- 【Spark】SparkStreaming-流处理-规则动态更新-解决方案
SparkStreaming-流处理-规则动态更新-解决方案 image2017-10-27_11-10-53.png (1067×738) elasticsearch-head Elasticsea ...
- java 从网络Url中下载文件 (转)
http://blog.csdn.net/xb12369/article/details/40543649/ /** * 从网络Url中下载文件 * @param urlStr ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十六)Structured Streaming:WARN clients.NetworkClient: Error while fetching metadata with correlation id 1 : {my-topic=LEADER_NOT_AVAILABLE}
问题描述: 我之前使用kafka的命令删除了改topic: ./kafka-topics.sh --delete --zookeeper [zookeeper server] --topic [to ...
- apache 错误:The system cannot find the file specified.
在启动apache时出现了以下错误信息 Window日志里也记录了此错误信息 而出现此错误的原因是IIS占用了80端口 停止IIS再重新启动apache即可解决 参考: cannot find ...
- (转)HLSL,函数列表
中文列表 函数名 说明 abs 计算输入值的绝对值. acos 返回输入值反余弦值. all 测试非0值. any 测试输入值中的任何非零值. asin 返回输入值的反正弦值. atan 返回输入值的 ...
- retrofit+RXjava二次封装
接入说明:项目中已集成RXjava,RXandroid.Retrofit,为避免包冲突,不须要再次接入. 就可以直接使用RXjava,Retrofit的所有api. github地址:https:// ...
- ASP入门(二十一)- 如何自己获取 ADO 连接字符串
1.新建一个文本文件,并将文件名修改为[ado.udl] 注意 如果不显示扩展名,请在资源管理器的[查看 | 选项]对话框中去掉"隐藏已知文件类型的扩展名"勾就可以了. 2.双击这 ...
- 【Nodejs】使用http.request批量下载MP3,发现网络文件大于1000K时下载文件为0K
这又一次让我对http.request产生质疑 //====================================================== // 喜爱123四年级上英语MP3下载 ...
- WIFI:802.11协议帧格式
802协议桢格式 802.11和Wi-Fi技术并不是同一个东西.Wi-Fi标准是802.11标准的一个子集,并且是Wi-Fi联盟负责管理 802协议桢格式: 协议 发布年份/日期 Op.标准频宽 实际 ...