-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseutil.c
181 lines (127 loc) · 5.33 KB
/
parseutil.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
#include "util.h"
#include "dependency.h"
#include "parseutil.h"
#include <stdbool.h>
#ifdef __GNUC__
#include <signal.h>
#include <sys/types.h>
#endif
static bool keepRunning = true;
void intHandler(int dummy) {
keepRunning = false;
}
#define STOP_ON_CONVERGE true
#define MAX_IDLE_ITER 3
#define MIN_DELTA 0.001
/**
* ai-parse.c file for actual storage allocation for those two variables
*/
extern const char *epattern;
extern enum EmbeddingTranformation etransform;
extern enum Kernel kernel;
extern const char *modelname;
extern int polynomial_degree;
extern float bias;
extern float rbf_lambda;
void* optimize(int max_numit, int max_rec, const char* path, const char* train_sections_str, const char* dev_sections_str, int embedding_dimension) {
DArray *train_sections = parse_range(train_sections_str);
DArray *dev_sections = parse_range(dev_sections_str);
signal(SIGINT, intHandler);
log_info("Development sections to be used in %s: %s", path, join_range(dev_sections));
CoNLLCorpus dev = create_CoNLLCorpus(path, dev_sections, embedding_dimension, NULL);
log_info("Training sections to be used in %s: %s", path, join_range(train_sections));
CoNLLCorpus train = create_CoNLLCorpus(path, train_sections, embedding_dimension, NULL);
log_info("Reading training corpus");
read_corpus(train, false);
log_info("Reading dev corpus");
read_corpus(dev, false);
float *numit_dev_avg = (float*) malloc(sizeof (float)* max_numit);
float *numit_train_avg = (float*) malloc(sizeof (float)*max_numit);
check(numit_dev_avg != NULL, "Memory allocation failed for numit_dev_avg");
check(numit_train_avg != NULL, "Memory allocation failed for numit_train_avg");
PerceptronModel model = NULL;
KernelPerceptron kmodel = NULL;
if (kernel == KLINEAR){
log_info("Creating a averaged perceptron model");
model = create_PerceptronModel(train->transformed_embedding_length, NULL);
}
else if (kernel == KPOLYNOMIAL)
kmodel = create_PolynomialKernelPerceptron(polynomial_degree, bias);
else
kmodel = create_RBFKernelPerceptron(rbf_lambda);
int numit;
int best_iter = -1;
float best_score = 0.0;
for (numit = 1; numit <= max_numit && keepRunning; numit++) {
log_info("BEGIN-TRAIN: Iteration %d", numit);
if (kernel == KLINEAR)
train_once_PerceptronModel(model, train, max_rec);
else
train_once_KernelPerceptronModel(kmodel, train, max_rec);
log_info("END-TRAIN: Iteration %d", numit);
ParserTestMetric dev_metric;
log_info("BEGIN-TEST: Iteration %d", numit);
if (kernel == KLINEAR)
dev_metric = test_KernelPerceptronModel(model, dev, true, NULL, NULL);
else
dev_metric = test_KernelPerceptronModel(kmodel, dev, true, NULL, NULL);
log_info("END-TEST: Iteration %d", numit);
log_info("\nnumit=%d", numit);
printParserTestMetric(dev_metric);
double dev_acc = (dev_metric->without_punc->true_prediction * 1.) / dev_metric->without_punc->total_prediction;
numit_dev_avg[numit - 1] = dev_acc;
numit_train_avg[numit - 1] = 0.0;
freeParserTestMetric(dev_metric);
if (best_score < dev_acc) {
if (best_score + MIN_DELTA > dev_acc)
log_warn("Improvement is less than %f", MIN_DELTA);
best_score = dev_acc;
best_iter = numit;
if (kernel == KLINEAR)
mark_best_PerceptronModel(model, numit);
else
mark_best_KernelPerceptronModel(kmodel, numit);
}
if (numit - best_iter > MAX_IDLE_ITER && STOP_ON_CONVERGE) {
log_info("No improvement in last %d iterations", MAX_IDLE_ITER);
keepRunning = false;
}
}
log_info("Iteration\tAccuracy(dev)\tAccuracy(train)");
for (int i = 0; i < numit - 1; i++) {
log_info("%d\t\t%f\t%f%s", i + 1, numit_dev_avg[i], numit_train_avg[i], (i + 1 == best_iter) ? " (*)" : "");
}
//free_CoNLLCorpus(dev, true);
//free_CoNLLCorpus(train, true);
if (kernel == KLINEAR)
return (void*) model;
else
return (void*) kmodel;
error:
log_err("Memory allocation error");
exit(1);
}
void parseall(const void *model, const char* path, const char* test_sections_str, int embedding_dimension) {
DArray *test_sections = parse_range(test_sections_str);
signal(SIGINT, intHandler);
log_info("Test sections to be used in %s: %s", path, join_range(test_sections));
CoNLLCorpus test = create_CoNLLCorpus(path, test_sections, embedding_dimension, NULL);
log_info("Reading test corpus");
read_corpus(test, false);
char* output_filename = (char*) malloc(sizeof (char) * (strlen(modelname) + 13));
check_mem(output_filename);
sprintf(output_filename, "%s.gold.conll", modelname);
FILE *gold_fp = fopen(output_filename, "w");
sprintf(output_filename, "%s.model.conll", modelname);
FILE *model_fp = fopen(output_filename, "w");
ParserTestMetric test_metric = test_KernelPerceptronModel(model, test, true, gold_fp, model_fp);
fclose(gold_fp);
fclose(model_fp);
printParserTestMetric(test_metric);
freeParserTestMetric(test_metric);
free(output_filename);
return;
error:
log_err("Memory allocation error");
exit(1);
}