[r80]: src / multi_class.cc Maximize Restore History

Download this file

multi_class.cc    675 lines (556 with data), 18.6 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
 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
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_linalg.h>
#include "full_util.h"
#include "peteys_tmpl_lib.h"
#include "agf_lib.h"
#include "multi_class.h"
#define MAXNPART 200
#define MAXOPTSTACK 200
using namespace std;
using namespace libpetey;
namespace libagf {
template <class real, class cls_t>
classifier_obj<real, cls_t>::classifier_obj() {
ncls=0;
D=0;
}
template <class real, class cls_t>
classifier_obj<real, cls_t>::~classifier_obj() { }
template <class real, class cls_t>
cls_t classifier_obj<real, cls_t>::n_class() {
return ncls;
}
template <class real, class cls_t>
dim_ta classifier_obj<real, cls_t>::n_feat() {
return D;
}
template <class real, class cls_t>
cls_t classifier_obj<real, cls_t>::classify(real *x, real &pdf) {
real *p;
cls_t c;
p=new real[this->ncls];
c=classify(x, p);
pdf=p[c];
delete [] p;
return c;
}
template <class real, class cls_t>
cls_t classifier_obj<real, cls_t>::classify(real *x, real *pdf) {
real p;
cls_t c;
c=classify(x, p);
for (cls_t i=0; i<ncls; i++) pdf[i]=(1-p)/(ncls-1);
pdf[c]=p;
return c;
}
template <class real, class cls_t>
int classifier_obj<real, cls_t>::ltran(real **mat, real *b, dim_ta d1, dim_ta d2, int flag) {return 0;}
template <class real, class cls_t>
int classifier_obj<real, cls_t>::max_depth(int cur) {return 1;}
template <class real, class cls_t>
cls_t classifier_obj<real, cls_t>::class_list(cls_t *cls) {
for (cls_t i=0; i<ncls; i++) cls[i]=i;
return ncls;
}
//a whole bunch of nothing:
template <class real, class cls_t>
oneclass<real, cls_t>::oneclass(cls_t cl) {
cls=cl;
this->ncls=1;
}
//more nothing:
template <class real, class cls_t>
oneclass<real, cls_t>::~oneclass() {}
template <class real, class cls_t>
cls_t oneclass<real, cls_t>::classify(real *x, real *pdf) {
pdf[0]=1;
return cls;
}
template <class real, class cls_t>
cls_t oneclass<real, cls_t>::classify(real *x, real &pdf) {
pdf=1;
return cls;
}
template <class real, class cls_t>
cls_t oneclass<real, cls_t>::class_list(cls_t *cls1) {
cls1[0]=cls;
return this->ncls;
}
template <class real, class cls_t>
int oneclass<real, cls_t>::max_depth(int cur) {return 0;}
//normflag: pick up normalization matrix
//uflag: border samples are stored un-normalized
template <class real, class cls_t>
agf2class<real, cls_t>::agf2class(const char *fbase) {
//ave=NULL;
mat=NULL;
xtran=NULL;
int err=agf_read_borders(fbase, brd, grd, n, this->D);
if (err!=0) exit(err);
fprintf(stderr, "agf2class: %d border samples found in model, %s\n", n, fbase);
D1=this->D;
this->ncls=2;
}
template <class real, class cls_t>
agf2class<real, cls_t>::~agf2class() {
delete_matrix(brd);
delete_matrix(grd);
if (mat!=NULL) delete [] xtran;
}
//if flag, then border vectors are stored un-normalized
template <class real, class cls_t>
int agf2class<real, cls_t>::ltran(real **mat1, real *b1, dim_ta d1, dim_ta d2, int flag) {
int err2=0;
real **brd2;
real **grd2;
//err2=read_stats(normfile, ave, std, D);
mat=mat1;
b=b1;
if (flag) {
if (d1!=this->D) {
fprintf(stderr, "agf2class: first dimension (%d) of trans. mat. does not agree with that of borders data (%d)\n", d1, this->D);
return DIMENSION_MISMATCH;
}
fprintf(stderr, "agf2class: Normalising the border samples...\n");
//from the outside, the classifier looks like it has the same number of
//features as before normalization:
D1=d2;
//apply constant factor:
for (nel_ta i=0; i<n; i++) {
for (dim_ta j=0; j<D1; j++) {
brd[i][j]=brd[i][j]-b[j];
}
}
brd2=matrix_mult(brd, mat, n, this->D, D1);
grd2=matrix_mult(grd, mat, n, this->D, D1);
delete_matrix(brd);
delete_matrix(grd);
brd=brd2;
grd=grd2;
} else {
if (d2!=this->D) {
fprintf(stderr, "agf2class: second dimension of trans. mat. does not that of borders data: %d vs. %d\n", d2, this->D);
return DIMENSION_MISMATCH;
}
//this is very clear:
D1=this->D;
this->D=d1;
//from the outside, the classifier looks like it has the same number of
//features as before normalization:
}
xtran=new real[D1];
return err2;
}
template <class real, class cls_t>
cls_t agf2class<real, cls_t>::classify(real *x, real &pdf) {
real r;
cls_t cls;
if (mat!=NULL) {
real tmp;
//for (dim_ta i=0; i<this->D; i++) x[i]=x[i]-b[i];
//this whole business with the linear transformation matrix is way fucking
//more trouble than it's worth...
for (dim_ta j=0; j<D1; j++) xtran[j]=0;
for (dim_ta i=0; i<this->D; i++) {
tmp=x[i]-b[i];
for (dim_ta j=0; j<D1; j++) xtran[j]=xtran[j]+tmp*mat[i][j];
}
//xtran=left_vec_mult(x, mat, this->D, D1);
} else {
xtran=x;
}
//awesome, completely analytic (no 'if' statments):
r=border_classify(brd, grd, D1, n, xtran);
//printf("agf2class: r=%g\n", r);
pdf=(1+fabs(r))/2; //stupid: this will all be reversed...
cls=round((r+1)/2);
return cls;
}
template <class real, class cls_t>
cls_t agf2class<real, cls_t>::classify(real *x, real *pdf) {
cls_t cls;
real p;
cls=classify(x, p);
pdf[cls]=p;
pdf[abs(cls-1)]=1-p;
return cls;
}
void print_gsl_matrix(FILE *fs, gsl_matrix * mat) {
for (int i=0; i<mat->size1; i++) {
for (int j=0; j<mat->size2; j++) {
fprintf(fs, "%10.5g ", gsl_matrix_get(mat, i, j));
}
fprintf(fs, "\n");
}
}
template <class real, class cls_t, class twoclass_t>
multiclass<real, cls_t, twoclass_t>::multiclass(FILE *fs, int &lineno) {
cls_t *part[MAXNPART];
char *fname[MAXNPART];
//singular value decomposition:
gsl_matrix *imap;
//gsl_matrix *u;
//gsl_matrix *vt;
//gsl_vector *s;
gsl_vector *work;
cls_ta count;
multi_parse_param param;
param.infs=fs;
param.trainflag=0;
param.lineno=lineno;
nmodel=parse_multi_partitions(&param, fname, part, MAXNPART);
for (this->ncls=0; part[0][this->ncls]>=0; this->ncls++) {
//printf("multiclass part 0, cls 0: %d\n", part[0][this->ncls]);
}
for (count=0; part[1][count]>=0; count++) {
//printf("multiclass part 0, cls 1: %d\n", part[1][count]);
}
this->ncls+=count;
/* GIGO
if (2*nmodel+1 < this->ncls) {
fprintf(stderr, "multiclass: partition set is under-determined\n");
fprintf(stderr, " 2*npart+1 (=%d) < ncls (=%d)\n", 2*nmodel+1, this->ncls);
exit(PARAMETER_OUT_OF_RANGE);
}
*/
twoclass=new classifier_obj<real, cls_t> *[nmodel];
for (cls_t i=0; i<nmodel; i++) {
//twoclass[i]=new agf2class<real, cls_t>(fname[i]);
twoclass[i]=new twoclass_t(fname[i]);
}
//create the mapping:
imap=gsl_matrix_alloc(2*nmodel+1, this->ncls);
gsl_matrix_set_zero(imap);
//the sum of the conditional probabilities should always equal 1:
for (int i=0; i<this->ncls; i++) gsl_matrix_set(imap, nmodel*2, i, 1.);
//sum of the conditional probabilities on one side of the partition
//is equal to the conditional probability returned from the 2-class
//classification result:
for (int i=0; i<nmodel*2; i++) {
for (int j=0; part[i][j]>=0; j++) gsl_matrix_set(imap, i, part[i][j], 1.);
}
/*
printf("multiclass: pdf inverse mapping:\n");
for (int i=0; i<nmodel*2+1; i++) {
for (int j=0; j<this->ncls; j++) printf("%5.2lf ", gsl_matrix_get(imap, i, j));
printf("\n");
}
*/
//gsl_matrix_fprintf(stdout, imap, "%5.2f");
//now we find the inverse of this matrix:
u=gsl_matrix_alloc(2*nmodel+1, this->ncls);
gsl_matrix_memcpy(u, imap);
//print_gsl_matrix(stdout, u);
vt=gsl_matrix_alloc(this->ncls, this->ncls);
s=gsl_vector_alloc(this->ncls);
work=gsl_vector_alloc(this->ncls);
gsl_linalg_SV_decomp(u, vt, s, work);
//gsl_linalg_SV_decomp_jacobi(u, vt, s);
/*
printf("U:\n");
print_gsl_matrix(stdout, u);
printf("S:\n");
for (int i=0; i<s->size; i++) printf("%10.5g ", gsl_vector_get(s, i));
printf("\nV^T:\n");
print_gsl_matrix(stdout, vt);
*/
map=allocate_matrix<real, cls_t>(this->ncls, 2*nmodel+1);
for (int i=0; i<this->ncls; i++) {
for (int j=0; j<2*nmodel+1; j++) {
map[i][j]=0;
for (int k=0; k<this->ncls; k++) {
map[i][j]+=gsl_matrix_get(vt, k, i)*gsl_vector_get(s, k)*gsl_matrix_get(u, j, k);
}
}
}
//printf("multiclass: pdf mapping:\n");
//print_matrix(stdout, map, this->ncls, 2*nmodel+1);
gsl_matrix_free(imap);
//gsl_matrix_free(u);
//gsl_matrix_free(vt);
//gsl_vector_free(s);
gsl_vector_free(work);
for (int i=0; i<nmodel; i++) delete [] fname[i];
for (int i=0; i<nmodel*2; i++) delete [] part[i];
}
template <class real, class cls_t, class twoclass_t>
multiclass<real, cls_t, twoclass_t>::~multiclass() {
for (int i=0; i<nmodel; i++) delete twoclass[i];
delete [] twoclass;
delete_matrix(map);
gsl_vector_free(s);
gsl_matrix_free(vt);
gsl_matrix_free(u);
}
template <class real, class cls_t, class twoclass_t>
int multiclass<real, cls_t, twoclass_t>::ltran(real **mat, real *b, dim_ta d1, dim_ta d2, int flag) {
int err=0;
for (int i=0; i<nmodel; i++) {
err=twoclass[i]->ltran(mat, b, d1, d2, flag);
if (err!=0) {
fprintf(stderr, "multiclass::ltran: an error occured transforming partition #%d\n", i);
return err;
}
}
this->D=-1;
return err;
}
template <class real, class cls_t, class twoclass_t>
cls_t multiclass<real, cls_t, twoclass_t>::classify(real *x, real *pdf) {
real rawpdf[nmodel*2+1];
gsl_vector *b;
gsl_vector *p;
cls_t cls;
b=gsl_vector_alloc(nmodel*2+1);
p=gsl_vector_alloc(this->ncls);
//printf("multiclass raw pdfs: ");
for (int i=0; i<nmodel; i++) {
twoclass[i]->classify(x, rawpdf+i*2);
//printf(" %6.4f %6.4f", rawpdf[i*2], rawpdf[i*2+1]);
gsl_vector_set(b, i*2, rawpdf[i*2]);
gsl_vector_set(b, i*2+1, rawpdf[i*2+1]);
}
//printf("\n");
rawpdf[nmodel*2]=0;
gsl_linalg_SV_solve(u, vt, s, b, p);
cls=0;
for (int i=0; i<this->ncls; i++) {
//pdf[i]=0;
//for (int j=0; j<nmodel*2+1; j++) pdf[i]+=map[i][j]*rawpdf[j];
pdf[i]=gsl_vector_get(p, i);
if (pdf[i]>pdf[cls]) cls=i;
}
//for (int i=0; i<this->ncls; i++) printf("%g ", pdf[i]);
//printf("\n");
gsl_vector_free(b);
gsl_vector_free(p);
return cls;
}
template <class real, class cls_t, class twoclass_t>
dim_ta multiclass<real, cls_t, twoclass_t>::n_feat() {
cls_t nchild;
cls_t D1, D2;
if (this->D<=0) {
D1=twoclass[0]->n_feat();
//printf("multiclass: partition %d has %d features\n", 0, D1);
for (cls_t i=1; i<nmodel; i++) {
D2=twoclass[i]->n_feat();
//printf("multiclass: partition %d has %d features\n", i, D2);
if (D2!=D1) {
fprintf(stderr, "multiclass: number of features in classifier %d does not match that in child %d", 0, i);
fprintf(stderr, " %d vs. %d\n", D1, D2);
exit(DIMENSION_MISMATCH);
}
}
this->D=D1;
}
return this->D;
}
template <class real, class cls_t, class twoclass_t>
multiclass_hier<real, cls_t, twoclass_t>::multiclass_hier(FILE *fs, int &lineno) {
char *fname; //name of file containing model
int flag;
int c1;
char c2;
cls_t *cls;
cls_t npart;
cls_t topcls; //possible class label at top of hierarchy
multi_parse_param param;
param.infs=fs;
param.lineno=lineno;
param.trainflag=0;
fname=parse_multi_start(&param, flag);
//three possibilities:
assert(flag!=2);
if (flag==1) {
//multi-class classifier (non-hierarchical):
classifier=new multiclass<real, cls_t, twoclass_t>(fs, lineno);
//we have to scan for the opening brackets:
do {
c1=fgetc(fs);
if (c1==EOF) {
fprintf(stderr, "multiclass_hier: %d, unexpected end of file(1).\n", lineno);
exit(FILE_READ_ERROR);
}
c2=(char) c1;
//printf("6.(%c)\n", c2);
if (c2=='{') {
//we've moved to the next level:
break;
}
if (isspace(c2)==0) {
fprintf(stderr, "multiclass_hier: %d, syntax error in control file(2) at \"%c\".\n", lineno, c2);
exit(FILE_READ_ERROR);
}
if (c2=='\n') lineno++;
} while (1);
} else {
//otherwise we read in the two-class model:
//classifier=new agf2class<real, cls_t>(fname);
//printf("multiclass_hier: initializing two class classifier, %s\n", fname);
classifier=new twoclass_t(fname);
}
delete [] fname;
npart=classifier->n_class();
children=new classifier_obj<real, cls_t> *[npart];
//go to the next level in the hierarchy:
for (int i=0; i<npart; i++) {
//check to see if next classifier is a single class label:
fname=scan_class_label(fs, lineno);
if (fname==NULL) {
children[i]=new multiclass_hier<real, cls_t, twoclass_t>(fs, lineno);
} else {
sscanf(fname, "%d", &topcls);
//printf("_hier: oneclass with class, %d\n", topcls);
//"one-class" classifier:
children[i]=new oneclass<real, cls_t>(topcls);
delete [] fname;
}
}
//scan for the closing bracket:
do {
c1=fgetc(fs);
if (c1==EOF) {
fprintf(stderr, "multiclass_hier: %d, unexpected end of file(2).\n", lineno);
exit(FILE_READ_ERROR);
}
c2=(char) c1;
//printf("7.(%c)\n", c2);
if (c2=='}') {
break;
}
if (c2!=' ' && c2!='\t' && c2!='\n') {
fprintf(stderr, "multiclass_hier: %d, syntax error in control file(3) at \"%c\".\n", lineno, c2);
exit(FILE_READ_ERROR);
}
if (c2=='\n') lineno++;
} while (1);
//check to make sure everything's Kosher:
this->ncls=-1;
n_class();
//printf("_hier->ncls=%d\n", this->ncls);
cls=new cls_t[this->ncls];
class_list(cls);
heapsort_inplace(cls, this->ncls);
//printf("multiclass_hier class list: %d\n", cls[0]);
for (int i=1; i<this->ncls; i++) {
//printf("multiclass_hier class list: %d\n", cls[i]);
if (cls[i-1]==cls[i]) {
fprintf(stderr, "multiclass_hier: error, duplicate classes\n");
exit(OTHER_ERROR);
}
}
if (max_depth()==1) nonh_flag=1; else nonh_flag=0;
delete [] cls;
}
template <class real, class cls_t, class twoclass_t>
multiclass_hier<real, cls_t, twoclass_t>::~multiclass_hier() {
for (int i=0; i<classifier->n_class(); i++) delete children[i];
delete [] children;
delete classifier;
}
template <class real, class cls_t, class twoclass_t>
cls_t multiclass_hier<real, cls_t, twoclass_t>::classify(real *x, real &pdf) {
cls_t cls1, cls2;
real pdf2;
cls1=classifier->classify(x, pdf);
//printf(" ");
cls2=children[cls1]->classify(x, pdf2);
//printf("_hier: child [%d] returned %d\n", cls1, cls2);
pdf=pdf*pdf2;
return cls2;
}
template <class real, class cls_t, class twoclass_t>
cls_t multiclass_hier<real, cls_t, twoclass_t>::classify(real *x, real *pdf) {
if (nonh_flag) {
return classifier->classify(x, pdf);
} else {
return this->classifier_obj<real, cls_t>::classify(x, pdf);
}
}
template <class real, class cls_t, class twoclass_t>
int multiclass_hier<real, cls_t, twoclass_t>::ltran(real **mat, real *b, dim_ta d1, dim_ta d2, int flag) {
int err=0;
err=classifier->ltran(mat, b, d1, d2, flag);
if (err!=0) {
fprintf(stderr, "multiclass_hier::ltran: an error occurred transforming partition classifier\n");
return err;
}
for (int i=0; i<classifier->n_class(); i++) {
err=children[i]->ltran(mat, b, d1, d2, flag);
if (err!=0) {
fprintf(stderr, "multiclass_hier::ltran: an error occurred transforming child #%d\n", i);
return err;
}
}
this->D=-1;
return err;
}
template <class real, class cls_t, class twoclass_t>
cls_t multiclass_hier<real, cls_t, twoclass_t>::n_class() {
cls_t nchild;
if (this->ncls<=0) {
this->ncls=0;
nchild=classifier->n_class();
for (cls_t i=0; i<nchild; i++) this->ncls+=children[i]->n_class();
}
return this->ncls;
}
template <class real, class cls_t, class twoclass_t>
dim_ta multiclass_hier<real, cls_t, twoclass_t>::n_feat() {
cls_t nchild;
cls_t D1, D2;
if (this->D<=0) {
nchild=classifier->n_class();
D1=classifier->n_feat();
for (cls_t i=0; i<nchild; i++) {
if (children[i]->max_depth() == 0) continue;
D2=children[i]->n_feat();
if (D2!=D1) {
fprintf(stderr, "multiclass_hier: number of features in classifier does not mathc that in child %d", i);
fprintf(stderr, " %d vs. %d\n", D1, D2);
exit(DIMENSION_MISMATCH);
}
}
this->D=D1;
}
return this->D;
}
template <class real, class cls_t, class twoclass_t>
int multiclass_hier<real, cls_t, twoclass_t>::max_depth(int cur) {
int maxdepth;
int depth;
int npart;
npart=classifier->n_class();
maxdepth=0;
cur++;
for (int i=0; i<npart; i++) {
depth=children[i]->max_depth(cur);
if (depth>maxdepth) maxdepth=depth;
}
return maxdepth+1;
}
template <class real, class cls_t, class twoclass_t>
cls_t multiclass_hier<real, cls_t, twoclass_t>::class_list(cls_t *cls) {
cls_t nchild;
cls_t nc_child;
cls_t ncls1=0;
nchild=classifier->n_class();
//printf("_hier::class_list: %d children\n", nchild);
for (cls_t i=0; i<nchild; i++) {
nc_child=children[i]->class_list(cls+ncls1);
//printf("_hier::class_list: child %d has %d classes\n", i, nc_child);
ncls1+=nc_child;
}
assert(this->ncls==ncls1);
return this->ncls;
}
template class multiclass_hier<real_a, cls_ta, agf2class<real_a, cls_ta> >;
}