-
Notifications
You must be signed in to change notification settings - Fork 7
/
runExample.m
66 lines (50 loc) · 1.5 KB
/
runExample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%% Startup (addpaths)
startup
%% Clear
clear ; close all ; clc ;
%% Load Data
% You can obtain patches.mat from
% http://cs.stanford.edu/~jngiam/data/patches.mat
fprintf('Loading Data\n');
% Loads a variable data (size 256x50000)
load patches.mat
% Reduce dataset size for faster training
data = data(:, 1:20000);
%% PCA Whitening
fprintf('\nPCA Whitening\n');
% Remove DC
data = bsxfun(@minus, data, mean(data, 1));
% Remove the "mean" patch
data = bsxfun(@minus, data, mean(data, 2));
% Compute Covariance Matrix and Eigenstuff
cov = data * data' / size(data, 2);
[E,D] = eig(cov);
d = diag(D);
% Sort eigenvalues in descending order
[dsort, idx] = sort(d, 'descend');
% PCA Whitening (and pick top 99% of eigenvalues)
dsum = cumsum(dsort);
dcutoff = find(dsum > 0.99 * dsum(end), 1);
E = E(:, idx(1:dcutoff));
d = d(idx(1:dcutoff));
V = diag(1./sqrt(d+1e-6)) * E';
%% Whiten the data
whiteData = V * data;
%% Run the optimization with minFunc (ICA)
fprintf('\nTraining ICA (w/ Score Matching)\n\n');
nHidden = 400; nInput = size(whiteData, 1);
W = randn(nHidden, nInput);
options.Method = 'lbfgs';
options.maxIter = 100; % Maximum number of iterations of L-BFGS to run
options.display = 'on';
tic
[optW, cost] = minFunc( @icaScoreMatching, ...
W(:), options, whiteData, ...
nHidden, nInput);
toc
%% Display Results
optW = reshape(optW, nHidden, nInput);
displayData(optW * V);
fprintf('ICA Training Completed.\n');
fprintf('Press Enter to Continue.\n\n');
pause