-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathssvm.cpp
More file actions
2314 lines (2039 loc) · 68.2 KB
/
Copy pathssvm.cpp
File metadata and controls
2314 lines (2039 loc) · 68.2 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
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* vi:ts=4:tw=78:shiftwidth=4:expandtab
* vim600:fdm=marker
*/
/**
@file ssvm.cpp
@brief (linear chain) Structural SVMs
@author Changki Lee (leeck@kangwon.ac.kr)
@date 2013/3/1
*/
#ifdef WIN32
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#pragma warning(disable: 4267)
#pragma warning(disable: 4244)
#pragma warning(disable: 4018)
#endif
#include <cassert>
#include <stdexcept> //for std::runtime_error
#include <memory> //for std::bad_alloc
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <string.h>
#include <stdlib.h>
#include "ssvm.hpp"
#include "timer.hpp"
#include "pqueue.hpp"
using namespace std;
SSVM::SSVM() {
#ifdef BINARY_FEATURE
cerr << "# You cannot use general feature mode!" << endl;
#endif
// pointer
theta = NULL;
edge = "@@@edge="; // 내부적으로만 쓰인다
n_pred = 0;
n_theta = 0;
n_event = 0;
n_test_event = 0;
default_oid = 0;
// make_pid4train
is_train = 0;
// parameter
use_comment = 0;
owps_format = 0;
hash_feature = 0;
support_feature = 0;
general_feature = 0;
incremental = 0;
beam = 0;
verbose = 0;
binary = 0;
skip_eval = 0;
train_num = 0;
threshold = 1e-04;
// pegasos
iter = 100;
period = 0;
// svm
cost = 1;
rm_inactive = 50; // inactive const 제거 조건 (iteration)
buf = 100;
eps = 0.01;
// linear이면 1e-25
precision = 1e-25;
// final optimality check for shrinking
final_opt_check = 0;
// GRAM size
gram_size = 100;
// domain adaptation
domain_adaptation = 0;
// Joint SSVM
y_cost = 1;
z_cost = 1;
init_iter = 10;
}
SSVM::~SSVM() {
clear();
}
void SSVM::clear() {
n_theta = n_outcome = n_event = n_test_event = 0;
if (theta != NULL) {
delete[] theta;
theta = NULL;
}
edge_pid.clear();
train_data.clear();
test_data.clear();
train_data_comment.clear();
test_data_comment.clear();
m_vec.clear();
// ssvm
alpha.clear();
alpha_history.clear();
work_set.clear();
loss.clear();
x_norm_vec.clear();
sent_ids.clear();
work_set_ids.clear();
y_seq_vec.clear();
opti.clear();
slacks.clear();
slacks_id.clear();
cost_diff_vec.clear();
// gram matrix
for (size_t i=0; i < gram.size(); i++) {
gram[i].clear();
}
gram.clear();
gram_size = 100;
}
void SSVM::load(const string model) {
ifstream f(model.c_str());
if (!f) {
cerr << "Fail to open file: " << model << endl;
exit(1);
}
cerr << "loading " << model << " ... ";
timer t;
n_theta = 0;
int count, fid;
int i;
string line;
// check model format
getline(f, line);
if (!strncmp(line.c_str(), "#txt,shash", 10)) {
cerr << "Model format: sparse hash txt model" << endl;
hash_feature = 2;
} else if (!strncmp(line.c_str(), "#txt,hash", 9)) {
cerr << "Model format: hash txt model" << endl;
hash_feature = 1;
} else if (!strncmp(line.c_str(), "#txt,ssvm", 9)) {
cerr << "Model format: ssvm txt model" << endl;
hash_feature = 0;
} else if (!strncmp(line.c_str(), "#txt,maxent", 11)) { // 하위호환
cerr << "Model format: crf txt model" << endl;
hash_feature = 0;
} else {
cerr << "Model format error: not txt model!" << endl;
char temp[100];
strncpy(temp, line.c_str(), 9);
temp[9] = 0;
cerr << temp << endl;
exit(1);
}
// read context predicates
getline(f, line);
n_pred = atoi(line.c_str());
if (!hash_feature) {
for (i = 0; i < n_pred; ++i) {
getline(f, line);
pred_map[line] = i;
pred_vec.push_back(line);
}
cerr << "(pred_map:" << t.elapsed() << ") ";
}
// read outcomes
getline(f, line);
count = atoi(line.c_str());
for (i = 0; i < count; ++i) {
getline(f, line);
outcome_map[line] = i;
outcome_vec.push_back(line);
}
// read paramaters (count가 0 이면 all_feature, 아니면 support_feature)
getline(f, line);
count = atoi(line.c_str());
if (count == 0) {
support_feature = false;
} else {
support_feature = true;
vector<string> tokens;
fid = 0;
vector<pair<int, int> > param;
for (i = 0; i < count; ++i) {
param.clear();
getline(f, line);
int oid;
tokenize(line, tokens, " \t");
vector<string>::iterator it = tokens.begin();
++it; // skip count which is only used in binary format
for (; it != tokens.end(); it++) {
oid = atoi(it->c_str());
param.push_back(make_pair(oid,fid++));
}
params.push_back(param);
}
}
// load theta
getline(f, line);
n_theta = atoi(line.c_str());
if (theta != NULL) {
delete[] theta;
}
theta = new float[n_theta];
// sparse hash: set zero
if (hash_feature == 2) {
for (i = 0; i < n_theta; ++i) theta[i] = 0;
}
i = 0;
unsigned int uint;
while (getline(f, line)) {
assert(!line.empty());
// sparse hash
if (hash_feature == 2) {
uint = atoi(line.c_str());
if (uint < 0 || uint >= n_theta) {
cerr << "Error: i=" << i << " index=" << uint << " n_theta=" << n_theta << endl;
}
getline(f, line);
theta[uint] = atof(line.c_str());
i++;
} else {
// hash, support, all
theta[i] = atof(line.c_str());
i++;
}
}
if (hash_feature != 2) assert(i == n_theta);
n_outcome = outcome_vec.size();
make_edge_pid();
// 소요 시간 출력
cerr << "(" << t.elapsed() << ") done." << endl;
}
void SSVM::load_bin(const string model) {
FILE *f;
f = fopen(model.c_str(), "rb");
if (!f) {
cerr << "Fail to open file: " << model << endl;
exit(1);
}
cerr << "loading " << model << " ... ";
timer t;
n_theta = 0;
int count, fid, len, i, j;
// 너무 많이 잡으면 win32에서 에러가 남
char buffer[1024*16];
// check model format
fread((void*)&buffer, sizeof("#bin,hash"), 1, f);
if (!strncmp(buffer, "#bin,shash", sizeof("#bin,hash"))) {
cerr << "Model format: sparse hash binary model!" << endl;
hash_feature = 2;
} else if (!strncmp(buffer, "#bin,hash", sizeof("#bin,hash"))) {
cerr << "Model format: hash binary model!" << endl;
hash_feature = 1;
} else if (!strncmp(buffer, "#bin,ssvm", sizeof("#bin,ssvm"))) {
cerr << "Model format: ssvm binary model!" << endl;
hash_feature = 0;
} else if (!strncmp(buffer, "#bin,maxen", sizeof("#bin,maxe"))) { // 하위호환
cerr << "Model format: crf binary model!" << endl;
hash_feature = 0;
} else {
cerr << "Model format error: not binary model!" << endl;
buffer[sizeof("#bin,hash")] = 0;
cerr << buffer << endl;
exit(1);
}
// read context predicates
fread((void*)&n_pred, sizeof(n_pred), 1, f);
if (!hash_feature) {
for (i = 0; i < n_pred; ++i) {
fread((void*)&len, sizeof(len), 1, f);
fread((void*)&buffer, len, 1, f);
string line(buffer, len);
pred_map[line] = i;
pred_vec.push_back(line);
}
cerr << "(pred_map:" << t.elapsed() << ") ";
}
// read outcomes
fread((void*)&count, sizeof(count), 1u, f);
for (i = 0; i < count; ++i) {
fread((void*)&len, sizeof(len), 1u, f);
fread((void*)&buffer, len, 1u, f);
string line(buffer, len);
outcome_map[line] = i;
outcome_vec.push_back(line);
}
// read paramaters (count가 0 이면 all_feature, 아니면 support_feature)
fread((void*)&count, sizeof(count), 1u, f);
if (count == 0) {
support_feature = false;
} else {
support_feature = true;
fid = 0;
vector<pair<int, int> > param;
for (i=0; i < count; i++) {
param.clear();
// 현재는 이전 포맷과의 호환성 문제로 oid를 int로 저장
int count2, oid;
fread((void*)&count2, sizeof(count2), 1u, f);
for (j=0; j < count2; j++) {
fread((void*)&oid, sizeof(oid), 1u, f);
param.push_back(make_pair(oid,fid++));
}
params.push_back(param);
}
}
// load theta
fread((void*)&n_theta, sizeof(n_theta), 1u, f);
if (theta != NULL) {
delete[] theta;
}
theta = new float[n_theta];
float theta_i;
// sparse hash
if (hash_feature == 2) {
// set zero
for (i = 0; i < n_theta; ++i) theta[i] = 0;
// read data
int nonzero_num, index;
fread((void*)&nonzero_num, sizeof(int), 1u, f);
for (i = 0; i < nonzero_num; ++i) {
fread((void*)&index, sizeof(int), 1u, f);
fread((void*)&theta_i, sizeof(float), 1u, f);
theta[index] = theta_i;
}
} else {
// hash, support, all
for (i = 0; i < n_theta; ++i) {
fread((void*)&theta_i, sizeof(float), 1u, f);
theta[i] = theta_i;
}
}
fclose(f);
n_outcome = outcome_vec.size();
make_edge_pid();
// 소요 시간 출력
cerr << "(" << t.elapsed() << ") done." << endl;
}
void SSVM::save(const string model) {
FILE *f;
f = fopen(model.c_str(), "w");
if (!f) {
cerr << "Unable to open model file to write: " << model << endl;
exit(1);
}
// todo: write a header section here
if (hash_feature == 2) {
fprintf(f, "#txt,shash\n");
} else if (hash_feature) {
fprintf(f, "#txt,hash\n");
} else {
fprintf(f, "#txt,ssvm\n");
}
// n_pred
fprintf(f, "%d\n", n_pred);
if (!hash_feature) {
for (int i = 0; i < n_pred; ++i) {
fprintf(f, "%s\n", pred_vec[i].c_str());
}
}
fprintf(f, "%d\n", outcome_vec.size());
for (int i = 0; i < outcome_vec.size(); ++i) {
fprintf(f, "%s\n", outcome_vec[i].c_str());
}
if (support_feature) {
fprintf(f, "%d\n", params.size());
for (int i = 0; i < params.size(); ++i) {
vector<pair<int, int> >& param = params[i];
fprintf(f, "%d", param.size());
for (int j = 0; j < param.size(); ++j) {
fprintf(f, " %d", param[j].first);
}
fprintf(f, "\n");
}
} else {
fprintf(f, "0\n");
}
// write theta
fprintf(f, "%d\n", n_theta);
for (int i = 0; i < n_theta; ++i) {
// sparse hash
if (hash_feature == 2) {
if (theta[i] != 0) {
fprintf(f, "%d\n", i);
fprintf(f, "%g\n", theta[i]);
}
} else {
// hash, support, all
fprintf(f, "%g\n", theta[i]);
}
}
fclose(f);
}
void SSVM::save_bin(const string model) {
FILE *f;
f = fopen(model.c_str(), "wb");
if (!f) {
cerr << "Unable to open model file to write: " << model << endl;
exit(1);
}
int i, j, uint;
// todo: write a header section here
if (hash_feature == 2) {
// sparse hash
fwrite((void*)"#bin,shash", sizeof("#bin,hash"), 1u, f);
} else if (hash_feature) {
fwrite((void*)"#bin,hash", sizeof("#bin,hash"), 1u, f);
} else {
fwrite((void*)"#bin,ssvm", sizeof("#bin,ssvm"), 1u, f);
}
// n_pred
uint = n_pred;
fwrite((void*)&uint, sizeof(uint), 1u, f);
if (!hash_feature) {
for (i = 0; i < n_pred; ++i) {
uint = pred_vec[i].size();
fwrite((void*)&uint, sizeof(uint), 1u, f);
fwrite((void*)pred_vec[i].c_str(), pred_vec[i].size(), 1u, f);
}
}
uint = outcome_vec.size();
fwrite((void*)&uint, sizeof(uint), 1u, f);
for (i = 0; i < outcome_vec.size(); ++i) {
uint = outcome_vec[i].size();
fwrite((void*)&uint, sizeof(uint), 1u, f);
fwrite((void*)outcome_vec[i].c_str(), outcome_vec[i].size(), 1u, f);
}
if (support_feature) {
uint = params.size();
fwrite((void*)&uint, sizeof(uint), 1u, f);
for (i = 0; i < params.size(); ++i) {
vector<pair<int, int> >& param = params[i];
uint = param.size();
fwrite((void*)&uint, sizeof(uint), 1u, f);
for (j = 0; j < param.size(); ++j) {
// 현재는 이전 포맷과의 호환성 문제로 oid를 int로 저장
uint = param[j].first;
fwrite((void*)&uint, sizeof(uint), 1u, f);
}
}
} else {
uint = 0;
fwrite((void*)&uint, sizeof(uint), 1u, f);
}
// write theta
uint = n_theta;
fwrite((void*)&uint, sizeof(uint), 1u, f);
float theta_i;
// sparse hash
if (hash_feature == 2) {
uint = 0;
for (i = 0; i < n_theta; ++i) if (theta[i] != 0) uint++;
fwrite((void*)&uint, sizeof(uint), 1u, f);
for (i = 0; i < n_theta; ++i) {
if (theta[i] != 0) {
uint = i;
fwrite((void*)&uint, sizeof(uint), 1u, f);
theta_i = theta[i];
fwrite((void*)&theta_i, sizeof(float), 1u, f);
}
}
} else {
// hash, support, all
for (i = 0; i < n_theta; ++i) {
theta_i = theta[i];
fwrite((void*)&theta_i, sizeof(float), 1u, f);
}
}
fclose(f);
}
// load event and make param
void SSVM::load_event(const string file) {
string line, comment;
int i, j, count = 0;
int old_n_theta = n_theta;
int old_pred_vec_size = pred_vec.size();
ifstream f(file.c_str());
if (!f) {
cerr << "Can not open data file to read: " << file << endl;
exit(1);
}
if (hash_feature && support_feature) {
cerr << "Hash + Support feature mode is not avalable!" << endl;
exit(1);
}
sent_t sent;
context_t cont;
while (getline(f, line)) {
// remove newline for windows format file
string find_str = "\r";
string::size_type find_pos = line.find(find_str);
if (string::npos != find_pos) {
line.replace(find_pos, find_str.size(), "");
}
if (line.empty()) {
// comment 처리
if (use_comment) {
train_data_comment.push_back(comment);
comment = "";
}
if (!owps_format) {
train_data.push_back(sent);
sent.clear();
if (train_num > 0 && train_num <= train_data.size()) {
break;
}
}
} else if (line[0] == '#') {
// comment 처리
if (use_comment) {
if (comment == "") {
comment += line;
} else {
comment += "\n";
comment += line;
}
}
} else {
/// Tokenizer
vector<string> tokens;
tokenize(line, tokens, " \t");
vector<string>::iterator it = tokens.begin();
// outcome
int oid;
if (outcome_map.find(*it) == outcome_map.end()) {
//cerr << *it << " ";
oid = outcome_vec.size();
outcome_map[*it] = oid;
outcome_vec.push_back(*it);
if (verbose) cerr << "New outcome: " << *it << endl;
} else {
oid = outcome_map[*it];
if (outcome_vec[oid] != *it) {
cerr << "oid=" << oid << " : " << outcome_vec[oid] << " != " << *it << endl;
exit(1);
}
}
node_t node;
node.outcome = oid;
node.start = sent.size();
node.end = sent.size();
it++;
cont.clear();
for (; it != tokens.end(); it++) {
string fi(it->c_str());
// test
//cerr << " " << *it;
// pred
int pid;
bool new_pid = false;
// qid:n.m 처리
if (fi.find("qid:") != string::npos) {
vector<string> str_vec, str_vec2;
split(fi, str_vec, ":");
split(str_vec[1], str_vec2, ".");
node.start = atoi(str_vec2[0].c_str());
node.end = atoi(str_vec2[1].c_str());
continue;
}
float fval = 1.0;
#ifndef BINARY_FEATURE
if (general_feature && fi.find(":") != string::npos) {
vector<string> str_vec;
split(fi, str_vec, ":");
fi = str_vec[0];
fval = atof(str_vec[1].c_str());
}
#endif
// hash
if (hash_feature) {
new_pid = false;
pid = hash(fi) % n_pred;
}
else if (pred_map.find(fi) == pred_map.end()) {
new_pid = true;
pid = pred_vec.size();
pred_map[fi] = pid;
pred_vec.push_back(fi);
}
else {
new_pid = false;
pid = pred_map[fi];
}
feature_t feature;
feature.pid = pid;
#ifndef BINARY_FEATURE
feature.fval = fval;
#endif
cont.push_back(feature);
// support_feature인 경우 params 업데이트
if (support_feature) {
if (new_pid) {
vector<pair<int, int> > param;
int fid = n_theta;
n_theta++;
param.push_back(make_pair(oid, fid));
params.push_back(param);
} else {
vector<pair<int, int> >& param = params[pid];
for (i=0; i < param.size(); i++) {
if (param[i].first == oid) {
break;
}
}
if (i == param.size()) {
int fid = n_theta;
n_theta++;
param.push_back(make_pair(oid, fid));
// sort
sort(param.begin(), param.end());
}
}
}
}
node.context = cont;
sent.push_back(node);
if (owps_format) {
train_data.push_back(sent);
sent.clear();
// comment 처리
if (use_comment) {
train_data_comment.push_back(comment);
comment = "";
}
if (train_num > 0 && train_num <= train_data.size()) {
++count;
break;
}
}
++count;
if (count % 10000 == 0) {
cerr << ".";
if (count % 100000 == 0) cerr << " ";
if (count % 500000 == 0) cerr << "\t" << count << " " << train_data.size() << endl;
}
}
}
if (!sent.empty()) {
train_data.push_back(sent);
// comment 처리
if (use_comment) {
train_data_comment.push_back(comment);
comment = "";
}
}
n_event += count;
if (support_feature && !incremental) {
// fid sorting
int fid = 0;
for (i = 0; i < params.size(); ++i) {
vector<pair<int, int> >& param = params[i];
for (j = 0; j < param.size(); ++j) {
param[j].second = fid;
fid++;
}
}
}
int old_n_outcome = n_outcome;
n_outcome = outcome_vec.size();
// hash
if (hash_feature) {
// support only all feature
n_theta = n_pred * n_outcome;
if (theta != NULL) delete[] theta;
// alloc memory
cerr << endl << "theta allocated: " << n_theta << " ... ";
theta = new float[n_theta];
cerr << "Done." << endl;
// init
for (int i=0; i < n_theta; i++) {
theta[i] = 0.0;
}
return;
}
n_pred = pred_vec.size();
// not incremental mode
if (!incremental) {
if (!support_feature) {
n_theta = pred_vec.size() * n_outcome;
}
if (theta != NULL) delete[] theta;
// alloc memory
cerr << endl << "theta allocated: " << n_theta << " ... ";
theta = new float[n_theta];
cerr << "Done." << endl;
// init
for (i=0; i < n_theta; i++) {
theta[i] = 0.0;
}
} else {
// incremental인 경우
if (support_feature) {
// 새로 outcome이 추가된 경우 --> add_edge에서 처리
// alloc memory
cerr << endl << "theta allocated: " << n_theta << " ... ";
float *new_theta = new float[n_theta];
cerr << "Done." << endl;
// copy previous data & fid sorting
for (i=0; i < n_theta; i++) {
new_theta[i] = 0;
}
int fid = 0;
for (i = 0; i < old_pred_vec_size; ++i) {
vector<pair<int, int> >& param = params[i];
for (j = 0; j < param.size(); ++j) {
int outcome = param[j].first;
int old_fid = param[j].second;
param[j].second = fid;
// copy previous data
if (old_fid < old_n_theta) {
new_theta[fid] = theta[old_fid];
} else {
//cerr << " new fid:" << old_fid;
}
fid++;
}
}
if (theta != NULL) delete[] theta;
theta = new_theta;
} else {
// all_feature인 경우
int new_n_theta = pred_vec.size() * n_outcome;
// alloc memory
cerr << endl << "theta allocated: " << new_n_theta << " ... ";
float *new_theta = new float[new_n_theta];
cerr << "Done." << endl;
// copy previous data
if (n_outcome == old_n_outcome) {
for (i=0; i < n_theta; i++) {
new_theta[i] = theta[i];
}
for (i=n_theta; i < new_n_theta; i++) {
new_theta[i] = 0;
}
} else {
// 새로 outcome이 추가된 경우 --> add_edge에서 처리
for (i=0; i < new_n_theta; i++) {
new_theta[i] = 0;
}
for (i=0; i < old_pred_vec_size; i++) {
int old_fid = i * old_n_outcome;
int fid = i * n_outcome;
for (j = 0; j < old_n_outcome; j++) {
new_theta[fid + j] = theta[old_fid + j];
}
}
}
n_theta = new_n_theta;
if (theta != NULL) delete[] theta;
theta = new_theta;
}
}
}
void SSVM::load_test_event(const string file) {
string line, comment;
int count = 0;
ifstream f(file.c_str());
if (!f) {
cerr << "Can not open data file to read: " << file << endl;
exit(1);
}
sent_t sent;
context_t cont;
test_data.clear();
while (getline(f, line)) {
// remove newline for windows format file
string find_str = "\r";
string::size_type find_pos = line.find(find_str);
if (string::npos != find_pos) {
line.replace(find_pos, find_str.size(), "");
}
if (line.empty()) {
if (!owps_format) {
test_data.push_back(sent);
sent.clear();
}
// comment 처리
if (use_comment) {
test_data_comment.push_back(comment);
comment = "";
}
} else if (line[0] == '#') {
// comment 처리 안함
if (use_comment) {
if (comment == "") {
comment += line;
} else {
comment += "\n";
comment += line;
}
}
} else {
/// Tokenizer
vector<string> tokens;
tokenize(line, tokens, " \t");
vector<string>::iterator it = tokens.begin();
int oid;
if (outcome_map.find(*it) == outcome_map.end()) {
//cerr << *it << " ";
oid = outcome_vec.size();
//exit(1);
} else {
oid = outcome_map[*it];
if (outcome_vec[oid] != *it) {
cerr << "oid=" << oid << " : " << outcome_vec[oid] << " != " << *it << endl;
}
}
node_t node;
node.outcome = oid;
node.start = sent.size();
node.end = sent.size();
++it;
cont.clear();
for (; it != tokens.end();) {
string fi(it->c_str()); ++it;
// qid:n.m 처리
if (fi.find("qid:") != string::npos) {
vector<string> str_vec, str_vec2;
split(fi, str_vec, ":");
split(str_vec[1], str_vec2, ".");
node.start = atoi(str_vec2[0].c_str());
node.end = atoi(str_vec2[1].c_str());
continue;
}
feature_t feature;
#ifndef BINARY_FEATURE
feature.fval = 1.0;
if (general_feature && fi.find(":") != string::npos) {
vector<string> str_vec;
split(fi, str_vec, ":");
fi = str_vec[0];
feature.fval = atof(str_vec[1].c_str());
}
#endif
// hash
if (hash_feature) {
feature.pid = hash(fi) % n_pred;
cont.push_back(feature);
}
else if (pred_map.find(fi) != pred_map.end()) {
feature.pid = pred_map[fi];
cont.push_back(feature);
}
}
node.context = cont;
sent.push_back(node);
if (owps_format) {
test_data.push_back(sent);
sent.clear();
// comment 처리
if (use_comment) {
test_data_comment.push_back(comment);
comment = "";
}
}
++count;
if (count % 10000 == 0) {
cerr << ".";
if (count % 100000 == 0)
cerr << " ";
if (count % 500000 == 0)
cerr << "\t" << count << endl;
}
}
}
if (!sent.empty()) {
test_data.push_back(sent);
// comment 처리
if (use_comment) {
test_data_comment.push_back(comment);
comment = "";
}
}
n_test_event = count;
}
// log scale 사용
int SSVM::predict(ostream& f) {
vector<sent_t>::iterator it = test_data.begin();
int correct = 0;
int total = 0;
int sent_i = 0;
// 처음에 반드시 실행
make_M_matrix();
// 파일에 쓰기
f << "# output answer" << endl;