From 32063406bdf7bb8bc2e5bcb7b1429f23fe518fd3 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Tue, 12 Jul 2016 20:34:57 +0000 Subject: [PATCH] [PCA] - minor improvements git-svn-id: http://moon:8086/svn/matlab/trunk@95 801c6759-fa7c-4059-a304-17956f83a07c --- RBM/UFLDL/common/loadMNISTImages.m | 1 + RBM/mnist_gen_pca.m | 2 +- RBM/pca.m | 15 ++++++++------- RBM/pca_eval.asv | 21 ++++++++++++++------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/RBM/UFLDL/common/loadMNISTImages.m b/RBM/UFLDL/common/loadMNISTImages.m index 6eb2304..1b1ac62 100644 --- a/RBM/UFLDL/common/loadMNISTImages.m +++ b/RBM/UFLDL/common/loadMNISTImages.m @@ -14,6 +14,7 @@ numCols = fread(fp, 1, 'int32', 0, 'ieee-be'); images = fread(fp, inf, 'unsigned char'); images = reshape(images, numCols, numRows, numImages); +%images = images(:,:, 1:200); images = permute(images,[2 1 3]); fclose(fp); diff --git a/RBM/mnist_gen_pca.m b/RBM/mnist_gen_pca.m index 0964713..b54933d 100644 --- a/RBM/mnist_gen_pca.m +++ b/RBM/mnist_gen_pca.m @@ -17,7 +17,7 @@ avg = mean(x, 1); % Compute the mean pixel intensity value separately for ea x = x - repmat(avg, size(x, 1), 1); fprintf('Do the PCA whitening\n'); -[xHat, k, xZCAWhite] = pca(x, retained_variance_target); +[xHat, k, xZCAWhite, xPCAWhite] = pca(x, retained_variance_target); figure('name',['PCA processed images ',sprintf('(%d / %d dimensions)', k, size(x, 1)),'']); display_network(xHat(:,randsel)); diff --git a/RBM/pca.m b/RBM/pca.m index b29ea2b..23ec303 100644 --- a/RBM/pca.m +++ b/RBM/pca.m @@ -5,8 +5,9 @@ % the raw image data from the kth 12x12 image patch sampled. % You do not need to change the code below. -function [xHat, k, xZCAWhite] = pca(x, retained_variance_target) - +function [xHat, k, xZCAWhite, xPCAWhite] = pca(x, retained_variance_target) +epsilon = 1e-1; + %%================================================================ %% Step 1a: Implement PCA to obtain xRot % Implement PCA to obtain xRot, the matrix in which the data is expressed @@ -19,8 +20,8 @@ fprintf('Perform Singular Value Decomposition\n'); [U,S,V] = svd(sigma); fprintf('Perform PCA transformation\n'); -xrot = U' * x; - +wpca = U' * diag(sqrt(1./(diag(S) + epsilon))); +wzca = U * wpca; %%================================================================ %% Step 2: Find k, the number of components to retain % Write code to determine k, the number of components to retain in order @@ -51,6 +52,7 @@ k = k % correspond to dimensions with low variation. fprintf('Calculate xHat\n'); +xrot = U' * x; xHat = U(:, 1:k) * xrot(1:k, :); %%================================================================ @@ -58,10 +60,9 @@ xHat = U(:, 1:k) * xrot(1:k, :); % Implement PCA with whitening and regularisation to produce the matrix % xPCAWhite. -epsilon = 1e-1; %%% YOUR CODE HERE %%% fprintf('Calculate xPCAWhite\n'); -xPCAWhite = diag(sqrt(1./(diag(S) + epsilon))) * xrot; +xPCAWhite = wpca * x; %%================================================================ %% Step 5: Implement ZCA whitening @@ -71,5 +72,5 @@ xPCAWhite = diag(sqrt(1./(diag(S) + epsilon))) * xrot; %%% YOUR CODE HERE %%% fprintf('Calculate xZCAWhite\n'); -xZCAWhite = U * xPCAWhite; +xZCAWhite = wzca * x; diff --git a/RBM/pca_eval.asv b/RBM/pca_eval.asv index 0112439..49191ec 100644 --- a/RBM/pca_eval.asv +++ b/RBM/pca_eval.asv @@ -7,8 +7,8 @@ Ndim = 2; K = 1; epsilon = 0.1; -x = 2*(rand(Ndim, Ntrain)-0.5) -x = 0.3*x + repmat(linspace(-0.7,0.7, Ntrain), Ndim, 1) +x = 2*(rand(Ndim, Ntrain)-0.5); +x = 0.3*x + repmat(linspace(-0.7,0.7, Ntrain), Ndim, 1); plot(x(1,:), x(2,:), '+'); axis([-1 1 -1 1]); grid; title('Raw data'); xlabel('x_1'); ylabel('x_2'); sigma = x * x' / size(x, 2) @@ -17,20 +17,27 @@ xrot = U' * x; figure; plot(xrot(1,:), xrot(2,:), '+'); axis([-1 1 -1 1]); grid; title('Rotated data'); xlabel('x_{rot,1}'); ylabel('x_{rot,2}'); -lambda = diag(S); -retained_variance = sum(lambda(1:K))./sum(lambda) - -xpca = diag(sqrt(1./(lambda + epsilon))) * xrot; +xpca = pcdiag(sqrt(1./(diag(S) + epsilon))) * xrot; figure; plot(xpca(1,:), xpca(2,:), '+'); axis([-1 1 -1 1]); grid; title('Whitened data dim=2'); xlabel('x_{pca,1}'); ylabel('x_{pca,2}'); + xtilde = zeros(size(xrot)); xtilde(1:K, :) = xrot(1:K, :); +lambda = diag(S); +retained_variance = sum(lambda(1:K))./sum(lambda) + figure; plot(xtilde(1,:), xtilde(2,:), '+'); axis([-1 1 -1 1]); grid; title('Rotated data reduced to dim = 1'); xlabel('x_{tilde,1}'); ylabel('x_{tilde,2}'); -xhat = U*xtilde; +size (U) +size(xrot) +xhat = U(:, 1:K) * xrot(1:K, :); figure; plot(xhat(1,:), xhat(2,:), '+'); axis([-1 1 -1 1]); grid; title('Data reduced to dim = 1'); xlabel('x_{hat,1}'); ylabel('x_{hat,2}'); + +xpca1 = diag(sqrt(1./(diag(S)))) * xhat; +figure; +plot(xpca1(1,:), xpca1(2,:), '+'); axis([-1 1 -1 1]); grid; title('Whitened data dim=1'); xlabel('x_{pca,1}'); ylabel('x_{pca,2}');