-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptron.c
764 lines (503 loc) · 20.9 KB
/
perceptron.c
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
#include "perceptron.h"
#include "debug.h"
#include "uthash.h"
#include "corpus.h"
#include "dependency.h"
#include "memman.h"
#include "parseutil.h"
extern enum BudgetMethod budget_method;
extern size_t budget_target;
extern int verbosity;
extern const char *modelname;
extern enum Kernel kernel;
#include <time.h>
#include <stdlib.h>
KernelPerceptron create_PolynomialKernelPerceptron(int power, float bias) {
srand(time(NULL));
KernelPerceptron kp = (KernelPerceptron) malloc(sizeof (struct KernelPerceptron));
check(kp != NULL, "KernelPerceptron allocation error");
kp->M = 0;
kp->alpha = NULL;
kp->beta = NULL;
kp->alpha_avg = NULL;
kp->best_alpha_avg = NULL;
kp->best_kernel_matrix = NULL;
kp->c = 1;
kp->kernel_matrix = NULL;
kp->arch_to_index_map = NULL;
kp->kernel = KPOLYNOMIAL;
kp->bias = bias;
kp->power = power;
log_info("Polynomial kernel of degree %d with bias %f is created", kp->power, kp->bias);
return kp;
error:
exit(1);
}
KernelPerceptron create_RBFKernelPerceptron(float lambda) {
srand(time(NULL));
KernelPerceptron kp = (KernelPerceptron) malloc(sizeof (struct KernelPerceptron));
check(kp != NULL, "KernelPerceptron allocation error");
kp->M = 0;
kp->alpha = NULL;
kp->beta = NULL;
kp->alpha_avg = NULL;
kp->best_alpha_avg = NULL;
kp->best_kernel_matrix = NULL;
kp->c = 1;
kp->kernel_matrix = NULL;
kp->arch_to_index_map = NULL;
kp->kernel = KRBF;
kp->rbf_lambda = lambda;
log_info("RBF kernel with lambda %f is created", kp->rbf_lambda);
return kp;
error:
exit(1);
}
/*
alpha_t* create_alpha_idx(int sentence_idx, int from, int to, int length) {
IS_ARC_VALID(from, to, length);
alpha_t *idx = (alpha_t*) malloc(sizeof (alpha_t));
check_mem(idx);
idx->sentence_idx = sentence_idx;
idx->from = from;
idx->to = to;
return idx;
error:
log_err("Error in allocating alpha index for sentence %d arch %d->%d", sentence_idx, from, to);
exit(1);
}
*/
/**
* Allocate and initialize an new alpha_key
*
* @param sidx Sentence index
* @param from from word index
* @param to to word index
*
* @return Initialized point to alpha_key allocated
*/
alpha_key_t* create_alpha_key(uint32_t sidx, uint16_t from, uint16_t to) {
alpha_key_t *key;
key = (alpha_key_t*) malloc(sizeof (alpha_key_t));
check(key != NULL, "Alpha key allocation error");
memset(key, 0, sizeof (alpha_key_t));
key->sentence_idx = sidx;
key->from = from;
key->to = to;
return key;
error:
exit(1);
}
unsigned get_keylen() {
return offsetof(alpha_t, to) /* offset of last key field */
+ sizeof (uint16_t) /* size of last key field */
- offsetof(alpha_t, sentence_idx); /* offset of first key field */
}
void update_alpha(KernelPerceptron kp, uint32_t sidx, uint16_t from, uint16_t to, struct FeaturedSentence* sent, float inc) {
unsigned keylen;
alpha_t *a, *dummy = NULL;
alpha_key_t *a_key = NULL;
keylen = get_keylen();
a_key = create_alpha_key(sidx, from, to);
HASH_FIND(hh, kp->arch_to_index_map, &a_key->sentence_idx, keylen, a);
if (a != NULL) {
(kp->alpha)[a->idx] += inc;
(kp->beta)[a->idx] += inc * kp->c;
} else {
vector v = embedding_feature(sent, from, to, NULL);
size_t n = (kp->M);
if (n == 0) {
kp->N = v->n;
kp->kernel_matrix = (float*) mkl_64bytes_malloc((n + 1) * v->n * sizeof (float));
for (size_t i = 0; i < v->n; i++)
(kp->kernel_matrix)[n * v->n + i] = v->data[i];
kp->alpha = (float*) mkl_64bytes_malloc((n + 1) * sizeof (float));
kp->beta = (float*) mkl_64bytes_malloc((n + 1) * sizeof (float));
(kp->alpha)[n] = inc;
(kp->beta)[n] = inc * kp->c;
} else {
check(kp->N == v->n, "%lu dimensional embedding does not confirm with the previous embedding size (%lu)", v->n, kp->N);
kp->kernel_matrix = (float*) mkl_64bytes_realloc(kp->kernel_matrix, (n + 1) * v->n * sizeof (float));
for (size_t i = 0; i < v->n; i++)
(kp->kernel_matrix)[n * v->n + i] = v->data[i];
kp->alpha = (float*) mkl_64bytes_realloc(kp->alpha, (n + 1) * sizeof (float));
kp->beta = (float*) mkl_64bytes_realloc(kp->beta, (n + 1) * sizeof (float));
(kp->alpha)[n] = inc;
(kp->beta)[n] = inc * kp->c;
}
vector_free(v);
a = (alpha_t*) malloc(sizeof (alpha_t));
memset(a, 0, sizeof (alpha_t)); /* zero fill */
a->from = from;
a->to = to;
a->sentence_idx = sidx;
a->idx = n;
HASH_ADD(hh, kp->arch_to_index_map, sentence_idx, keylen, a);
debug("%u keys in alpha", HASH_COUNT(kp->arch_to_index_map));
(kp->M)++;
}
return;
error:
exit(1);
}
size_t delete_hypothesis(KernelPerceptron kp, size_t idx) {
if (kp->M > 1) {
unsigned keylen = get_keylen();
alpha_t *a = NULL;
alpha_t *rm_v = NULL, *mv_v = NULL;
alpha_key_t *rm_k = NULL, *mv_k = NULL;
for (a = kp->arch_to_index_map; a != NULL && (mv_v == NULL || rm_v == NULL); a = a->hh.next) {
// You found the one to be moved
if (a->idx == kp->M - 1) {
mv_v = a;
//mv_k = create_alpha_key(a->sentence_idx, a->from,a->to);
}
// You found the one to be deleted
if (a->idx == idx) {
rm_v = a;
//rm_k = create_alpha_key(a->sentence_idx, a->from,a->to);
}
}
check(mv_v != NULL && rm_v != NULL, "Check the code. move_key or rm_key is NULL");
int alpha_chopped = (int) (kp->alpha)[rm_v->idx];
if (alpha_chopped != 1 && alpha_chopped != -1)
return false;
(kp->alpha)[rm_v->idx] = (kp->alpha)[mv_v->idx];
kp->alpha = (float*) mkl_64bytes_realloc(kp->alpha, ((kp->M) - 1) * sizeof (float));
(kp->beta)[rm_v->idx] = (kp->beta)[mv_v->idx];
kp->beta = (float*) mkl_64bytes_realloc(kp->beta, ((kp->M) - 1) * sizeof (float));
for (size_t i = 0; i < kp->N; i++) {
kp->kernel_matrix[ rm_v->idx * kp->N + i ] = kp->kernel_matrix[ mv_v->idx * kp->N + i ];
}
kp->kernel_matrix = (float*) mkl_64bytes_realloc(kp->kernel_matrix, ((kp->M - 1) * kp->N) * sizeof (float));
if (mv_v == rm_v) {
HASH_DEL(kp->arch_to_index_map, mv_v);
} else {
HASH_DEL(kp->arch_to_index_map, mv_v);
HASH_DEL(kp->arch_to_index_map, rm_v);
}
a = (alpha_t*) malloc(sizeof (alpha_t));
memset(a, 0, sizeof (alpha_t)); /* zero fill */
a->from = mv_v->from;
a->to = mv_v->to;
a->sentence_idx = mv_v->sentence_idx;
a->idx = rm_v->idx;
HASH_ADD(hh, kp->arch_to_index_map, sentence_idx, keylen, a);
if (mv_v == rm_v) {
free(mv_v);
} else {
free(mv_v);
free(rm_v);
}
(kp->M)--;
return true;
} else {
log_warn("There are only %lu hypothesis vectors left. Do deletion will be done.", kp->M);
return false;
}
error:
exit(1);
}
#define MAX_FAIL_TO_DELETE 5
size_t delete_n_random_hypothesis(KernelPerceptron kp, size_t delete_count) {
size_t ndel_success = 0;
if (kp->M > delete_count + 1) {
for (size_t i = 0; i < delete_count; i++) {
bool deleted = false;
int fail_to_delete = 0;
while (!deleted && fail_to_delete < MAX_FAIL_TO_DELETE) {
size_t r = ((size_t) rand()) % (kp->M);
bool rc = delete_hypothesis(kp, r);
if (rc) {
deleted = true;
ndel_success++;
} else {
fail_to_delete++;
}
}
}
} else {
log_warn("There are only %lu hypothesis vectors left. %lu vectors are asked for deletion.Do deletion will be done.", kp->M, delete_count);
}
return ndel_success;
}
void update_average_alpha(KernelPerceptron kp) {
if (kp->alpha_avg != NULL) {
mkl_free(kp->alpha_avg);
}
kp->alpha_avg = (float*) mkl_64bytes_malloc((kp->M) * sizeof (float));
for (size_t i = 0; i < (kp->M); i++) {
(kp->alpha_avg)[i] = (kp->alpha)[i] - (kp->beta)[i] / (kp->c);
}
}
static void dump_support_vectors(KernelPerceptron mdl) {
char filename[1024];
time_t result = time(NULL);
sprintf(filename, "%s.%d.sv", modelname, (int) result);
FILE *fp = fopen(filename, "w");
for (size_t i = 0; i < mdl->M; i++) {
fprintf(fp, "%f\t", mdl->alpha[i]);
for (size_t j = 0; j < mdl->N; j++) {
fprintf(fp, "%f", mdl->kernel_matrix[i * mdl->N + j]);
if (j < mdl->N - 1)
fprintf(fp, "\t");
}
fprintf(fp, "\n");
}
fclose(fp);
}
void train_once_KernelPerceptronModel(KernelPerceptron mdl, const CoNLLCorpus corpus, int max_rec) {
long match = 0, total = 0;
//size_t slen=0;
double s_initial = dsecnd();
int max_sv = 0;
log_info("Total number of training instances %d", (max_rec == -1) ? DArray_count(corpus->sentences) : max_rec);
for (int si = 0; si < ((max_rec == -1) ? DArray_count(corpus->sentences) : max_rec); si++) {
FeaturedSentence sent = (FeaturedSentence) DArray_get(corpus->sentences, si);
debug("Building feature matrix for sentence %d", si);
set_FeatureMatrix(NULL, corpus, si);
set_adjacency_matrix_fast(corpus, si, mdl, false);
max_sv += (sent->length + 1) * sent->length - sent->length;
int *model = parse(sent);
//printfarch(model, sent->length);
debug("Parsing sentence %d of length %d is done", si, sent->length);
int *empirical = get_parents(sent);
//printfarch(empirical, sent->length);
int nm = nmatch(model, empirical, sent->length);
debug("Model matches %d arcs out of %d arcs", nm, sent->length);
if (nm != sent->length) { // root has no valid parent.
log_info("Sentence %d (section %d) of length %d (%d arcs out of %d arcs are correct)", si, sent->section, sent->length, nm, sent->length);
int sentence_length = sent->length;
for (int to = 1; to <= sentence_length; to++) {
if (model[to] != empirical[to]) {
update_alpha(mdl, si, model[to], to, sent, -1);
update_alpha(mdl, si, empirical[to], to, sent, +1);
}
}
} else {
log_info("Sentence %d (section %d) of length %d (Perfect parse)", si, sent->section, sent->length);
}
size_t nsuccess;
if (budget_method == RANDOMIZED) {
if (mdl->M > budget_target) {
size_t nbefore = mdl->M;
size_t nasked = nbefore - budget_target;
nsuccess = delete_n_random_hypothesis(mdl, nasked);
log_info("%lu vectors deleted (%lu asked). Current hypothesis set size reduced from %lu to %lu", nsuccess, nasked, nbefore, mdl->M);
}
}
mdl->c++;
free_feature_matrix(corpus, si);
match += nm;
total += (sent->length);
if ((si + 1) % 1000 == 0 && si != 0) {
log_info("Running training accuracy %lf after %d sentence.", (match * 1.) / total, si + 1);
unsigned nsv = mdl->M;
log_info("%u (%f of total %d) support vectors", nsv, (nsv * 1.) / max_sv, max_sv);
}
free(model);
free(empirical);
}
unsigned nsv = mdl->M;
log_info("Running training accuracy %lf", (match * 1.) / total);
log_info("%u (%f of total %d) support vectors", nsv, (nsv * 1.) / max_sv, max_sv);
if (verbosity > 0) {
dump_support_vectors(mdl);
}
update_average_alpha(mdl);
return;
}
void dump_conll_word(Word w, bool true_parent, FILE* ofp) {
for (int i = 0; i < 10; i++) {
if (i != 6)
fprintf(ofp, "%s", (char*) DArray_get(w->conll_piece, i));
else {
if (true_parent)
fprintf(ofp, "%d", w->parent);
else
fprintf(ofp, "%d", w->predicted_parent);
}
if (i < 9)
fprintf(ofp, "\t");
}
fprintf(ofp, "\n");
}
ParserTestMetric test_KernelPerceptronModel(void *mdl, const CoNLLCorpus corpus, bool use_temp_weight, FILE *gold_ofp, FILE *model_ofp) {
ParserTestMetric metric = create_ParserTestMetric();
PerceptronModel linear_mdl;
KernelPerceptron kernel_mdl;
if (kernel == KLINEAR) {
linear_mdl = (PerceptronModel) mdl;
} else {
kernel_mdl = (KernelPerceptron) mdl;
}
for (int si = 0; si < DArray_count(corpus->sentences); si++) {
FeaturedSentence sent = DArray_get(corpus->sentences, si);
debug("Test sentence %d (section %d) of length %d", si, sent->section, sent->length);
if (kernel != KLINEAR) {
debug("Generating feature matrix for sentence %d", si);
set_FeatureMatrix(NULL, corpus, si);
}
if (kernel == KLINEAR) {
if (use_temp_weight) {
debug("\tI will be using a weight vector (raw) of length %ld", linear_mdl->embedding_w_temp->n);
build_adjacency_matrix(corpus, si, linear_mdl->embedding_w_temp, NULL);
} else {
debug("\tI will be using a weight vector (averaged) of length %ld", linear_mdl->embedding_w->n);
build_adjacency_matrix(corpus, si, linear_mdl->embedding_w, NULL);
}
} else {
debug("Generating adj. matrix for sentence %d", si);
set_adjacency_matrix_fast(corpus, si, kernel_mdl, true);
}
debug("Now parsing sentence %d", si);
int *model = parse(sent);
(metric->total_sentence)++;
debug("Now comparing actual arcs with model generated arcs for sentence %d (Last sentence is %d)", si, sent->length);
for (int j = 0; j < sent->length; j++) {
Word w = (Word) DArray_get(sent->words, j);
w->predicted_parent = model[j + 1];
// TODO: One file per section idea
if (model_ofp != NULL)
dump_conll_word(w, true, model_ofp);
if (gold_ofp != NULL)
dump_conll_word(w, false, gold_ofp);
if (w->parent == 0 && model[j + 1] == 0)
(metric->true_root_predicted)++;
debug("\tTrue parent of word %d (with %s:%s) is %d whereas estimated parent is %d", j + 1, w->postag, w->form, w->parent, model[j + 1]);
int pmatch_nopunc = 0, ptotal_nopunc = 0, pmatch = 0;
if (strcmp(w->postag, ",") != 0 && strcmp(w->postag, ":") != 0 && strcmp(w->postag, ".") != 0 && strcmp(w->postag, "``") != 0 && strcmp(w->postag, "''") != 0) {
if (w->parent == model[j + 1]) {
(metric->without_punc->true_prediction)++;
pmatch_nopunc++;
}
ptotal_nopunc++;
(metric->without_punc->total_prediction)++;
}
if (pmatch_nopunc == ptotal_nopunc && pmatch_nopunc != 0) {
(metric->complete_sentence_without_punc)++;
}
(metric->all->total_prediction)++;
if (w->parent == model[j + 1]) {
pmatch++;
(metric->all->true_prediction)++;
}
if (pmatch == sent->length && pmatch != 0)
(metric->complete_sentence)++;
}
if (model_ofp != NULL) {
fprintf(model_ofp, "\n");
}
if (gold_ofp != NULL) {
fprintf(gold_ofp, "\n");
}
free(model);
debug("Releasing feature matrix for sentence %d", si);
free_feature_matrix(corpus, si);
}
return metric;
}
void mark_best_KernelPerceptronModel(KernelPerceptron kmodel, int numit) {
kmodel->best_numit = numit;
if (kmodel->best_alpha_avg != NULL) {
mkl_free(kmodel->best_alpha_avg);
}
if (kmodel->best_kernel_matrix != NULL) {
mkl_free(kmodel->best_kernel_matrix);
}
kmodel->best_alpha_avg = (float*) mkl_64bytes_malloc((kmodel->M) * sizeof (float));
kmodel->best_kernel_matrix = (float*) mkl_64bytes_malloc((kmodel->N) * (kmodel->M) * sizeof (float));
for (size_t i = 0; i < (kmodel->M); i++) {
(kmodel->best_alpha_avg)[i] = (kmodel->alpha_avg)[i];
}
size_t mn = (kmodel->N) * (kmodel->M);
#pragma ivdep
#pragma loop_count min(102400)
for (size_t i = 0; i < mn; i++) {
(kmodel->best_kernel_matrix)[i] = (kmodel->kernel_matrix)[i];
}
kmodel->best_m = kmodel->M;
}
void dump_KernelPerceptronModel(FILE *fp, KernelPerceptron kp) {
fprintf(fp, "kernel=%d\n", kp->kernel);
fprintf(fp, "power=%d\n", kp->power);
fprintf(fp, "bias=%f\n", kp->bias);
fprintf(fp, "nsv=%lu\n", kp->best_m);
fprintf(fp, "edim=%lu\n", kp->N);
fprintf(fp, "numit=%d\n", kp->best_numit);
fprintf(fp, "c=%d\n", kp->c);
for (size_t i = 0; i < kp->best_m; i++) {
//fprintf(fp, "alpha_avg[%lu]=%f alpha[%lu]=%f beta[%lu]=%f\n", i, (kp->best_alpha_avg)[i],i, (kp->alpha)[i],i, (kp->beta)[i]);
fprintf(fp, "alpha[%lu]=%f\n", i, (kp->best_alpha_avg)[i]);
}
for (size_t i = 0; i < kp->best_m * kp->N; i++) {
fprintf(fp, "K[%lu]=%f\n", i, (kp->best_kernel_matrix)[i]);
}
}
PerceptronModel load_PerceptronModel(FILE *fp){
size_t dummy;
char buffer[1024];
int n = fscanf(fp, "edimension=%lu\n",&dummy);
check(n == 1,"Embedding dimension can not read");
n = fscanf(fp, "epattern=%s\n",buffer);
check(n == 1,"Embedding pattern can not read");
n = fscanf(fp, "bestnumit=%lu\n",&dummy);
check(n == 1,"Best numit can not read");
n = fscanf(fp, "transformation=%s\n",buffer);
check(n == 1,"Embedding transformation can not read");
n = fscanf(fp, "dimension=%lu\n",&dummy);
check(n == 1,"Transformed embedding dimension can not read");
PerceptronModel model = create_PerceptronModel(dummy, NULL);
size_t real_idx;
for (size_t i = 0; i < dummy; i++) {
n = fscanf(fp, "%lu=%f\n", &real_idx, &((model->embedding_w->data)[i]));
check(n == 2, "Either index (%lu) or coefficient(%f) is missing", real_idx, (model->embedding_w->data)[i]);
check(i == real_idx, "Expected index (%lu) does not match with current index(%lu)", i, real_idx);
(model->embedding_w_avg->data)[i] = (model->embedding_w->data)[i];
(model->embedding_w_best->data)[i] = (model->embedding_w->data)[i];
(model->embedding_w_temp->data)[i] = (model->embedding_w->data)[i];
}
return model;
error:
return NULL;
}
KernelPerceptron load_KernelPerceptronModel(FILE *fp) {
enum Kernel type;
int n = fscanf(fp, "kernel=%d\n", &type);
debug("Kernel type is %d", type);
check(n == 1, "No kernel type found in file");
check(type == KPOLYNOMIAL, "Only POLYNOMIAL is supported. %d is not", type);
int power;
float bias;
n = fscanf(fp, "power=%d\n", &power);
check(n == 1 && power > 0, "No power found for polynomial model");
debug("Power is %d", power);
n = fscanf(fp, "bias=%f\n", &bias);
check(n == 1, "No bias found for polynomial model");
debug("Bias is %f", bias);
KernelPerceptron model = create_PolynomialKernelPerceptron(power, bias);
n = fscanf(fp, "nsv=%lu\nedim=%lu\nnumit=%d\nc=%d\n", &(model->M), &(model->N), &(model->best_numit), &(model->c));
check(n == 4, "Num s.v. or embedding dimension or numit or c is missing in model file");
debug("Number of Support Vectors are %lu", model->M);
debug("Embedding dimensiob is %lu", model->N);
debug("Number of Iterations are %d", model->best_numit);
debug("C is %d", model->c);
model->alpha_avg = (float*) mkl_64bytes_malloc(model->M * sizeof (float));
model->alpha = (float*) mkl_64bytes_malloc(model->M * sizeof (float));
size_t real_idx;
for (size_t i = 0; i < model->M; i++) {
n = fscanf(fp, "alpha[%lu]=%f\n", &real_idx, &((model->alpha)[i]));
check(n == 2, "Either index (%lu) or coefficient(%f) is missing", real_idx, (model->alpha)[i]);
check(i == real_idx, "Expected index (%lu) does not match with current index(%lu)", i, real_idx);
(model->alpha_avg)[i] = (model->alpha)[i];
}
model->kernel_matrix = (float*) mkl_64bytes_malloc(model->M * model->N * sizeof (float));
for (size_t i = 0; i < model->M * model->N; i++) {
n = fscanf(fp, "K[%lu]=%f\n", &real_idx, &((model->kernel_matrix)[i]));
check(n == 2, "Either index (%lu) or coefficient(%f) is missing", real_idx, (model->kernel_matrix)[i]);
check(i == real_idx, "Expected index (%lu) does not match with current index(%lu)", i, real_idx);
}
return model;
error:
return NULL;
}