-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GMM.cpp
829 lines (693 loc) · 22.4 KB
/
GMM.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Alesis Novik, Weijie Lin, Sergey Lisitsyn,
* Heiko Strathmann, Evgeniy Andreev, Chiyuan Zhang, Evan Shelhamer,
* Wuwei Lin, Marcus Edel
*/
#include <shogun/lib/config.h>
#include <shogun/base/some.h>
#include <shogun/base/Parameter.h>
#include <shogun/clustering/GMM.h>
#include <shogun/clustering/KMeans.h>
#include <shogun/distance/EuclideanDistance.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/multiclass/KNN.h>
#include <vector>
using namespace shogun;
using namespace std;
CGMM::CGMM() : CDistribution(), m_components(), m_coefficients()
{
register_params();
}
CGMM::CGMM(int32_t n, ECovType cov_type) : CDistribution(), m_components(), m_coefficients()
{
m_coefficients = SGVector<float64_t>(n);
m_components = vector<CGaussian*>(n);
for (int32_t i=0; i<n; i++)
{
m_components[i]=new CGaussian();
SG_REF(m_components[i]);
m_components[i]->set_cov_type(cov_type);
}
register_params();
}
CGMM::CGMM(vector<CGaussian*> components, SGVector<float64_t> coefficients, bool copy) : CDistribution()
{
ASSERT(int32_t(components.size())==coefficients.vlen)
if (!copy)
{
m_components=components;
m_coefficients=coefficients;
for (int32_t i=0; i<int32_t(components.size()); i++)
{
SG_REF(m_components[i]);
}
}
else
{
m_coefficients = coefficients;
m_components = vector<CGaussian*>(components.size());
for (int32_t i=0; i<int32_t(components.size()); i++)
{
m_components[i]=new CGaussian();
SG_REF(m_components[i]);
m_components[i]->set_cov_type(components[i]->get_cov_type());
SGVector<float64_t> old_mean=components[i]->get_mean();
SGVector<float64_t> new_mean = old_mean.clone();
m_components[i]->set_mean(new_mean);
SGVector<float64_t> old_d=components[i]->get_d();
SGVector<float64_t> new_d = old_d.clone();
m_components[i]->set_d(new_d);
if (components[i]->get_cov_type()==FULL)
{
SGMatrix<float64_t> old_u=components[i]->get_u();
SGMatrix<float64_t> new_u = old_u.clone();
m_components[i]->set_u(new_u);
}
m_coefficients[i]=coefficients[i];
}
}
register_params();
}
CGMM::~CGMM()
{
if (!m_components.empty())
cleanup();
}
void CGMM::cleanup()
{
for (int32_t i = 0; i < int32_t(m_components.size()); i++)
SG_UNREF(m_components[i]);
m_components = vector<CGaussian*>();
m_coefficients = SGVector<float64_t>();
}
bool CGMM::train(CFeatures* data)
{
ASSERT(m_components.size() != 0)
/** init features with data if necessary and assure type is correct */
if (data)
{
if (!data->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n")
set_features(data);
}
return true;
}
float64_t CGMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_change)
{
if (!features)
SG_ERROR("No features to train on.\n")
CDotFeatures* dotdata=(CDotFeatures *) features;
int32_t num_vectors=dotdata->get_num_vectors();
SGMatrix<float64_t> alpha;
/* compute initialization via kmeans if none is present */
if (m_components[0]->get_mean().vector==NULL)
{
CKMeans* init_k_means=new CKMeans(int32_t(m_components.size()), new CEuclideanDistance());
init_k_means->train(dotdata);
SGMatrix<float64_t> init_means=init_k_means->get_cluster_centers();
alpha=alpha_init(init_means);
SG_UNREF(init_k_means);
max_likelihood(alpha, min_cov);
}
else
alpha=SGMatrix<float64_t>(num_vectors,int32_t(m_components.size()));
int32_t iter=0;
float64_t log_likelihood_prev=0;
float64_t log_likelihood_cur=0;
SGVector<float64_t> logPxy(num_vectors * m_components.size());
SGVector<float64_t> logPx(num_vectors);
//float64_t* logPost=SG_MALLOC(float64_t, num_vectors*m_components.vlen);
while (iter<max_iter)
{
log_likelihood_prev=log_likelihood_cur;
log_likelihood_cur=0;
for (int32_t i=0; i<num_vectors; i++)
{
logPx[i]=0;
SGVector<float64_t> v=dotdata->get_computed_dot_feature_vector(i);
for (int32_t j=0; j<int32_t(m_components.size()); j++)
{
logPxy[index_t(i * m_components.size() + j)] =
m_components[j]->compute_log_PDF(v) +
std::log(m_coefficients[j]);
logPx[i] +=
std::exp(logPxy[index_t(i * m_components.size() + j)]);
}
logPx[i] = std::log(logPx[i]);
log_likelihood_cur+=logPx[i];
for (int32_t j=0; j<int32_t(m_components.size()); j++)
{
//logPost[i*m_components.vlen+j]=logPxy[i*m_components.vlen+j]-logPx[i];
alpha.matrix[i * m_components.size() + j] = std::exp(
logPxy[index_t(i * m_components.size() + j)] - logPx[i]);
}
}
if (iter>0 && log_likelihood_cur-log_likelihood_prev<min_change)
break;
max_likelihood(alpha, min_cov);
iter++;
}
return log_likelihood_cur;
}
float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov, int32_t max_em_iter, float64_t min_change)
{
if (!features)
SG_ERROR("No features to train on.\n")
if (m_components.size()<3)
SG_ERROR("Can't run SMEM with less than 3 component mixture model.\n")
CDotFeatures* dotdata = features->as<CDotFeatures>();
auto num_vectors = dotdata->get_num_vectors();
float64_t cur_likelihood=train_em(min_cov, max_em_iter, min_change);
int32_t iter=0;
SGVector<float64_t> logPxy(num_vectors * m_components.size());
SGVector<float64_t> logPx(num_vectors);
SGVector<float64_t> logPost(num_vectors * m_components.size());
SGVector<float64_t> logPostSum(m_components.size());
SGVector<float64_t> logPostSum2(m_components.size());
SGVector<float64_t> logPostSumSum(
m_components.size() * (m_components.size() - 1) / 2);
SGVector<float64_t> split_crit(m_components.size());
SGVector<float64_t> merge_crit(
m_components.size() * (m_components.size() - 1) / 2);
SGVector<int32_t> split_ind(m_components.size());
SGVector<int32_t> merge_ind(
m_components.size() * (m_components.size() - 1) / 2);
while (iter<max_iter)
{
linalg::zero(logPostSum);
linalg::zero(logPostSum2);
linalg::zero(logPostSumSum);
for (int32_t i=0; i<num_vectors; i++)
{
logPx[i]=0;
SGVector<float64_t> v=dotdata->get_computed_dot_feature_vector(i);
for (int32_t j=0; j<int32_t(m_components.size()); j++)
{
logPxy[index_t(i * m_components.size() + j)] =
m_components[j]->compute_log_PDF(v) +
std::log(m_coefficients[j]);
logPx[i] +=
std::exp(logPxy[index_t(i * m_components.size() + j)]);
}
logPx[i] = std::log(logPx[i]);
for (int32_t j=0; j<int32_t(m_components.size()); j++)
{
logPost[index_t(i * m_components.size() + j)] =
logPxy[index_t(i * m_components.size() + j)] - logPx[i];
logPostSum[j] +=
std::exp(logPost[index_t(i * m_components.size() + j)]);
logPostSum2[j] +=
std::exp(2 * logPost[index_t(i * m_components.size() + j)]);
}
int32_t counter=0;
for (int32_t j=0; j<int32_t(m_components.size()); j++)
{
for (int32_t k=j+1; k<int32_t(m_components.size()); k++)
{
logPostSumSum[counter] += std::exp(
logPost[index_t(i * m_components.size() + j)] +
logPost[index_t(i * m_components.size() + k)]);
counter++;
}
}
}
int32_t counter=0;
for (int32_t i=0; i<int32_t(m_components.size()); i++)
{
logPostSum[i] = std::log(logPostSum[i]);
split_crit[i]=0;
split_ind[i]=i;
for (int32_t j=0; j<num_vectors; j++)
{
split_crit[i] +=
(logPost[index_t(j * m_components.size() + i)] -
logPostSum[i] -
logPxy[index_t(j * m_components.size() + i)] +
std::log(m_coefficients[i])) *
(std::exp(logPost[index_t(j * m_components.size() + i)]) /
std::exp(logPostSum[i]));
}
for (int32_t j=i+1; j<int32_t(m_components.size()); j++)
{
merge_crit[counter] = std::log(logPostSumSum[counter]) -
(0.5 * std::log(logPostSum2[i])) -
(0.5 * std::log(logPostSum2[j]));
merge_ind[counter]=i*m_components.size()+j;
counter++;
}
}
CMath::qsort_backward_index(
split_crit.vector, split_ind.vector, int32_t(m_components.size()));
CMath::qsort_backward_index(
merge_crit.vector, merge_ind.vector,
int32_t(m_components.size() * (m_components.size() - 1) / 2));
bool better_found=false;
int32_t candidates_checked=0;
for (int32_t i=0; i<int32_t(m_components.size()); i++)
{
for (int32_t j=0; j<int32_t(m_components.size()*(m_components.size()-1)/2); j++)
{
if (merge_ind[j]/int32_t(m_components.size()) != split_ind[i] && int32_t(merge_ind[j]%m_components.size()) != split_ind[i])
{
candidates_checked++;
CGMM* candidate=new CGMM(m_components, m_coefficients, true);
candidate->train(features);
candidate->partial_em(split_ind[i], merge_ind[j]/int32_t(m_components.size()), merge_ind[j]%int32_t(m_components.size()), min_cov, max_em_iter, min_change);
float64_t cand_likelihood=candidate->train_em(min_cov, max_em_iter, min_change);
if (cand_likelihood>cur_likelihood)
{
cur_likelihood=cand_likelihood;
set_comp(candidate->get_comp());
set_coef(candidate->get_coef());
for (int32_t k=0; k<int32_t(candidate->get_comp().size()); k++)
{
SG_UNREF(candidate->get_comp()[k]);
}
better_found=true;
delete candidate;
break;
}
else
delete candidate;
if (candidates_checked>=max_cand)
break;
}
}
if (better_found || candidates_checked>=max_cand)
break;
}
if (!better_found)
break;
iter++;
}
return cur_likelihood;
}
void CGMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min_cov, int32_t max_em_iter, float64_t min_change)
{
CDotFeatures* dotdata=(CDotFeatures *) features;
int32_t num_vectors=dotdata->get_num_vectors();
SGVector<float64_t> init_logPxy(num_vectors * m_components.size());
SGVector<float64_t> init_logPx(num_vectors);
SGVector<float64_t> init_logPx_fix(num_vectors);
SGVector<float64_t> post_add(num_vectors);
for (int32_t i=0; i<num_vectors; i++)
{
init_logPx[i]=0;
init_logPx_fix[i]=0;
SGVector<float64_t> v=dotdata->get_computed_dot_feature_vector(i);
for (int32_t j=0; j<int32_t(m_components.size()); j++)
{
init_logPxy[index_t(i * m_components.size() + j)] =
m_components[j]->compute_log_PDF(v) +
std::log(m_coefficients[j]);
init_logPx[i] +=
std::exp(init_logPxy[index_t(i * m_components.size() + j)]);
if (j!=comp1 && j!=comp2 && j!=comp3)
{
init_logPx_fix[i] +=
std::exp(init_logPxy[index_t(i * m_components.size() + j)]);
}
}
init_logPx[i] = std::log(init_logPx[i]);
post_add[i] = std::log(
std::exp(
init_logPxy[index_t(i * m_components.size() + comp1)] -
init_logPx[i]) +
std::exp(
init_logPxy[index_t(i * m_components.size() + comp2)] -
init_logPx[i]) +
std::exp(
init_logPxy[index_t(i * m_components.size() + comp3)] -
init_logPx[i]));
}
vector<CGaussian*> components(3);
SGVector<float64_t> coefficients(3);
components[0]=m_components[comp1];
components[1]=m_components[comp2];
components[2]=m_components[comp3];
coefficients.vector[0]=m_coefficients.vector[comp1];
coefficients.vector[1]=m_coefficients.vector[comp2];
coefficients.vector[2]=m_coefficients.vector[comp3];
float64_t coef_sum=coefficients.vector[0]+coefficients.vector[1]+coefficients.vector[2];
int32_t dim_n=components[0]->get_mean().vlen;
float64_t alpha1=coefficients.vector[1]/(coefficients.vector[1]+coefficients.vector[2]);
float64_t alpha2=coefficients.vector[2]/(coefficients.vector[1]+coefficients.vector[2]);
float64_t noise_mag =
SGVector<float64_t>::twonorm(components[0]->get_mean().vector, dim_n) *
0.1 / std::sqrt((float64_t)dim_n);
SGVector<float64_t> mean(dim_n);
linalg::add(components[1]->get_mean(), components[2]->get_mean(), mean, alpha1, alpha2);
components[1]->set_mean(mean);
for (int32_t i=0; i<dim_n; i++)
{
components[2]->get_mean().vector[i]=components[0]->get_mean().vector[i]+CMath::randn_double()*noise_mag;
components[0]->get_mean().vector[i]=components[0]->get_mean().vector[i]+CMath::randn_double()*noise_mag;
}
coefficients.vector[1]=coefficients.vector[1]+coefficients.vector[2];
coefficients.vector[2]=coefficients.vector[0]*0.5;
coefficients.vector[0]=coefficients.vector[0]*0.5;
if (components[0]->get_cov_type()==FULL)
{
SGMatrix<float64_t> c1=components[1]->get_cov();
SGMatrix<float64_t> c2=components[2]->get_cov();
linalg::add(c1, c2, c1, alpha1, alpha2);
SGVector<float64_t> eigenvalues(dim_n);
linalg::eigen_solver_symmetric(c1, eigenvalues, c1);
components[1]->set_d(eigenvalues);
components[1]->set_u(c1);
float64_t new_d=0;
for (int32_t i=0; i<dim_n; i++)
{
new_d += std::log(components[0]->get_d().vector[i]);
for (int32_t j=0; j<dim_n; j++)
{
if (i==j)
{
components[0]->get_u().matrix[i*dim_n+j]=1;
components[2]->get_u().matrix[i*dim_n+j]=1;
}
else
{
components[0]->get_u().matrix[i*dim_n+j]=0;
components[2]->get_u().matrix[i*dim_n+j]=0;
}
}
}
new_d = std::exp(new_d * (1. / dim_n));
for (int32_t i=0; i<dim_n; i++)
{
components[0]->get_d().vector[i]=new_d;
components[2]->get_d().vector[i]=new_d;
}
}
else if(components[0]->get_cov_type()==DIAG)
{
auto result_d = components[1]->get_d();
auto temp_d = components[2]->get_d();
linalg::add(result_d, temp_d, result_d, alpha1, alpha2);
components[1]->set_d(result_d);
float64_t new_d=0;
for (int32_t i=0; i<dim_n; i++)
{
new_d += std::log(components[0]->get_d().vector[i]);
}
new_d = std::exp(new_d * (1. / dim_n));
for (int32_t i=0; i<dim_n; i++)
{
components[0]->get_d().vector[i]=new_d;
components[2]->get_d().vector[i]=new_d;
}
}
else if(components[0]->get_cov_type()==SPHERICAL)
{
components[1]->get_d().vector[0]=alpha1*components[1]->get_d().vector[0]+
alpha2*components[2]->get_d().vector[0];
components[2]->get_d().vector[0]=components[0]->get_d().vector[0];
}
CGMM* partial_candidate=new CGMM(components, coefficients);
partial_candidate->train(features);
float64_t log_likelihood_prev=0;
float64_t log_likelihood_cur=0;
int32_t iter=0;
SGMatrix<float64_t> alpha(num_vectors, 3);
SGVector<float64_t> logPxy(num_vectors * 3);
SGVector<float64_t> logPx(num_vectors);
//float64_t* logPost=SG_MALLOC(float64_t, num_vectors*m_components.vlen);
while (iter<max_em_iter)
{
log_likelihood_prev=log_likelihood_cur;
log_likelihood_cur=0;
for (int32_t i=0; i<num_vectors; i++)
{
logPx[i]=0;
SGVector<float64_t> v=dotdata->get_computed_dot_feature_vector(i);
for (int32_t j=0; j<3; j++)
{
logPxy[i * 3 + j] = components[j]->compute_log_PDF(v) +
std::log(coefficients[j]);
logPx[i] += std::exp(logPxy[i * 3 + j]);
}
logPx[i] = std::log(logPx[i] + init_logPx_fix[i]);
log_likelihood_cur+=logPx[i];
for (int32_t j=0; j<3; j++)
{
//logPost[i*m_components.vlen+j]=logPxy[i*m_components.vlen+j]-logPx[i];
alpha.matrix[i * 3 + j] =
std::exp(logPxy[i * 3 + j] - logPx[i] + post_add[i]);
}
}
if (iter>0 && log_likelihood_cur-log_likelihood_prev<min_change)
break;
partial_candidate->max_likelihood(alpha, min_cov);
partial_candidate->get_coef().vector[0]=partial_candidate->get_coef().vector[0]*coef_sum;
partial_candidate->get_coef().vector[1]=partial_candidate->get_coef().vector[1]*coef_sum;
partial_candidate->get_coef().vector[2]=partial_candidate->get_coef().vector[2]*coef_sum;
iter++;
}
m_coefficients.vector[comp1]=coefficients.vector[0];
m_coefficients.vector[comp2]=coefficients.vector[1];
m_coefficients.vector[comp3]=coefficients.vector[2];
delete partial_candidate;
}
void CGMM::max_likelihood(SGMatrix<float64_t> alpha, float64_t min_cov)
{
CDotFeatures* dotdata=(CDotFeatures *) features;
int32_t num_dim=dotdata->get_dim_feature_space();
float64_t alpha_sum;
float64_t alpha_sum_sum=0;
for (int32_t i=0; i<alpha.num_cols; i++)
{
alpha_sum=0;
SGVector<float64_t> mean_sum(num_dim);
linalg::zero(mean_sum);
for (int32_t j=0; j<alpha.num_rows; j++)
{
alpha_sum+=alpha.matrix[j*alpha.num_cols+i];
SGVector<float64_t> v=dotdata->get_computed_dot_feature_vector(j);
linalg::add(
v, mean_sum, mean_sum, alpha.matrix[j * alpha.num_cols + i],
1.0);
}
linalg::scale(mean_sum, mean_sum, 1.0 / alpha_sum);
m_components[i]->set_mean(mean_sum);
SGMatrix<float64_t> cov_sum;
ECovType cov_type = m_components[i]->get_cov_type();
if (cov_type==FULL)
{
cov_sum = SGMatrix<float64_t>(num_dim, num_dim);
linalg::zero(cov_sum);
}
else if(cov_type==DIAG)
{
cov_sum = SGMatrix<float64_t>(1, num_dim);
linalg::zero(cov_sum);
}
else if(cov_type==SPHERICAL)
{
cov_sum = SGMatrix<float64_t>(1, 1);
linalg::zero(cov_sum);
}
for (int32_t j=0; j<alpha.num_rows; j++)
{
SGVector<float64_t> v=dotdata->get_computed_dot_feature_vector(j);
linalg::add(v, mean_sum, v, 1.0, -1.0);
switch (cov_type)
{
case FULL:
linalg::dger(
alpha.matrix[j * alpha.num_cols + i], v, v, cov_sum);
break;
case DIAG:
{
auto temp_matrix = SGMatrix<float64_t>(v.vector, 1, v.vlen);
auto temp_result = linalg::matrix_prod(
temp_matrix, temp_matrix, true, false);
cov_sum = temp_result.get_diagonal_vector().clone();
linalg::scale(
cov_sum, cov_sum, alpha.matrix[j * alpha.num_cols + i]);
}
break;
case SPHERICAL:
float64_t temp = 0;
temp = linalg::dot(v, v);
cov_sum(0, 0) +=
temp * alpha.matrix[j * alpha.num_cols + i];
break;
}
}
switch (cov_type)
{
case FULL:
{
linalg::scale(cov_sum, cov_sum, 1.0 / alpha_sum);
SGVector<float64_t> d0(num_dim);
linalg::eigen_solver_symmetric(cov_sum, d0, cov_sum);
for (auto& v: d0)
v = CMath::max(min_cov, v);
m_components[i]->set_d(d0);
m_components[i]->set_u(cov_sum);
break;
}
case DIAG:
for (int32_t j = 0; j < num_dim; j++)
{
cov_sum(0, j) /= alpha_sum;
cov_sum(0, j) = CMath::max(min_cov, cov_sum(0, j));
}
m_components[i]->set_d(cov_sum.get_row_vector(0));
break;
case SPHERICAL:
cov_sum[0] /= alpha_sum * num_dim;
cov_sum[0] = CMath::max(min_cov, cov_sum[0]);
m_components[i]->set_d(cov_sum.get_row_vector(0));
break;
}
m_coefficients.vector[i]=alpha_sum;
alpha_sum_sum+=alpha_sum;
}
linalg::scale(m_coefficients, m_coefficients, 1.0 / alpha_sum_sum);
}
int32_t CGMM::get_num_model_parameters()
{
return 1;
}
float64_t CGMM::get_log_model_parameter(int32_t num_param)
{
ASSERT(num_param==1)
return std::log(m_components.size());
}
index_t CGMM::get_num_components() const
{
return m_components.size();
}
CDistribution* CGMM::get_component(index_t index) const
{
return m_components[index];
}
float64_t CGMM::get_log_derivative(int32_t num_param, int32_t num_example)
{
SG_NOTIMPLEMENTED
return 0;
}
float64_t CGMM::get_log_likelihood_example(int32_t num_example)
{
SG_NOTIMPLEMENTED
return 1;
}
float64_t CGMM::get_likelihood_example(int32_t num_example)
{
float64_t result=0;
ASSERT(features);
ASSERT(features->get_feature_class() == C_DENSE);
ASSERT(features->get_feature_type() == F_DREAL);
for (auto i: range(index_t(m_components.size())))
{
SGVector<float64_t> point= ((CDenseFeatures<float64_t>*) features)->get_feature_vector(num_example);
result += std::exp(
m_components[i]->compute_log_PDF(point) +
std::log(m_coefficients[i]));
}
return result;
}
SGVector<float64_t> CGMM::get_nth_mean(int32_t num)
{
ASSERT(num<int32_t(m_components.size()))
return m_components[num]->get_mean();
}
void CGMM::set_nth_mean(SGVector<float64_t> mean, int32_t num)
{
ASSERT(num<int32_t(m_components.size()))
m_components[num]->set_mean(mean);
}
SGMatrix<float64_t> CGMM::get_nth_cov(int32_t num)
{
ASSERT(num<int32_t(m_components.size()))
return m_components[num]->get_cov();
}
void CGMM::set_nth_cov(SGMatrix<float64_t> cov, int32_t num)
{
ASSERT(num<int32_t(m_components.size()))
m_components[num]->set_cov(cov);
}
SGVector<float64_t> CGMM::get_coef()
{
return m_coefficients;
}
void CGMM::set_coef(const SGVector<float64_t> coefficients)
{
m_coefficients=coefficients;
}
vector<CGaussian*> CGMM::get_comp()
{
return m_components;
}
void CGMM::set_comp(vector<CGaussian*> components)
{
for (int32_t i=0; i<int32_t(m_components.size()); i++)
{
SG_UNREF(m_components[i]);
}
m_components=components;
for (int32_t i=0; i<int32_t(m_components.size()); i++)
{
SG_REF(m_components[i]);
}
}
SGMatrix<float64_t> CGMM::alpha_init(SGMatrix<float64_t> init_means)
{
CDotFeatures* dotdata=(CDotFeatures *) features;
auto num_vectors=dotdata->get_num_vectors();
SGVector<float64_t> label_num(init_means.num_cols);
linalg::range_fill(label_num);
auto knn=some<CKNN>(1, new CEuclideanDistance(), new CMulticlassLabels(label_num));
knn->train(new CDenseFeatures<float64_t>(init_means));
auto init_labels = knn->apply(features)->as<CMulticlassLabels>();
SGMatrix<float64_t> alpha(num_vectors, index_t(m_components.size()));
for (auto i: range(num_vectors))
alpha[i * m_components.size() + init_labels->get_int_label(i)] = 1;
SG_UNREF(init_labels);
return alpha;
}
SGVector<float64_t> CGMM::sample()
{
REQUIRE(m_components.size()>0, "Number of mixture components is %d but "
"must be positive\n", m_components.size());
float64_t rand_num = CMath::random(0.0, 1.0);
float64_t cum_sum=0;
for (auto i: range(m_coefficients.vlen))
{
cum_sum+=m_coefficients.vector[i];
if (cum_sum>=rand_num)
{
SG_DEBUG("Sampling from mixture component %d\n", i);
return m_components[i]->sample();
}
}
return m_components[m_coefficients.vlen-1]->sample();
}
SGVector<float64_t> CGMM::cluster(SGVector<float64_t> point)
{
SGVector<float64_t> answer(m_components.size()+1);
answer.vector[m_components.size()]=0;
for (auto i: range(index_t(m_components.size())))
{
answer.vector[i] = m_components[i]->compute_log_PDF(point) +
std::log(m_coefficients[i]);
answer.vector[m_components.size()] += std::exp(answer.vector[i]);
}
answer.vector[m_components.size()] =
std::log(answer.vector[m_components.size()]);
return answer;
}
void CGMM::register_params()
{
//TODO serialization broken
//m_parameters->add((SGVector<CSGObject*>*) &m_components, "m_components", "Mixture components");
SG_ADD(
&m_coefficients, "m_coefficients", "Mixture coefficients.",
MS_NOT_AVAILABLE);
}