-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnmf_IS.m
124 lines (100 loc) · 2.68 KB
/
mnmf_IS.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function [ wrt, H, T, V ] = mnmf_IS( X, K, Itr )
% time counting start
tic;
exe_time = 0;
% Getting size
[M,~,I,J] = size( X );
% initialization
T = abs(randn( I, K ));
V = abs(randn( K, J ));
H = zeros( M, M, I, K );
Id = eye( M, M ) / M;
for ii=1:I
for kk=1:K
H(:,:,ii,kk) = Id;
end
end
% Scale Fitting
Xf = make_fctrz( H, T, V );
up = X(:)' * Xf(:);
low = Xf(:)' * Xf(:);
coef = sqrt( up / low );
T = T * coef;
V = V * coef;
Xf = Xf * coef * coef;
% Itration by IS
wrt = zeros( Itr, 2 );
Xfp = zeros( size( Xf ) );
for i=1:I
for j=1:J
Xfp(:,:,i,j) = pinv(Xf(:,:,i,j) );
end
end
for lp=1:Itr
% create new tmp variables
tmpT = T;
tmpV = V;
tmpH = H;
for k=1:K
xhu = zeros( I, J );
xhl = zeros( I, J );
for i=1:I
for j=1:J
vp = vec( Xfp(:,:,i,j)' );
vxph = vec( X(:,:,i,j) * Xfp(:,:,i,j) * H(:,:,i,k) );
vh = vec( H(:,:,i,k) );
tru(i,j) = ( vp' * vxph );
trl(i,j) = ( vp' * vh );
end
end
% update T
up = tru * V(k,:)' ;
low = trl * V(k,:)' ;
tmpT(:,k) = T(:,k) .* sqrt(up ./ low);
% update V
up = T(:,k)' * tru;
low = T(:,k)' * trl;
tmpV(k,:) = V(k,:) .* sqrt(up ./ low);
% update H
for i=1:I
% update H
A = zeros(M,M);
BB = zeros(M,M);
for j=1:J
tmp = V(k,j) * Xfp(:,:,i,j);
A = A + tmp;
BB = BB + tmp * X(:,:,i,j) * Xfp(:,:,i,j);
end
B = H(:,:,i,k) * BB * H(:,:,i,k);
%function of riccati
tmp = Riccati(A,B);
tmp = ( tmp + tmp' ) / 2;
tmp = tmp / trace( tmp );
tmpH(:,:,i,k) = tmp;
end
end
% Update variables
H = tmpH;
V = tmpV;
T = tmpT;
Xf = make_fctrz( H, T, V );
for i=1:I
for j=1:J
Xfp(:,:,i,j) = pinv(Xf(:,:,i,j) );
end
end
exe_time = exe_time + toc;
% make IS
IS = zeros( I, J );
for i=1:I
for j=1:J
t = X(:,:,i,j) * Xfp(:,:,i,j);
IS(i,j) = trace(t) - log(det(t)) - M;
end
end
D = reshape( IS, I*J,1 );
% Extension IS
wrt(lp,:) = [ exe_time norm(D,1) ];
tic;
end
end