UFLDL教程之(三)PCA and Whitening exercise

系统管理员 2021-07-16 17:30 407阅读 0赞

Exercise:PCA and Whitening

第0步:数据准备

UFLDL下载的文件中,包含数据集IMAGES_RAW,它是一个512*512*10的矩阵,也就是10幅512*512的图像

(a)载入数据

利用sampleIMAGESRAW函数,从IMAGES_RAW中提取numPatches个图像块儿,每个图像块儿大小为patchSize,并将提取到的图像块儿按列存放,分别存放在在矩阵patches的每一列中,即patches(:,i)存放的是第i个图像块儿的所有像素值

(b)数据去均值化处理

将每一个图像块儿的所有像素值都减去该图像块儿的平均像素值,实现数据的去均值化

下图是显示的随机选取的图像块儿

271503287651852.jpg

第一步:执行PCA

该部分分为两部分

(1)进行PCA计算,这里仅仅对数据x进行旋转得到xrot,而不进行主成分的提取

具体地:

①计算数据x的协方差矩阵sigma

  1. ![271519180789570.png][]

②对sigma进行特征分解,利用matlab的eig函数,从而得到sigma的特征向量构成的矩阵U

[U,S,V]=eig(sigma);

U=[u1,…,ui,…,un],它的每一列分别是sigma的特征向量,n是输入数据的特征维数

S=diag([λ1,…λi,…,λn])是由sigma的特征值作为对角元素的对角阵,ui和λi相对应;

为了后续的计算,这里要将U的各列次序进行调换,使得调换后的各列所对应的特征值大小依次递减;

调换后的矩阵仍记作U,相应的特征值对角阵仍即为S,即:

U=[u1,…,ui,…,un],S=diag([λ1,…λi,…,λn]),满足:λ1>=…>=λi>=…>=λn

③利用矩阵U对数据x进行旋转,得到xrot,即xrot=U‘*x

(2)对旋转后的数据求解协方差矩阵covar,并将其可视化,观察得到的选择后的数据是否正确

PCA保证选择后的数据的协方差矩阵是一个对角阵,如果covar是正确的

那么它的图像应该是一个蓝色背景,并且在对角线位置有一斜线

这里显示协方差矩阵covar利用了matlab的imagesc,该函数真的很强大呀

imagesc(covar)的作用是:把矩阵covar以图像形式显示出来,矩阵中不同的数值会被赋予不同的颜色

得到的协方差矩阵的图像如下:可以看到,图像处了对角线位置外,其余部分颜色都相同

271537486569025.jpg  

第二步:满足条件的主成分个数

本部分,找到满足条件的主成分的个数k

也就是找到最小的k值,使得(λ1+…+ λk)/(λ1+…+ λn)>某个百分数,如99%

第三步:利用找到的主成分个数,对数据进行降维

在第二步,已经找到了数字k,也就是,保留数据的k个主成分就满足了要求

在该步,将对数据x进行降维,只留下k个主成分,得到xTidle

同时,为了观察降维后的数据的好坏,在利用U(:,k)将降维后的数据变换会原来的维数,也就是得到了原数据的近似恢复数据

并利用网格将恢复出的图像显示出,与原图像进行比较,下面第一幅图是由降维后的图像恢复出的原数据,下图是相应的原数据,可以发现,降维后的数据基本可以恢复出于原数据非常相近的数据

271552062971408.jpg271552158445057.jpg

第四步:PCA白化+正则化

该部分分为两步

(1)执行具有白化和正则化的PCA

首先,对数据进行旋转(利用特征矩阵U)

然后,利用特征值对旋转后的数据进行缩放,实现白化

同时,在利用特征值缩放时,利用参数ε对特征值进行微调,实现正则化

271631124694609.png

(b)计算百化后的数据的协方差矩阵,观察该协方差矩阵

如果加入了正则化项,则该协方差矩阵的对角线元素都小于1

