-
Notifications
You must be signed in to change notification settings - Fork 1
/
BMD_MoG.m
147 lines (129 loc) · 3.78 KB
/
BMD_MoG.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function [label,model,OutU,OutV,llh,converged,t] = BMD_MoG(InX,r,param)
% function [label,model,OutU,OutV,llh,converged,t] = BMD_MoG(InX,r,param)
% Perform EM algorithm for fitting the BMD_MoG model.
% Step 1: Max parameters;
% Step 2: Expectation;
% Step 3: Max weighted L2 MF;
% Step 4: Expectation.
%Input:
% InX: d x n input data matrix
% r: the rank
% param.maxiter: maximal iteration number
% param.OriX: ground truth matrix
% param.InU,InV: Initialized factorized matrices
% param.k: the number of GMM
% param.display: display the iterative process
% param.tol: the thresholding for stop
%Output:
% label:the labels of the noises
% model:model.mu, the means of the different Gaussians
% model.Sigma,the variance of the different Gaussians,sigma^2
% model.weight,the mixing coefficients
% model.alpha,the parameter of Dirichlet prior on V
% model.lambda,the parameter of Laplace prior on U
% W: d x n weighted matrix
% OutU: the fianl factorized matrix U
% OutV: the fianl factorized matrix V
% llh: the log likelihood
%
%% initialization
[d,n] = size(InX);
if (~isfield(param,'maxiter'))
maxiter = 100;
else
maxiter = param.maxiter;
end
if (~isfield(param,'OriX'))
OriX = InX;
else
OriX = param.OriX;
clear param.OriX;
end
if (~isfield(param,'InU'))
s = median(abs(InX(:)));
s = sqrt(s/r);
if min(InX(:)) >= 0
InU = rand(d,r)*s;
else
InU = rand(d,r)*s*2-s;
end
else
InU = param.InU;
end
if (~isfield(param,'InV'))
InV = rand(r,n)*s;
else
InV = param.InV;
end
if (~isfield(param,'k'))
k = 3;
else
k = param.k;
end
if (~isfield(param,'tol'))
tol = 1.0e-5;
else
tol = param.tol;
end
%%Initialize GMM parameters
tempX=InX(:);
R = initialization(tempX',k);
[~,label(1,:)] = max(R,[],2);
R = R(:,unique(label));
model.mu = zeros(1,k);
model.Sigma = rand(1,k);
model.alpha = 1.1*ones(1,r);
model.lambda = 10;
nk = sum(R,1);
model.weight = nk/size(R,1);
% llh = -inf(1,maxiter);
converged = false;
TempU = InU;
TempV = InV;
t = 1;
%%%%%%%%%%%%%%%%Initialized E Step %%%%%%%%%%%%%%%%%%%
[TempU, TempV] = expectationUV(InX,R,TempU,TempV,model);
TempX = TempU*TempV;
Error = InX(:)-TempX(:);
[R, llh(t)] = expectationR(Error',model,TempU,TempV);
%%%%%%%%%%%%%%%%Initialized E Step %%%%%%%%%%%%%%%%%%%
while ~converged && t < maxiter
t = t+1;
%%%%%%%%%%%%%%%% M Step %%%%%%%%%%%%%%%%%%%
[model] = maximizationWS(Error',R,model);
[model,k,R] = Ktuning(model,k,R);
%%%%%%%%%%%%%%%% M Step %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% E Step %%%%%%%%%%%%%%%%%%%
[TempU, TempV] = expectationUV(InX,R,TempU,TempV,model);
TempX = TempU*TempV;
Error = InX(:)-TempX(:);
[R, llh(t)] = expectationR(Error',model,TempU,TempV);
L1 = llh(t);
%%%%%%%%%%%%%%%% E Step %%%%%%%%%%%%%%%%%%%
if (~isfield(param,'k'))
[~,label(:)] = max(R,[],2);
u = unique(label); % non-empty components
if size(R,2) ~= size(u,2)
R = R(:,u); % remove empty components
else
converged = abs(llh(t)-llh(t-1)) < tol*abs(llh(t));
end
k = length(u);
else
converged = abs(llh(t)-llh(t-1)) < tol*abs(llh(t));
end
end
OutU = TempU;
OutV = TempV;
disp(['The likelihood in this step is ',num2str(L1),';']);
disp(['There are ',num2str(k),' Gaussian noises mixed in data']);
disp(['with variances ',num2str(model.Sigma)]);
disp(['with weights ',num2str(model.weight),'.']);
disp(['Relative reconstruction error ', num2str(sum(sum(((OriX - TempU*TempV)).^2))/sum(sum((OriX).^2)))]);
disp(['L2 RMSE is ', num2str(sqrt(mean(Error.^2)))]);
disp(['L1 RMSE is ', num2str(mean(abs(Error)))]);
if converged
fprintf('Converged in %d steps.\n',t-1);
else
fprintf('Not converged in %d steps.\n',maxiter);
end