- minor improvements

git-svn-id: http://moon:8086/svn/matlab/trunk@95 801c6759-fa7c-4059-a304-17956f83a07c
This commit is contained in:
2016-07-12 20:34:57 +00:00
parent a331d35237
commit 32063406bd
4 changed files with 24 additions and 15 deletions
+1
View File
@@ -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);
+1 -1
View File
@@ -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));
+8 -7
View File
@@ -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;
+14 -7
View File
@@ -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}');