-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNMF.m
More file actions
86 lines (75 loc) · 1.79 KB
/
NMF.m
File metadata and controls
86 lines (75 loc) · 1.79 KB
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
86
function [W,H,err,iteration] = NMF(X, Wini, Hini , threshold,verbose,normW,varargin)
% X = F times N
% W = F times K
% H = K times N
% fixed_flag = Either 'W' or 'H' it allows to fix one of them with the
% inital value, so it will not be optimized.
x = 1;
err = Inf;
errdif = 1;
Errors = [];
flag_fixedW = 0;
flag_fixedH = 0;
affine = 'n'; % If 'y', the algorithms assumes a DC component that is substracted to reduce invariant solutions.
aini = rand(size(X,1),1);
%while err>threshold && errdif > 1e-3 && x<1e7
while errdif > threshold && x<1e4
if x ==1
H = Hini;
W = Wini;
a = aini;
if length(varargin)>0
fixed_flag = varargin{1};
if fixed_flag =='W'
flag_fixedW = 1;
end
if fixed_flag =='H'
flag_fixedH = 1;
end
end
end
if affine == 'y'
Xp = W*H + a;
else
Xp = W*H;
end
err0 = 100*norm(X-Xp,'fro')/norm(X,'fro');
if flag_fixedW ==0
W = W.*((X*H')./(W*(H*H')));
if strcmp(normW,'y')
[W] = NormalizeW(W,1);
end
end
if flag_fixedH ==0
H = H.*((W'*X)./(W'*W*H));
end
if affine == 'y'
Xp = W*H + a;
else
Xp = W*H;
end
err = 100*norm(X-Xp,'fro')/norm(X,'fro');
if x>1
errdif = errp - err;
errp = err;
else
errp = err;
end
Errors = [Errors err];
if isnan(err)
disp('Nan');
end
if mod(x,1)==0
disp(['Iteration: ' num2str(x) '===> Error: ' num2str(err) ' errdif: ' num2str(errdif)])
end
if verbose ==1
figure(1000)
plot(x,err,'*b');
grid on
hold on
drawnow
end
x = x+1;
end
iteration = x;
end