-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLCGMM.m
294 lines (255 loc) · 7.46 KB
/
LCGMM.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
function [pkx_final, cluster_final, LogL_final] = LCGMM(X, k, W, options)
% Local Consistent Gaussian Mixture Model (LCGMM)
%
% where
% X
% Notation:
% X ... (nSmp x mFea) observed data matrix
% nSmp ... number of samples
% mFea ... number of features
%
% K ... number of clusters
% W ... weight matrix of the affinity graph
%
% options ... Structure holding all settings
%
% You only need to provide the above four inputs.
%
% pkx ... P(z|x)
% R ... covariance matrix
% mu ... mean
%
%
% References:
% [1] Jialu Liu, Deng Cai, Xiaofei He, "Gaussian Mixture Model with
% Local Consistency", AAAI 2010.
% [2] Jialu Liu, "Notes on Local Consistent Gaussian Mixture Model(LCGMM)",
% Online available at http://relau.com/jialuliu/technical_notes/LCGMM.pdf
% [accessed 27-Dec-2011].
%
% version 2.0 --Dec/2011
% version 1.0 --Dec/2009
%
% Written by Jialu Liu (remenberl AT gmail.com)
% Deng Cai (dengcai AT gmail.com)
%
ZERO_OFFSET = 1e-200;
differror = 1e-7;
if isfield(options,'error')
differror = options.error;
end
lambda = 0.1;
if isfield(options,'lambda')
lambda = options.lambda;
end
nRepeat = 5;
if isfield(options,'nRepeat')
nRepeat = options.nRepeat;
end
maxIter = 100;
if isfield(options,'maxIter')
maxIter = options.maxIter;
end
minIterOrig = 5;
if isfield(options,'minIter')
minIterOrig = options.minIter;
end
minIter = minIterOrig-1;
meanFitRatio = 0.1;
if isfield(options,'meanFitRatio')
meanFitRatio = options.meanFitRatio;
end
meanFitControl = 1;
if isfield(options,'meanFitControl')
meanFitControl = options.meanFitControl;
end
if ~isfield(options,'InitWay')
options.InitWay = 'kmeans';
end
show = 0;
if isfield(options,'show')
show = options.show;
end
debug = 0;
if isfield(options,'debug')
debug = options.debug;
end
% init mixture
[nSmp mFea] = size(X);
if lambda > 0
DCol = full(sum(W,2));
D = spdiags(DCol,0,nSmp,nSmp);
L = D - W;
end
[cluster,pkx,LogL] = GMM_init(X,k,options);
meanFit = LogL/10;
tryNo = 0;
selectInit = 1;
nIter = 0;
while tryNo < nRepeat
tryNo = tryNo+1;
maxErr = 1;
retry = 0;
while(maxErr > differror || maxErr==-1)
% EM iteration
alertFlag = 0;
for kidx=1:k
% compute pi
cluster(kidx).pb = sum(pkx(:,kidx));
if cluster(kidx).pb < 1e-20
retry = 1;
break;
end
% compute Tk
if lambda > 0
Tk = 1 - lambda * sum(bsxfun(@minus, pkx(:,kidx), pkx(:,kidx)') .* W, 2) ./ (pkx(:,kidx) + ZERO_OFFSET);
else
Tk = ones(nSmp, 1);
end
if min(Tk) < 0 && alertFlag == 0 && debug == 1
alertFlag = 1;
disp('The covariance matrix might not be positive semidefinate since lambda is too big such that some value of Tik is negative.');
end
% compute mean
cluster(kidx).mu = ((pkx(:,kidx) .* Tk)' * X) / cluster(kidx).pb;
% compute covariance matrix
Y1 = X-repmat(cluster(kidx).mu,nSmp,1);
Y2 = bsxfun(@times,sqrt(pkx(:,kidx) .* Tk), Y1);
R = (Y2'*Y2) /cluster(kidx).pb;
%for elemetns in Tk which are negative
Y3 = bsxfun(@times,sqrt(pkx(:,kidx) .* (-(Tk<0) .* Tk)), Y1);
R = R - 2 * (Y3'*Y3) /cluster(kidx).pb;
%
clear Y2;
R = max(R,R');
detR = det(R);
if detR <= 0
retry = 1;
break;
end
cluster(kidx).cov = R;
const = -(mFea*log(2*pi) + log(detR))/2;
Y2 = R\Y1';
Y2 = -Y2'/2;
pkx(:,kidx) = dot(Y1,Y2,2)+const;
clear Y1 Y2 R;
end
if retry
break;
end
llmax=max(pkx,[],2);
pkx =exp( pkx-repmat(llmax,1,k) );
pkx = pkx.*repmat([cluster(:).pb],nSmp,1);
ss = sum(pkx,2);
llnew = sum(log(ss)+llmax);
pkx = pkx./(repmat(ss,1,k));
% compute new likelihood
if lambda > 0
llnew = llnew - sum(sum((log(pkx' + ZERO_OFFSET) * L).* pkx')) * lambda / 2;
end
LogL = [LogL llnew];
%
nIter = nIter + 1;
meanFit = meanFitRatio*meanFit + (1-meanFitRatio)*llnew;
maxErr = (llnew-meanFit)/meanFit;
if show
if length(LogL) > 1
disp(['tryNo: ',num2str(tryNo),' Iteration: ',num2str(nIter),' LogL: ',num2str(LogL(end)),' deltaLogL: ',num2str(LogL(end)-LogL(end-1)),' maxErr:',num2str(maxErr)]);
else
disp(['tryNo: ',num2str(tryNo),' Iteration: ',num2str(nIter),' LogL: ',num2str(LogL(end)),' maxErr:',num2str(maxErr)]);
end
end
if nRepeat > 1 && selectInit
maxErr = 1;
end
if ~meanFitControl
maxErr = 1;
end
if nIter > minIter
if selectInit
maxErr = 0;
else
if nIter >= maxIter
maxErr = 0;
end
end
end
end
if retry && ~(tryNo == nRepeat && nIter >= nIter_final)
tryNo = tryNo - 1;
[cluster,pkx,LogL] = GMM_init(X,k,options);
meanFit = LogL/10;
nIter = 0;
continue;
end
if tryNo == 1
pkx_final = pkx;
cluster_final = cluster;
LogL_final = LogL;
nIter_final = nIter;
else
if LogL(end) > LogL_final(end)
pkx_final = pkx;
cluster_final = cluster;
LogL_final = LogL;
nIter_final = nIter;
end
end
if selectInit
if tryNo < nRepeat
[cluster,pkx,LogL] = GMM_init(X,k,options);
meanFit = LogL/10;
nIter = 0;
else
tryNo = tryNo - 1;
selectInit = 0;
pkx = pkx_final;
cluster = cluster_final;
LogL = LogL_final;
nIter = nIter_final;
meanFit = LogL(end)/10;
end
end
end
function [cluster,pkx,llnew] = GMM_init(X,k,options)
ZERO_OFFSET = 1e-200;
[nSmp, mFea] = size(X);
if strcmpi(options.InitWay,'kmeans')
kmeansres = litekmeans(X,k,'maxIter',10);
residx = unique(kmeansres);
for kidx=1:k
smpidx = kmeansres==residx(kidx);
cluster(kidx).mu = mean(X(smpidx,:),1);
cluster(kidx).pb = 1/k;
end
else
permutation = randperm(nSmp);
nSmpClass = floor(nSmp/k);
for kidx=1:k
cluster(kidx).mu = mean(X(permutation((kidx-1)*nSmpClass+1:kidx*nSmpClass),:),1);
cluster(kidx).pb = 1/k;
end
end
R = (nSmp-1)*cov(X)/nSmp;
R = max(R,R');
% EM iteration
pkx=zeros(nSmp,k);
detR = det(R);
if detR <= 0
error('The covariance matrix is not positive definite. Use PCA to reduce the dimensions first!');
end
const = -(mFea*log(2*pi) +log(detR))/2;
for kidx=1:k
Y1=X-repmat(cluster(kidx).mu,nSmp,1);
Y2 = R\Y1';
Y2 = -Y2'/2;
pkx(:,kidx) = dot(Y1,Y2,2)+const;
clear Y1 Y2;
end
clear R;
llmax=max(pkx,[],2);
pkx =exp( pkx-repmat(llmax,1,k) );
pkx = pkx.*repmat([cluster(:).pb],nSmp,1);
ss = sum(pkx,2);
llnew = sum(log(ss)+llmax);
pkx = pkx./(repmat(ss,1,k));