如果没有加入正则项(即仅有旋转+白化),则该协方差矩阵的对角线元素都为1(实际上,是令ε为一个极小的数)

下图是白化后数据的协方差矩阵对应的图像,上图是加入正则化后的结果,下图是没有加入正则化后的结果

271656099842208.jpg

271657432976249.jpg

第五步:ZCA白化

ZCA白化,就是在PCA白化的基础上做了一个旋转,即

271923492655269.png

下面的第一幅图是ZCA白化后的结果图,第二幅图是相应的原始图像

可以看到,ZCA白化的结果图似乎是原始图像的边缘

271926024063210.jpg271926112978960.jpg

下面,是该部分的pca_gen的代码

  1. clc
  2. clear
  3. close all
  4. %%================================================================
  5. %% Step 0a: Load data
  6. % Here we provide the code to load natural image data into x.
  7. % x will be a 144 * 10000 matrix, where the kth column x(:, k) corresponds to
  8. % the raw image data from the kth 12x12 image patch sampled.
  9. % You do not need to change the code below.
  10. x = sampleIMAGESRAW();%从IMAGES_RAW中读取一些图像patches
  11. figure('name','Raw images');%显示一个figure,标题为raw images
  12. randsel = randi(size(x,2),200,1); % A random selection of samples for visualization
  13. display_network(x(:,randsel));%显示随机选取的图像块儿
  14. %% Step 0b: Zero-mean the data (by row)
  15. % You can make use of the mean and repmat/bsxfun functions.
  16. % -------------------- YOUR CODE HERE --------------------
  17. x=x-repmat(mean(x),size(x,1),1);%x的每一列的所有元素都减去该列的均值
  18. %%================================================================
  19. %% Step 1a: Implement PCA to obtain xRot
  20. % Implement PCA to obtain xRot, the matrix in which the data is expressed
  21. % with respect to the eigenbasis of sigma, which is the matrix U.
  22. % -------------------- YOUR CODE HERE --------------------
  23. xRot = zeros(size(x)); % You need to compute this
  24. % 计算协方差矩阵并进行特征值分解
  25. m=size(x,2);% 输入的样本个数
  26. sigma=x*x'/m;%输入数据的协方差矩阵
  27. [U,S,V]=eig(sigma);%对协方差矩阵进行特征值分解
  28. [S_Value,S_Index]=sort(diag(S),'descend');%提取S的对角线元素,将其按降序排列,sIndex是排序后的编号
  29. U=U(:,S_Index);
  30. S=diag(S_Value);
  31. % 对数据进行旋转
  32. xRot=U'*x;
  33. %% Step 1b: Check your implementation of PCA
  34. % The covariance matrix for the data expressed with respect to the basis U
  35. % should be a diagonal matrix with non-zero entries only along the main
  36. % diagonal. We will verify this here.
  37. % Write code to compute the covariance matrix, covar.
  38. % When visualised as an image, you should see a straight line across the
  39. % diagonal (non-zero entries) against a blue background (zero entries).
  40. % -------------------- YOUR CODE HERE --------------------
  41. covar = zeros(size(x, 1)); % You need to compute this
  42. covar=xRot*xRot'/m;%旋转数据后的数据对应的协方差矩阵
  43. % Visualise the covariance matrix. You should see a line across the
  44. % diagonal against a blue background.
  45. figure('name','Visualisation of covariance matrix');
  46. imagesc(covar);
  47. %%================================================================
  48. %% Step 2: Find k, the number of components to retain
  49. % Write code to determine k, the number of components to retain in order
  50. % to retain at least 99% of the variance.
  51. % -------------------- YOUR CODE HERE --------------------
  52. k = 0; % Set k accordingly
  53. S_diag=diag(S);
  54. S_sum=sum(S_diag);
  55. for k=1:size(x,1)
  56. Sk_sum=sum(S_diag(1:k));
  57. if Sk_sum/S_sum>=0.99
  58. break;
  59. end
  60. end
  61. %%================================================================
  62. %% Step 3: Implement PCA with dimension reduction
  63. % Now that you have found k, you can reduce the dimension of the data by
  64. % discarding the remaining dimensions. In this way, you can represent the
  65. % data in k dimensions instead of the original 144, which will save you
  66. % computational time when running learning algorithms on the reduced
  67. % representation.
  68. %
  69. % Following the dimension reduction, invert the PCA transformation to produce
  70. % the matrix xHat, the dimension-reduced data with respect to the original basis.
  71. % Visualise the data and compare it to the raw data. You will observe that
  72. % there is little loss due to throwing away the principal components that
  73. % correspond to dimensions with low variation.
  74. % -------------------- YOUR CODE HERE --------------------
  75. % 对数据进行降维
  76. xTidle=U(:,1:k)'*x;
  77. % 利用降维后的数据xTidle对数据进行恢复
  78. xHat = zeros(size(x)); % You need to compute this
  79. xHat = U*[xTidle;zeros(m-k,size(x,2))];
  80. % Visualise the data, and compare it to the raw data
  81. % You should observe that the raw and processed data are of comparable quality.
  82. % For comparison, you may wish to generate a PCA reduced image which
  83. % retains only 90% of the variance.
  84. figure('name',['PCA processed images ',sprintf('(%d / %d dimensions)', k, size(x, 1)),'']);
  85. display_network(xHat(:,randsel));
  86. figure('name','Raw images');
  87. display_network(x(:,randsel));
  88. %%================================================================
  89. %% Step 4a: Implement PCA with whitening and regularisation
  90. % Implement PCA with whitening and regularisation to produce the matrix
  91. % xPCAWhite.
  92. epsilon =0.000001;
  93. % -------------------- YOUR CODE HERE --------------------
  94. xPCAWhite = zeros(size(x));
  95. xPCAWhite=diag(1./sqrt(S_diag+epsilon))*xRot;
  96. %% Step 4b: Check your implementation of PCA whitening
  97. % Check your implementation of PCA whitening with and without regularisation.
  98. % PCA whitening without regularisation results a covariance matrix
  99. % that is equal to the identity matrix. PCA whitening with regularisation
  100. % results in a covariance matrix with diagonal entries starting close to
  101. % 1 and gradually becoming smaller. We will verify these properties here.
  102. % Write code to compute the covariance matrix, covar.
  103. %
  104. % Without regularisation (set epsilon to 0 or close to 0),
  105. % when visualised as an image, you should see a red line across the
  106. % diagonal (one entries) against a blue background (zero entries).
  107. % With regularisation, you should see a red line that slowly turns
  108. % blue across the diagonal, corresponding to the one entries slowly
  109. % becoming smaller.
  110. % -------------------- YOUR CODE HERE --------------------
  111. covar=xPCAWhite*xPCAWhite'/m;
  112. % Visualise the covariance matrix. You should see a red line across the
  113. % diagonal against a blue background.
  114. figure('name','Visualisation of covariance matrix');
  115. imagesc(covar);
  116. %%================================================================
  117. %% Step 5: Implement ZCA whitening
  118. % Now implement ZCA whitening to produce the matrix xZCAWhite.
  119. % Visualise the data and compare it to the raw data. You should observe
  120. % that whitening results in, among other things, enhanced edges.
  121. xZCAWhite = zeros(size(x));
  122. % -------------------- YOUR CODE HERE --------------------
  123. xZCAWhite=U*xPCAWhite;%ZCA白化即在PCA白化基础上做了一个旋转
  124. % Visualise the data, and compare it to the raw data.
  125. % You should observe that the whitened images have enhanced edges.
  126. figure('name','ZCA whitened images');
  127. display_network(xZCAWhite(:,randsel));
  128. figure('name','Raw images');
  129. display_network(x(:,randsel));

发表评论

表情:
评论列表 (有 0 条评论,407人围观)

还没有评论,来说两句吧...

相关阅读