Skip to content

Commit

Permalink
Fix parser.n not saving model bug when development file is not configed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneplus committed May 21, 2015
1 parent bdeca2c commit 84349f8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 36 deletions.
91 changes: 55 additions & 36 deletions src/parser.n/parser_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,41 @@ void NeuralNetworkParserFrontend::generate_training_samples_one_batch(
}
}

void NeuralNetworkParserFrontend::evaluate_one_instance(const Instance& data,
const std::vector<int>& heads, const std::vector<std::string>& deprels,
size_t& corr_heads, size_t& corr_deprels, size_t& nr_tokens) {
size_t L = heads.size();
for (size_t i = 1; i < L; ++ i) { // ignore dummy root
if (is_unicode_punctuation(data.raw_forms[i])) {
continue;
}
++ nr_tokens;
if (heads[i] == data.heads[i]) {
++ corr_heads;
if (deprels[i] == data.deprels[i]) { ++ corr_deprels; }
}
}
}

void NeuralNetworkParserFrontend::evaluate(double& uas, double& las) {
INFO_LOG("eval: start evaluating ...");
classifier.precomputing();

std::vector<int> heads;
std::vector<std::string> deprels;
size_t corr_heads = 0, corr_deprels = 0, nr_tokens = 0;

for (size_t i = 0; i < devel_dat.size(); ++ i) {
Instance* data = devel_dat[i];
predict((*data), heads, deprels);
evaluate_one_instance((*data), heads, deprels, corr_heads,
corr_deprels, nr_tokens);
}

uas = (double)corr_heads/nr_tokens;
las = (double)corr_deprels/nr_tokens;
}

void NeuralNetworkParserFrontend::train(void) {
if (!read_training_data()) { return; }
build_alphabet();
Expand Down Expand Up @@ -414,32 +449,9 @@ void NeuralNetworkParserFrontend::train(void) {
classifier.take_ada_gradient_step();

if (devel_dat.size() > 0 && (iter+1) % learn_opt->evaluation_stops == 0) {
INFO_LOG("eval: start evaluating ...");
classifier.precomputing();

std::vector<int> heads;
std::vector<std::string> deprels;
size_t corr_heads = 0, corr_deprels = 0, nr_tokens = 0;

t.restart();
for (size_t i = 0; i < devel_dat.size(); ++ i) {
Instance* data = devel_dat[i];
predict((*data), heads, deprels);

size_t L = heads.size();
for (size_t i = 1; i < L; ++ i) { // ignore dummy root
if (is_unicode_punctuation(data->raw_forms[i])) {
continue;
}
++ nr_tokens;
if (heads[i] == data->heads[i]) {
++ corr_heads;
if (deprels[i] == data->deprels[i]) { ++ corr_deprels; }
}
}
}
double uas = (double)corr_heads/nr_tokens;
double las = (double)corr_deprels/nr_tokens;
double uas, las;
evaluate(uas, las);
INFO_LOG("eval: evaluating done. UAS=%lf LAS=%lf (%lf)", uas, las, t.elapsed());

if (best_uas < uas && learn_opt->save_intermediate) {
Expand All @@ -449,6 +461,22 @@ void NeuralNetworkParserFrontend::train(void) {
}
}
}

if (devel_dat.size() > 0) {
timer t;
double uas, las;
evaluate(uas, las);
INFO_LOG("eval: evaluating done. UAS=%lf LAS=%lf (%lf)", uas, las, t.elapsed());

if (best_uas < uas && learn_opt->save_intermediate) {
best_uas = uas;
save(learn_opt->model_file);
INFO_LOG("report: model saved to %s", learn_opt->model_file.c_str());
}
} else {
save(learn_opt->model_file);
INFO_LOG("report: model saved to %s", learn_opt->model_file.c_str());
}
}

void NeuralNetworkParserFrontend::test(void) {
Expand Down Expand Up @@ -481,17 +509,8 @@ void NeuralNetworkParserFrontend::test(void) {
writer.write(*inst, heads, deprels);

if (test_opt->evaluate) {
size_t L = heads.size();
for (size_t i = 1; i < L; ++ i) { // ignore dummy root
if (is_unicode_punctuation(inst->raw_forms[i])) {
continue;
}
++ nr_tokens;
if (heads[i] == inst->heads[i]) {
++ corr_heads;
if (deprels[i] == inst->deprels[i]) { ++ corr_deprels; }
}
}
evaluate_one_instance((*inst), heads, deprels, corr_heads,
corr_deprels, nr_tokens);
}
delete inst;
}
Expand Down
7 changes: 7 additions & 0 deletions src/parser.n/parser_frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class NeuralNetworkParserFrontend: public NeuralNetworkParser {
void check_dataset(const std::vector<Instance*>& dataset);
void build_alphabet(void);
void build_cluster(void);
void evaluate_one_instance(const Instance& data,
const std::vector<int>& heads,
const std::vector<std::string>& deprels,
size_t& corr_heads,
size_t& corr_deprels,
size_t& nr_tokens);
void evaluate(double& uas, double& las);
void collect_precomputed_features(void);
void initialize_classifier(void);
void generate_training_samples_one_batch(
Expand Down

0 comments on commit 84349f8

Please sign in to comment.