-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmfEuc.m
85 lines (67 loc) · 1.9 KB
/
nmfEuc.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function [W, H] = nmfEuc( V, rdim, fname, showflag, epsilon, maxiter)
%
% Check that we have non-negative data
if min(V(:))<0, error('Negative values in data!'); end
% Globally rescale data to avoid potential overflow/underflow
V = V/max(V(:));
% Dimensions
vdim = size(V,1);
samples = size(V,2);
% Create initial matrices
W = abs(randn(vdim,rdim));
H = abs(randn(rdim,samples));
% Initialize displays
if showflag,
figure(1); clf; % this will show the energies and sparsenesses
figure(2); clf; % this will show the objective function
drawnow;
end
% Calculate initial objective
objhistory = sum(sum((V - W*H).^2));
timestarted = clock;
% Start iteration
iter = 0;
keepGoing = 1;
while keepGoing,
% Show progress
fprintf('[%d]: %.5f \n',iter,objhistory(end));
% Save every once in a while
if rem(iter,5)==0,
elapsed = etime(clock,timestarted);
fprintf('Saving...');
save(fname,'W','H','iter','objhistory','elapsed');
fprintf('Done!\n');
end
% Show stats
if showflag && (rem(iter,5)==0),
figure(1);
cursW = (sqrt(vdim)-(sum(W)./sqrt(sum(W.^2))))/(sqrt(vdim)-1);
cursH = (sqrt(samples)-(sum(H')./sqrt(sum(H'.^2))))/(sqrt(samples)-1);
subplot(3,1,1); bar(sqrt(sum(W.^2)));
subplot(3,1,2); bar(cursW);
subplot(3,1,3); bar(cursH);
if iter>1,
figure(2);
plot(objhistory(2:end));
end
drawnow;
end
% Update iteration count
iter = iter+1;
% Save old values
Wold = W;
Hold = H;
% Compute new W and H
W = W.*((V * H')./(W * H * H' + 1e-9));
H = H.*((W'*V)./(W' * W * H + 1e-9));
% Calculate objective
newobj = sum(sum((V - W*H).^2));
% fprintf('%.5f \n',(abs(newobj - objhistory(end)) / newobj * 100));
% if (abs(newobj - objhistory(end)) / newobj * 100) <= epsilon
% keepGoing = 0;
% end
if iter >= maxiter
keepGoing = 0;
end
objhistory = [objhistory newobj];
end