From 99206ca74140a121847349a442bae8d8feea99bf Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 7 Jun 2022 11:09:52 +0000 Subject: [PATCH 01/13] added some logs --- .../src/NNDisParser-synmlp-do/NNDisParser.cpp | 90 ++++++++++++++++--- 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index c6a4494..361029d 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -154,7 +154,7 @@ void DisParser::train(const string &trainFile, const string &devFile, getDepFeats(trainInsts, m_options.conllFolder + path_separator + "train.conll.predict"); getDepFeats(devInsts, m_options.conllFolder + path_separator + "dev.conll.predict"); getDepFeats(testInsts, m_options.conllFolder + path_separator + "test.conll.predict"); - + cout << "DEP FEATS FINISHED \n"; int word_count = 0, max_size; max_size = trainInsts.size(); for (int idx = 0; idx < max_size; idx++) { @@ -170,19 +170,26 @@ void DisParser::train(const string &trainFile, const string &devFile, } extern_nodes.resize(word_count * 10); node_count = 0; - + cout << "getSynFeats train STARTING \n"; string syn = "conll.dump.results"; getSynFeats(trainInsts, m_options.dumpFolder + path_separator + "train." + syn); + cout << "getSynFeats dev STARTING \n"; getSynFeats(devInsts, m_options.dumpFolder + path_separator + "dev." + syn); + cout << "getSynFeats test STARTING \n"; getSynFeats(testInsts, m_options.dumpFolder + path_separator + "test." + syn); + cout << "getSynFeats FINISHED \n"; vector > otherInsts(m_options.testFiles.size()); for (int idx = 0; idx < m_options.testFiles.size(); idx++) { m_pipe.readInstances(m_options.testFiles[idx], otherInsts[idx], m_options.maxInstance); } + cout << "addTestAlpha STARTING \n"; addTestAlpha(devInsts); + cout << "addTestAlpha STARTING \n"; addTestAlpha(testInsts); + cout << "createAlphabet STARTING \n"; createAlphabet(trainInsts); + cout << "getGoldActions STARTING \n"; getGoldActions(trainInsts); if(m_options.wordEmbFile == "") @@ -191,7 +198,7 @@ void DisParser::train(const string &trainFile, const string &devFile, m_driver._modelparams.edu_params.word_table.initial(&m_driver._hyperparams.wordAlpha, m_options.wordEmbFile, m_options.wordFineTune); m_driver._hyperparams.wordDim = m_driver._modelparams.edu_params.word_table.nDim; - + cout << "X1 STARTING \n"; m_driver._modelparams.edu_params.tag_table.initial(&m_driver._hyperparams.tagAlpha, m_options.tagEmbSize, m_options.tagFineTune); m_driver._hyperparams.tagDim = m_driver._modelparams.edu_params.tag_table.nDim; @@ -203,7 +210,9 @@ void DisParser::train(const string &trainFile, const string &devFile, m_driver._hyperparams.eduConcatDim = m_driver._hyperparams.eduHiddenDim + m_driver._hyperparams.etypeDim; //m_driver._hyperparams.eduConcatDim = m_driver._hyperparams.eduHiddenDim; + cout << "Y1 STARTING \n"; m_driver.initial(); + double bestFmeasure = -1; int inputSize = trainInsts.size(); std::vector indexes; @@ -213,6 +222,8 @@ void DisParser::train(const string &trainFile, const string &devFile, int batchBlock = inputSize / m_options.batchSize; if (inputSize % m_options.batchSize != 0) batchBlock++; + + cout << "Z1 STARTING \n"; vector subInstances; Metric eval; for (int iter = 0; iter < m_options.maxIter; ++iter) { @@ -355,6 +366,7 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { vector vecLines; vector info; int index = 0, sent_size; + int word_count = 0; while (getline(file, line)) { if (line == "") { sent_size = vecLines.size(); @@ -370,15 +382,21 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { Instance &cur_inst = vecInsts[index]; cur_inst.dep_feats.push_back(dep_feat); if (cur_inst.sent_types.size() == cur_inst.dep_feats.size()) { + //cout << "INDEX " << index << " VEC LINES " << vecLines.size() << " VEC INST SIZE: " << cur_inst.dep_feats.size() <<" word_count:" << word_count << endl; + word_count = 0; index++; if (index == vecInsts.size()) break; } vecLines.clear(); } - else + else { + word_count += 1; + //if (index >=10 && index <= 12) cout << line << endl; vecLines.push_back(line); + } } + file.close(); // checking... int inst_size = vecInsts.size(); @@ -391,10 +409,13 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { for (int idy = 0; idy < dep_feat_size; idy++) { word_num += inst.dep_feats[idy].words.size(); } + if (word_num != inst.words.size()) cout << path << " +++ " << idx << " ----- " << word_num << "<>" << inst.words.size() << endl; assert(word_num == inst.words.size()); i = 0, offset = 0; for (int idy = 0; idy < word_num; idy++) { const DepFeat &cur_feat = inst.dep_feats[i]; + if (!(inst.words[idy] == cur_feat.words[idy - offset] &&inst.tags[idy] == cur_feat.tags[idy - offset])) + cout << inst.words[idy] << "==" << cur_feat.words[idy - offset] << " " << inst.tags[idy] << "==" << cur_feat.tags[idy - offset] << endl; assert(inst.words[idy] == cur_feat.words[idy - offset] && inst.tags[idy] == cur_feat.tags[idy - offset]); if (idy - offset + 1 == cur_feat.words.size()) { @@ -422,7 +443,9 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { vector vecLine1, vecLine2, vecLine3, vecLine4, vecLine5; vector vecInfo; int index = 0, num = 0, offset = 0, word_offset; + while (1) { + int count1 = 0, count2=0, count3=0, count4=0, count5=0; vecLine1.clear(); while (1) { if (!my_getline(inf1, strLine)) { @@ -431,6 +454,7 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine1.push_back(strLine); + count1 ++; } vecLine2.clear(); @@ -441,6 +465,7 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine2.push_back(strLine); + count2 ++; } vecLine3.clear(); @@ -451,6 +476,7 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine3.push_back(strLine); + count3 ++; } vecLine4.clear(); @@ -461,6 +487,7 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine4.push_back(strLine); + count4 ++; } vecLine5.clear(); @@ -471,8 +498,8 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine5.push_back(strLine); + count5 ++; } - int vec_size = vecLine1.size(); if (vec_size == 0) { if (index != vecInsts.size()) { @@ -484,20 +511,20 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { vecLine4.size() != vec_size || vecLine5.size() != vec_size) { std::cout << "extern feature input error" << std::endl; } - Instance &cur_inst = vecInsts[index]; const DepFeat &cur_dep_feat = cur_inst.dep_feats[num - offset]; if (num - offset == 0) word_offset = 0; - if (cur_dep_feat.words.size() != vec_size) { continue; } - for (int i = 0; i < vec_size; i++) { vecInfo.clear(); split_bychar(vecLine1[i], vecInfo, ' '); + if (normalize_to_lowerwithdigit(vecInfo[0]).compare(cur_dep_feat.words[i]) != 0) { + cout << vecInfo[0] << " normalized: " << normalize_to_lowerwithdigit(vecInfo[0]) << " <> " << cur_dep_feat.words[i] << endl; + } assert(normalize_to_lowerwithdigit(vecInfo[0]).compare(cur_dep_feat.words[i]) == 0); } int cur_word_size = cur_dep_feat.words.size(), syn_offset; @@ -506,55 +533,90 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { cur_inst.syn_feats[syn_offset].resize(6); split_bychar(vecLine1[i], vecInfo, ' '); - assert(vecInfo.size() == 501); + //assert(vecInfo.size() == 501); extern_nodes[node_count].init(500, -1); cur_inst.syn_feats[syn_offset][0] = &extern_nodes[node_count]; for (int j = 0; j < 500; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } + // assert(vecInfo.size() == 513); // ----------------------- 501 + // extern_nodes[node_count].init(512, -1); + // cur_inst.syn_feats[syn_offset][0] = &extern_nodes[node_count]; + // for (int j = 0; j < 512; j++) { + // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + // } node_count++; split_bychar(vecLine2[i], vecInfo, ' '); - assert(vecInfo.size() == 101); + //vecInfo.size() == 101); extern_nodes[node_count].init(100, -1); cur_inst.syn_feats[syn_offset][1] = &extern_nodes[node_count]; for (int j = 0; j < 100; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } + // assert(vecInfo.size() == 129); // --------------------- 101 + // extern_nodes[node_count].init(128, -1); + // cur_inst.syn_feats[syn_offset][1] = &extern_nodes[node_count]; + // for (int j = 0; j < 128; j++) { + // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + // } node_count++; split_bychar(vecLine3[i], vecInfo, ' '); - assert(vecInfo.size() == 501); + // assert(vecInfo.size() == 501); extern_nodes[node_count].init(500, -1); cur_inst.syn_feats[syn_offset][2] = &extern_nodes[node_count]; for (int j = 0; j < 500; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } + // assert(vecInfo.size() == 513); + // extern_nodes[node_count].init(512, -1); + // cur_inst.syn_feats[syn_offset][2] = &extern_nodes[node_count]; + // for (int j = 0; j < 512; j++) { + // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + // } node_count++; split_bychar(vecLine4[i], vecInfo, ' '); - assert(vecInfo.size() == 101); + //assert(vecInfo.size() == 101); extern_nodes[node_count].init(100, -1); cur_inst.syn_feats[syn_offset][3] = &extern_nodes[node_count]; for (int j = 0; j < 100; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } + // assert(vecInfo.size() == 129); + // extern_nodes[node_count].init(128, -1); + // cur_inst.syn_feats[syn_offset][3] = &extern_nodes[node_count]; + // for (int j = 0; j < 128; j++) { + // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + // } node_count++; split_bychar(vecLine5[i], vecInfo, ' '); - assert(vecInfo.size() == 801); + //assert(vecInfo.size() == 801); extern_nodes[node_count].init(400, -1); cur_inst.syn_feats[syn_offset][4] = &extern_nodes[node_count]; for (int j = 0; j < 400; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } node_count++; - extern_nodes[node_count].init(400, -1); cur_inst.syn_feats[syn_offset][5] = &extern_nodes[node_count]; for (int j = 0; j < 400; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 401].c_str()); } + // assert(vecInfo.size() == 1025); // ---------------------------- 801 + // extern_nodes[node_count].init(512, -1); + // cur_inst.syn_feats[syn_offset][4] = &extern_nodes[node_count]; + // for (int j = 0; j < 512; j++) { + // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + // } + // node_count++; + // extern_nodes[node_count].init(512, -1); + // cur_inst.syn_feats[syn_offset][5] = &extern_nodes[node_count]; + // for (int j = 0; j < 512; j++) { + // extern_nodes[node_count].val[j] = atof(vecInfo[j + 513].c_str()); + // } node_count++; } From 10abfe5266ea71b61179711fd9c81bef538f3636 Mon Sep 17 00:00:00 2001 From: Mat Sepahi Date: Tue, 30 Aug 2022 23:14:03 +0430 Subject: [PATCH 02/13] changes made to successfully load model and initialize driver - but the output seems incorrect: same relations --- .../src/NNDisParser-synmlp-do/NNDisParser.cpp | 98 ++++++++++++++++--- .../NNDisParser-synmlp-do/model/EDUNodes.h | 2 +- .../NNDisParser-synmlp-do/model/GreedyGraph.h | 2 +- .../NNDisParser-synmlp-do/model/ModelParams.h | 6 +- NNDisParser/src/basic/Instance.h | 3 +- 5 files changed, 90 insertions(+), 21 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index c212c5c..a6fb7c8 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -198,7 +198,6 @@ void DisParser::train(const string &trainFile, const string &devFile, m_driver._modelparams.edu_params.word_table.initial(&m_driver._hyperparams.wordAlpha, m_options.wordEmbFile, m_options.wordFineTune); m_driver._hyperparams.wordDim = m_driver._modelparams.edu_params.word_table.nDim; - cout << "X1 STARTING \n"; m_driver._modelparams.edu_params.tag_table.initial(&m_driver._hyperparams.tagAlpha, m_options.tagEmbSize, m_options.tagFineTune); m_driver._hyperparams.tagDim = m_driver._modelparams.edu_params.tag_table.nDim; @@ -210,7 +209,6 @@ void DisParser::train(const string &trainFile, const string &devFile, m_driver._hyperparams.eduConcatDim = m_driver._hyperparams.eduHiddenDim + m_driver._hyperparams.etypeDim; //m_driver._hyperparams.eduConcatDim = m_driver._hyperparams.eduHiddenDim; - cout << "Y1 STARTING \n"; m_driver.initial(); double bestFmeasure = -1; @@ -285,6 +283,36 @@ void DisParser::train(const string &trainFile, const string &devFile, Metric dev_span, dev_nuclear, dev_relation, dev_full; Metric test_span, test_nuclear, test_relation, test_full; + if (true){ + Metric train_span, train_nuclear, train_relation, train_full; + auto t_start_train = std::chrono::high_resolution_clock::now(); + cout << "Train start." << std::endl; + train_span.reset(); + train_nuclear.reset(); + train_relation.reset(); + predict(trainInsts, decodeInstResults); + int trainNum = trainInsts.size(); + for (int idx = 0; idx < trainNum; idx++) { + trainInsts[idx].evaluate(decodeInstResults[idx], train_span, train_nuclear, train_relation, train_full); + } + auto t_end_train = std::chrono::high_resolution_clock::now(); + cout << "Train Performance measured. Total time taken is: " << std::chrono::duration(t_end_train - t_start_train).count() << std::endl; + cout << "train:" << std::endl; + cout << "S: "; + train_span.print(); + cout << "N: "; + train_nuclear.print(); + cout << "R: "; + train_relation.print(); + cout << "F: "; + train_full.print(); + if (!m_options.outBest.empty() && train_full.getAccuracy() > bestFmeasure) { + m_pipe.outputAllInstances(trainFile + m_options.outBest + to_string(iter), decodeInstResults); + } + } + + + if (devNum > 0) { auto t_start_dev = std::chrono::high_resolution_clock::now(); @@ -362,12 +390,13 @@ void DisParser::test(const string &testFile, const string &outputFile, const str m_options.showOptions(); m_driver._hyperparams.setRequared(m_options); + loadModelFile(modelFile); + vector testInsts; m_pipe.readInstances(testFile, testInsts, m_options.maxInstance); - getDepFeats(testInsts, m_options.conllFolder + path_separator + "test.conll.predict"); + getDepFeats(testInsts, testFile + ".conll"); int word_count = 0, max_size; - max_size = testInsts.size(); for (int idx = 0; idx < max_size; idx++) { word_count += testInsts[idx].words.size(); @@ -375,19 +404,42 @@ void DisParser::test(const string &testFile, const string &outputFile, const str extern_nodes.resize(word_count * 10); node_count = 0; - string syn = "conll.dump.results"; - getSynFeats(testInsts, m_options.dumpFolder + path_separator + "test." + syn); + getSynFeats(testInsts, testFile + ".dump"); + + addTestAlpha(testInsts); + //createAlphabet(trainInsts); + //getGoldActions(trainInsts); + + //if(m_options.wordEmbFile == "") { + // m_driver._modelparams.edu_params.word_table.initial(&m_driver._hyperparams.wordAlpha, m_options.wordEmbSize, m_options.wordFineTune); + //} + //else + // m_driver._modelparams.edu_params.word_table.initial(&m_driver._hyperparams.wordAlpha, m_options.wordEmbFile, m_options.wordFineTune); + m_driver._hyperparams.wordDim = m_driver._modelparams.edu_params.word_table.nDim; + m_driver._modelparams.edu_params.tag_table.initial(&m_driver._hyperparams.tagAlpha, m_options.tagEmbSize, m_options.tagFineTune); + m_driver._hyperparams.tagDim = m_driver._modelparams.edu_params.tag_table.nDim; + + m_driver._hyperparams.wordConcatDim = m_driver._hyperparams.wordDim + m_driver._hyperparams.tagDim; + //m_driver._modelparams.etype_table.initial(&m_driver._hyperparams.etypeAlpha, m_options.etypeEmbSize, m_options.etypeFineTune); + m_driver._hyperparams.etypeDim = m_driver._modelparams.etype_table.nDim; + m_driver._hyperparams.eduConcatDim = m_driver._hyperparams.eduHiddenDim + m_driver._hyperparams.etypeDim; + //m_driver._hyperparams.eduConcatDim = m_driver._hyperparams.eduHiddenDim; m_driver._modelparams.edu_params.word_table.elems = &m_driver._hyperparams.wordAlpha; m_driver._modelparams.edu_params.tag_table.elems = &m_driver._hyperparams.tagAlpha; m_driver._modelparams.etype_table.elems = &m_driver._hyperparams.etypeAlpha; m_driver._modelparams.scored_action_table.elems = &m_driver._hyperparams.actionAlpha; + + //m_driver._hyperparams.actionAlpha.initial(m_driver._hyperparams.action_stat, 0); + m_driver._hyperparams.actionNum = m_driver._hyperparams.actionAlpha.size(); + //m_driver._hyperparams.etypeAlpha.initial(m_driver._hyperparams.etype_stat, 0); + + m_driver.initial(); - loadModelFile(modelFile); - getDepFeats(testInsts, m_options.conllFolder + path_separator + "test.conll.predict"); vector decodeInstResults; int testNum = testInsts.size(); + Metric test_span, test_nuclear, test_relation, test_full; if (testNum > 0) { auto t_start_test = std::chrono::high_resolution_clock::now(); @@ -399,7 +451,9 @@ void DisParser::test(const string &testFile, const string &outputFile, const str test_nuclear.reset(); test_relation.reset(); test_full.reset(); + predict(testInsts, decodeInstResults); + for (int idx = 0; idx < testInsts.size(); idx++) { testInsts[idx].evaluate(decodeInstResults[idx], test_span, test_nuclear, test_relation, test_full); } @@ -415,9 +469,7 @@ void DisParser::test(const string &testFile, const string &outputFile, const str cout << "F: "; test_full.print(); - if (!m_options.outBest.empty()) { - m_pipe.outputAllInstances(testFile + m_options.outBest + ".test", decodeInstResults); - } + m_pipe.outputAllInstances(outputFile, decodeInstResults); } } @@ -470,7 +522,17 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { for (int idy = 0; idy < dep_feat_size; idy++) { word_num += inst.dep_feats[idy].words.size(); } - if (word_num != inst.words.size()) cout << path << " +++ " << idx << " ----- " << word_num << "<>" << inst.words.size() << endl; + if (word_num != inst.words.size()) { + cout << path << " +++ " << idx << " ----- " << word_num << "<>" << inst.words.size() << endl; + for (int i = 0; i < inst.words.size(); i ++) { + cout << inst.words[i] << " "; + } + cout <<"---------------" < &vecInsts, const string &folder) { } if (vecLine2.size() != vec_size || vecLine3.size() != vec_size || vecLine4.size() != vec_size || vecLine5.size() != vec_size) { + cout << vec_size << "-" << vecLine2.size() << "-" << vecLine3.size() << "-" << + vecLine4.size() << "-" << vecLine5.size() << endl; std::cout << "extern feature input error" << std::endl; } Instance &cur_inst = vecInsts[index]; @@ -584,7 +648,9 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { split_bychar(vecLine1[i], vecInfo, ' '); if (normalize_to_lowerwithdigit(vecInfo[0]).compare(cur_dep_feat.words[i]) != 0) { - cout << vecInfo[0] << " normalized: " << normalize_to_lowerwithdigit(vecInfo[0]) << " <> " << cur_dep_feat.words[i] << endl; + cout << vecInfo[0] << " (" << i << ") normalized: " << normalize_to_lowerwithdigit(vecInfo[0]) << " <> " << cur_dep_feat.words[i] << endl; + for (int k = 0; k < vec_size; k ++) cout << cur_dep_feat.words[k] << " "; cout << endl; + } assert(normalize_to_lowerwithdigit(vecInfo[0]).compare(cur_dep_feat.words[i]) == 0); } @@ -700,6 +766,8 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { void DisParser::writeModelFile(const string &outputModelFile) { + cout << "writeModelFile:" << outputModelFile; + cout << "outputModelFile: " << outputModelFile << endl; ofstream outf(outputModelFile.c_str()); m_driver._hyperparams.write(outf); m_driver._modelparams.saveModel(outf); @@ -709,7 +777,7 @@ void DisParser::writeModelFile(const string &outputModelFile) { void DisParser::loadModelFile(const string &inputModelFile) { ifstream inf(inputModelFile.c_str()); m_driver._hyperparams.read(inf); - m_driver._modelparams.loadModel(inf); + m_driver._modelparams.loadModel(inf, m_driver._hyperparams); inf.close(); } @@ -731,7 +799,7 @@ void DisParser::predict(const vector &inputs, vector &outputs int main(int argc, char* argv[]) { - std::string trainFile = "", devFile = "", testFile = "", modelFile = ""; + std::string trainFile = "", devFile = "", testFile = "", modelFile = "./model.bin"; std::string optionFile = ""; std::string outputFile = ""; bool bTrain = false; diff --git a/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h b/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h index 8899191..80ef735 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h +++ b/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h @@ -166,4 +166,4 @@ class EDUNodes{ } }; -#endif \ No newline at end of file +#endif diff --git a/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h b/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h index 540af82..aa456cf 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h +++ b/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h @@ -138,4 +138,4 @@ class GreedyGraphBuilder { } }; -#endif /* GreedyGraph_H_ */ \ No newline at end of file +#endif /* GreedyGraph_H_ */ diff --git a/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h b/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h index dfca1dd..0f7ee11 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h +++ b/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h @@ -66,13 +66,13 @@ class ModelParams { scored_action_table.save(os); } - void loadModel(std::ifstream &is) { + void loadModel(std::ifstream &is, HyperParams &opts) { edu_params.load(is); syn_params.load(is); - etype_table.load(is, etype_table.elems); + etype_table.load(is, &opts.etypeAlpha); edu_lstm_left_layer1_params.load(is); edu_lstm_right_layer1_params.load(is); - scored_action_table.load(is, scored_action_table.elems); + scored_action_table.load(is, &opts.actionAlpha); } }; diff --git a/NNDisParser/src/basic/Instance.h b/NNDisParser/src/basic/Instance.h index d9f825d..d867455 100644 --- a/NNDisParser/src/basic/Instance.h +++ b/NNDisParser/src/basic/Instance.h @@ -286,8 +286,9 @@ class Instance { EDU &edu = edus[idx]; assert(edu.start_index <= edu.end_index); assert(edu.start_index >= 0 && edu.end_index < total_text_size); - if (idx < edu_size - 1) + if (idx != edu_size - 1) { assert(edu.end_index + 1 == edus[idx + 1].start_index); + } for (int idy = edu.start_index; idy <= edu.end_index; idy++) { if(total_tags[idy] != nullkey){ edu.words.push_back(normalize_to_lowerwithdigit(total_text[idy])); From 3126fb604772bd0016788f7415e994cfe8362a2f Mon Sep 17 00:00:00 2001 From: Mat Sipahi Date: Tue, 30 Aug 2022 23:58:34 +0430 Subject: [PATCH 03/13] build file changes --- NNDisParser/build/build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NNDisParser/build/build.sh b/NNDisParser/build/build.sh index 1973f71..b0853e3 100644 --- a/NNDisParser/build/build.sh +++ b/NNDisParser/build/build.sh @@ -1,2 +1,2 @@ -path=/home/yunan/workspace/discourseParser/ -cmake .. -DEIGEN3_DIR=${path}eigen -DN3L_DIR=${path}N3LDG -DMKL=TRUE +path=/home/ubuntu/NNDisParser-cp/ +cmake .. -DEIGEN3_DIR=${path}eigen -DN3L_DIR=${path}N3LDG -DMKL=TRUE -DCMAKE_BUILD_TYPE=Debug From 36272422c421562a0bfa3fa6cbc4ecf8dc289646 Mon Sep 17 00:00:00 2001 From: Mat Sipahi Date: Wed, 31 Aug 2022 00:01:33 +0430 Subject: [PATCH 04/13] redundant changes reverted --- NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h | 2 +- NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h b/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h index 80ef735..8899191 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h +++ b/NNDisParser/src/NNDisParser-synmlp-do/model/EDUNodes.h @@ -166,4 +166,4 @@ class EDUNodes{ } }; -#endif +#endif \ No newline at end of file diff --git a/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h b/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h index aa456cf..540af82 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h +++ b/NNDisParser/src/NNDisParser-synmlp-do/model/GreedyGraph.h @@ -138,4 +138,4 @@ class GreedyGraphBuilder { } }; -#endif /* GreedyGraph_H_ */ +#endif /* GreedyGraph_H_ */ \ No newline at end of file From f3fcf53d5409d69161753aff131edc17500054fb Mon Sep 17 00:00:00 2001 From: Mat Sipahi Date: Wed, 31 Aug 2022 00:02:44 +0430 Subject: [PATCH 05/13] redundant changes again --- .../src/NNDisParser-synmlp-do/NNDisParser.cpp | 177 ++++++------------ 1 file changed, 57 insertions(+), 120 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index a6fb7c8..61865d5 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -154,7 +154,7 @@ void DisParser::train(const string &trainFile, const string &devFile, getDepFeats(trainInsts, m_options.conllFolder + path_separator + "train.conll.predict"); getDepFeats(devInsts, m_options.conllFolder + path_separator + "dev.conll.predict"); getDepFeats(testInsts, m_options.conllFolder + path_separator + "test.conll.predict"); - cout << "DEP FEATS FINISHED \n"; + int word_count = 0, max_size; max_size = trainInsts.size(); for (int idx = 0; idx < max_size; idx++) { @@ -170,26 +170,19 @@ void DisParser::train(const string &trainFile, const string &devFile, } extern_nodes.resize(word_count * 10); node_count = 0; - cout << "getSynFeats train STARTING \n"; + string syn = "conll.dump.results"; - getSynFeats(trainInsts, m_options.dumpFolder + path_separator + "train." + syn); - cout << "getSynFeats dev STARTING \n"; getSynFeats(devInsts, m_options.dumpFolder + path_separator + "dev." + syn); - cout << "getSynFeats test STARTING \n"; getSynFeats(testInsts, m_options.dumpFolder + path_separator + "test." + syn); - cout << "getSynFeats FINISHED \n"; vector > otherInsts(m_options.testFiles.size()); for (int idx = 0; idx < m_options.testFiles.size(); idx++) { m_pipe.readInstances(m_options.testFiles[idx], otherInsts[idx], m_options.maxInstance); } - cout << "addTestAlpha STARTING \n"; + addTestAlpha(devInsts); - cout << "addTestAlpha STARTING \n"; addTestAlpha(testInsts); - cout << "createAlphabet STARTING \n"; createAlphabet(trainInsts); - cout << "getGoldActions STARTING \n"; getGoldActions(trainInsts); if(m_options.wordEmbFile == "") @@ -210,7 +203,6 @@ void DisParser::train(const string &trainFile, const string &devFile, //m_driver._hyperparams.eduConcatDim = m_driver._hyperparams.eduHiddenDim; m_driver.initial(); - double bestFmeasure = -1; int inputSize = trainInsts.size(); std::vector indexes; @@ -221,7 +213,6 @@ void DisParser::train(const string &trainFile, const string &devFile, if (inputSize % m_options.batchSize != 0) batchBlock++; - cout << "Z1 STARTING \n"; vector subInstances; Metric eval; for (int iter = 0; iter < m_options.maxIter; ++iter) { @@ -283,37 +274,6 @@ void DisParser::train(const string &trainFile, const string &devFile, Metric dev_span, dev_nuclear, dev_relation, dev_full; Metric test_span, test_nuclear, test_relation, test_full; - if (true){ - Metric train_span, train_nuclear, train_relation, train_full; - auto t_start_train = std::chrono::high_resolution_clock::now(); - cout << "Train start." << std::endl; - train_span.reset(); - train_nuclear.reset(); - train_relation.reset(); - predict(trainInsts, decodeInstResults); - int trainNum = trainInsts.size(); - for (int idx = 0; idx < trainNum; idx++) { - trainInsts[idx].evaluate(decodeInstResults[idx], train_span, train_nuclear, train_relation, train_full); - } - auto t_end_train = std::chrono::high_resolution_clock::now(); - cout << "Train Performance measured. Total time taken is: " << std::chrono::duration(t_end_train - t_start_train).count() << std::endl; - cout << "train:" << std::endl; - cout << "S: "; - train_span.print(); - cout << "N: "; - train_nuclear.print(); - cout << "R: "; - train_relation.print(); - cout << "F: "; - train_full.print(); - if (!m_options.outBest.empty() && train_full.getAccuracy() > bestFmeasure) { - m_pipe.outputAllInstances(trainFile + m_options.outBest + to_string(iter), decodeInstResults); - } - } - - - - if (devNum > 0) { auto t_start_dev = std::chrono::high_resolution_clock::now(); cout << "Dev start." << std::endl; @@ -406,6 +366,11 @@ void DisParser::test(const string &testFile, const string &outputFile, const str getSynFeats(testInsts, testFile + ".dump"); + /* + NOTE (Mat-sipahi): The following lines (down to `m_driver.initial`) are copied from `train` function + in order to avoid segmentations faults we used to get because if uninitalized hyperparams in different parts of the code. + But I'm not sure which one of them is redundant or coming in incorrect order. + */ addTestAlpha(testInsts); //createAlphabet(trainInsts); //getGoldActions(trainInsts); @@ -436,6 +401,10 @@ void DisParser::test(const string &testFile, const string &outputFile, const str m_driver.initial(); + /* + End of the block copied from `train` (Mat-sipahi) + */ + vector decodeInstResults; int testNum = testInsts.size(); @@ -479,7 +448,6 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { vector vecLines; vector info; int index = 0, sent_size; - int word_count = 0; while (getline(file, line)) { if (line == "") { sent_size = vecLines.size(); @@ -495,19 +463,14 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { Instance &cur_inst = vecInsts[index]; cur_inst.dep_feats.push_back(dep_feat); if (cur_inst.sent_types.size() == cur_inst.dep_feats.size()) { - //cout << "INDEX " << index << " VEC LINES " << vecLines.size() << " VEC INST SIZE: " << cur_inst.dep_feats.size() <<" word_count:" << word_count << endl; - word_count = 0; index++; if (index == vecInsts.size()) break; } vecLines.clear(); } - else { - word_count += 1; - //if (index >=10 && index <= 12) cout << line << endl; + else vecLines.push_back(line); - } } file.close(); @@ -522,23 +485,10 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { for (int idy = 0; idy < dep_feat_size; idy++) { word_num += inst.dep_feats[idy].words.size(); } - if (word_num != inst.words.size()) { - cout << path << " +++ " << idx << " ----- " << word_num << "<>" << inst.words.size() << endl; - for (int i = 0; i < inst.words.size(); i ++) { - cout << inst.words[i] << " "; - } - cout <<"---------------" < &vecInsts, const string &folder) { vector vecLine1, vecLine2, vecLine3, vecLine4, vecLine5; vector vecInfo; int index = 0, num = 0, offset = 0, word_offset; - while (1) { - int count1 = 0, count2=0, count3=0, count4=0, count5=0; vecLine1.clear(); while (1) { if (!my_getline(inf1, strLine)) { @@ -577,7 +525,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine1.push_back(strLine); - count1 ++; } vecLine2.clear(); @@ -588,7 +535,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine2.push_back(strLine); - count2 ++; } vecLine3.clear(); @@ -599,7 +545,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine3.push_back(strLine); - count3 ++; } vecLine4.clear(); @@ -610,7 +555,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine4.push_back(strLine); - count4 ++; } vecLine5.clear(); @@ -621,8 +565,8 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { if (strLine.empty()) break; vecLine5.push_back(strLine); - count5 ++; } + int vec_size = vecLine1.size(); if (vec_size == 0) { if (index != vecInsts.size()) { @@ -632,26 +576,21 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { } if (vecLine2.size() != vec_size || vecLine3.size() != vec_size || vecLine4.size() != vec_size || vecLine5.size() != vec_size) { - cout << vec_size << "-" << vecLine2.size() << "-" << vecLine3.size() << "-" << - vecLine4.size() << "-" << vecLine5.size() << endl; std::cout << "extern feature input error" << std::endl; } Instance &cur_inst = vecInsts[index]; const DepFeat &cur_dep_feat = cur_inst.dep_feats[num - offset]; if (num - offset == 0) word_offset = 0; + if (cur_dep_feat.words.size() != vec_size) { continue; } + for (int i = 0; i < vec_size; i++) { vecInfo.clear(); split_bychar(vecLine1[i], vecInfo, ' '); - - if (normalize_to_lowerwithdigit(vecInfo[0]).compare(cur_dep_feat.words[i]) != 0) { - cout << vecInfo[0] << " (" << i << ") normalized: " << normalize_to_lowerwithdigit(vecInfo[0]) << " <> " << cur_dep_feat.words[i] << endl; - for (int k = 0; k < vec_size; k ++) cout << cur_dep_feat.words[k] << " "; cout << endl; - } assert(normalize_to_lowerwithdigit(vecInfo[0]).compare(cur_dep_feat.words[i]) == 0); } int cur_word_size = cur_dep_feat.words.size(), syn_offset; @@ -660,67 +599,67 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { cur_inst.syn_feats[syn_offset].resize(6); split_bychar(vecLine1[i], vecInfo, ' '); - //assert(vecInfo.size() == 501); + assert(vecInfo.size() == 501); extern_nodes[node_count].init(500, -1); cur_inst.syn_feats[syn_offset][0] = &extern_nodes[node_count]; for (int j = 0; j < 500; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - // assert(vecInfo.size() == 513); // ----------------------- 501 - // extern_nodes[node_count].init(512, -1); - // cur_inst.syn_feats[syn_offset][0] = &extern_nodes[node_count]; - // for (int j = 0; j < 512; j++) { - // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - // } + assert(vecInfo.size() == 513); + extern_nodes[node_count].init(512, -1); + cur_inst.syn_feats[syn_offset][0] = &extern_nodes[node_count]; + for (int j = 0; j < 512; j++) { + extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + } node_count++; split_bychar(vecLine2[i], vecInfo, ' '); - //vecInfo.size() == 101); + assert(vecInfo.size() == 101); extern_nodes[node_count].init(100, -1); cur_inst.syn_feats[syn_offset][1] = &extern_nodes[node_count]; for (int j = 0; j < 100; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - // assert(vecInfo.size() == 129); // --------------------- 101 - // extern_nodes[node_count].init(128, -1); - // cur_inst.syn_feats[syn_offset][1] = &extern_nodes[node_count]; - // for (int j = 0; j < 128; j++) { - // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - // } + assert(vecInfo.size() == 129); + extern_nodes[node_count].init(128, -1); + cur_inst.syn_feats[syn_offset][1] = &extern_nodes[node_count]; + for (int j = 0; j < 128; j++) { + extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + } node_count++; split_bychar(vecLine3[i], vecInfo, ' '); - // assert(vecInfo.size() == 501); + assert(vecInfo.size() == 501); extern_nodes[node_count].init(500, -1); cur_inst.syn_feats[syn_offset][2] = &extern_nodes[node_count]; for (int j = 0; j < 500; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - // assert(vecInfo.size() == 513); - // extern_nodes[node_count].init(512, -1); - // cur_inst.syn_feats[syn_offset][2] = &extern_nodes[node_count]; - // for (int j = 0; j < 512; j++) { - // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - // } + assert(vecInfo.size() == 513); + extern_nodes[node_count].init(512, -1); + cur_inst.syn_feats[syn_offset][2] = &extern_nodes[node_count]; + for (int j = 0; j < 512; j++) { + extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + } node_count++; split_bychar(vecLine4[i], vecInfo, ' '); - //assert(vecInfo.size() == 101); + assert(vecInfo.size() == 101); extern_nodes[node_count].init(100, -1); cur_inst.syn_feats[syn_offset][3] = &extern_nodes[node_count]; for (int j = 0; j < 100; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - // assert(vecInfo.size() == 129); - // extern_nodes[node_count].init(128, -1); - // cur_inst.syn_feats[syn_offset][3] = &extern_nodes[node_count]; - // for (int j = 0; j < 128; j++) { - // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - // } + assert(vecInfo.size() == 129); + extern_nodes[node_count].init(128, -1); + cur_inst.syn_feats[syn_offset][3] = &extern_nodes[node_count]; + for (int j = 0; j < 128; j++) { + extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + } node_count++; split_bychar(vecLine5[i], vecInfo, ' '); - //assert(vecInfo.size() == 801); + assert(vecInfo.size() == 801); extern_nodes[node_count].init(400, -1); cur_inst.syn_feats[syn_offset][4] = &extern_nodes[node_count]; for (int j = 0; j < 400; j++) { @@ -732,18 +671,18 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { for (int j = 0; j < 400; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 401].c_str()); } - // assert(vecInfo.size() == 1025); // ---------------------------- 801 - // extern_nodes[node_count].init(512, -1); - // cur_inst.syn_feats[syn_offset][4] = &extern_nodes[node_count]; - // for (int j = 0; j < 512; j++) { - // extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - // } - // node_count++; - // extern_nodes[node_count].init(512, -1); - // cur_inst.syn_feats[syn_offset][5] = &extern_nodes[node_count]; - // for (int j = 0; j < 512; j++) { - // extern_nodes[node_count].val[j] = atof(vecInfo[j + 513].c_str()); - // } + assert(vecInfo.size() == 1025); + extern_nodes[node_count].init(512, -1); + cur_inst.syn_feats[syn_offset][4] = &extern_nodes[node_count]; + for (int j = 0; j < 512; j++) { + extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); + } + node_count++; + extern_nodes[node_count].init(512, -1); + cur_inst.syn_feats[syn_offset][5] = &extern_nodes[node_count]; + for (int j = 0; j < 512; j++) { + extern_nodes[node_count].val[j] = atof(vecInfo[j + 513].c_str()); + } node_count++; } @@ -766,8 +705,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { void DisParser::writeModelFile(const string &outputModelFile) { - cout << "writeModelFile:" << outputModelFile; - cout << "outputModelFile: " << outputModelFile << endl; ofstream outf(outputModelFile.c_str()); m_driver._hyperparams.write(outf); m_driver._modelparams.saveModel(outf); From 23e9fe4586e1cfb1f5df147cb4e0d1db914307d0 Mon Sep 17 00:00:00 2001 From: Mat Sipahi Date: Wed, 31 Aug 2022 00:03:18 +0430 Subject: [PATCH 06/13] redundants reverted --- NNDisParser/build/build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NNDisParser/build/build.sh b/NNDisParser/build/build.sh index b0853e3..1973f71 100644 --- a/NNDisParser/build/build.sh +++ b/NNDisParser/build/build.sh @@ -1,2 +1,2 @@ -path=/home/ubuntu/NNDisParser-cp/ -cmake .. -DEIGEN3_DIR=${path}eigen -DN3L_DIR=${path}N3LDG -DMKL=TRUE -DCMAKE_BUILD_TYPE=Debug +path=/home/yunan/workspace/discourseParser/ +cmake .. -DEIGEN3_DIR=${path}eigen -DN3L_DIR=${path}N3LDG -DMKL=TRUE From ecb392265c4692c8bf7cdf1c568b9e7560354256 Mon Sep 17 00:00:00 2001 From: Mat Sipahi Date: Wed, 31 Aug 2022 00:12:57 +0430 Subject: [PATCH 07/13] redundant changes --- .../src/NNDisParser-synmlp-do/NNDisParser.cpp | 52 ++++--------------- 1 file changed, 9 insertions(+), 43 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index 61865d5..d5336ef 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -172,6 +172,7 @@ void DisParser::train(const string &trainFile, const string &devFile, node_count = 0; string syn = "conll.dump.results"; + getSynFeats(trainInsts, m_options.dumpFolder + path_separator + "train." + syn); getSynFeats(devInsts, m_options.dumpFolder + path_separator + "dev." + syn); getSynFeats(testInsts, m_options.dumpFolder + path_separator + "test." + syn); @@ -179,7 +180,6 @@ void DisParser::train(const string &trainFile, const string &devFile, for (int idx = 0; idx < m_options.testFiles.size(); idx++) { m_pipe.readInstances(m_options.testFiles[idx], otherInsts[idx], m_options.maxInstance); } - addTestAlpha(devInsts); addTestAlpha(testInsts); createAlphabet(trainInsts); @@ -191,6 +191,7 @@ void DisParser::train(const string &trainFile, const string &devFile, m_driver._modelparams.edu_params.word_table.initial(&m_driver._hyperparams.wordAlpha, m_options.wordEmbFile, m_options.wordFineTune); m_driver._hyperparams.wordDim = m_driver._modelparams.edu_params.word_table.nDim; + m_driver._modelparams.edu_params.tag_table.initial(&m_driver._hyperparams.tagAlpha, m_options.tagEmbSize, m_options.tagFineTune); m_driver._hyperparams.tagDim = m_driver._modelparams.edu_params.tag_table.nDim; @@ -212,7 +213,6 @@ void DisParser::train(const string &trainFile, const string &devFile, int batchBlock = inputSize / m_options.batchSize; if (inputSize % m_options.batchSize != 0) batchBlock++; - vector subInstances; Metric eval; for (int iter = 0; iter < m_options.maxIter; ++iter) { @@ -274,6 +274,7 @@ void DisParser::train(const string &trainFile, const string &devFile, Metric dev_span, dev_nuclear, dev_relation, dev_full; Metric test_span, test_nuclear, test_relation, test_full; + if (devNum > 0) { auto t_start_dev = std::chrono::high_resolution_clock::now(); cout << "Dev start." << std::endl; @@ -357,6 +358,7 @@ void DisParser::test(const string &testFile, const string &outputFile, const str getDepFeats(testInsts, testFile + ".conll"); int word_count = 0, max_size; + max_size = testInsts.size(); for (int idx = 0; idx < max_size; idx++) { word_count += testInsts[idx].words.size(); @@ -368,8 +370,8 @@ void DisParser::test(const string &testFile, const string &outputFile, const str /* NOTE (Mat-sipahi): The following lines (down to `m_driver.initial`) are copied from `train` function - in order to avoid segmentations faults we used to get because if uninitalized hyperparams in different parts of the code. - But I'm not sure which one of them is redundant or coming in incorrect order. + in order to avoid segmentations faults we used to get because if uninitalized hyperparams in different parts of the code. + But I'm not sure which one of them is redundant or coming in incorrect order. */ addTestAlpha(testInsts); //createAlphabet(trainInsts); @@ -408,7 +410,6 @@ void DisParser::test(const string &testFile, const string &outputFile, const str vector decodeInstResults; int testNum = testInsts.size(); - Metric test_span, test_nuclear, test_relation, test_full; if (testNum > 0) { auto t_start_test = std::chrono::high_resolution_clock::now(); @@ -460,6 +461,7 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { dep_feat.heads[idx] = atoi(info[6].c_str()) - 1; dep_feat.dep_relations[idx] = info[7]; } + Instance &cur_inst = vecInsts[index]; cur_inst.dep_feats.push_back(dep_feat); if (cur_inst.sent_types.size() == cur_inst.dep_feats.size()) { @@ -472,7 +474,6 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { else vecLines.push_back(line); } - file.close(); // checking... int inst_size = vecInsts.size(); @@ -599,18 +600,12 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { cur_inst.syn_feats[syn_offset].resize(6); split_bychar(vecLine1[i], vecInfo, ' '); - assert(vecInfo.size() == 501); + assert(vecInfo.size() == 501); extern_nodes[node_count].init(500, -1); cur_inst.syn_feats[syn_offset][0] = &extern_nodes[node_count]; for (int j = 0; j < 500; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - assert(vecInfo.size() == 513); - extern_nodes[node_count].init(512, -1); - cur_inst.syn_feats[syn_offset][0] = &extern_nodes[node_count]; - for (int j = 0; j < 512; j++) { - extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - } node_count++; split_bychar(vecLine2[i], vecInfo, ' '); @@ -620,12 +615,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { for (int j = 0; j < 100; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - assert(vecInfo.size() == 129); - extern_nodes[node_count].init(128, -1); - cur_inst.syn_feats[syn_offset][1] = &extern_nodes[node_count]; - for (int j = 0; j < 128; j++) { - extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - } node_count++; split_bychar(vecLine3[i], vecInfo, ' '); @@ -635,12 +624,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { for (int j = 0; j < 500; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - assert(vecInfo.size() == 513); - extern_nodes[node_count].init(512, -1); - cur_inst.syn_feats[syn_offset][2] = &extern_nodes[node_count]; - for (int j = 0; j < 512; j++) { - extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - } node_count++; split_bychar(vecLine4[i], vecInfo, ' '); @@ -650,12 +633,6 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { for (int j = 0; j < 100; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } - assert(vecInfo.size() == 129); - extern_nodes[node_count].init(128, -1); - cur_inst.syn_feats[syn_offset][3] = &extern_nodes[node_count]; - for (int j = 0; j < 128; j++) { - extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - } node_count++; split_bychar(vecLine5[i], vecInfo, ' '); @@ -666,23 +643,12 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); } node_count++; + extern_nodes[node_count].init(400, -1); cur_inst.syn_feats[syn_offset][5] = &extern_nodes[node_count]; for (int j = 0; j < 400; j++) { extern_nodes[node_count].val[j] = atof(vecInfo[j + 401].c_str()); } - assert(vecInfo.size() == 1025); - extern_nodes[node_count].init(512, -1); - cur_inst.syn_feats[syn_offset][4] = &extern_nodes[node_count]; - for (int j = 0; j < 512; j++) { - extern_nodes[node_count].val[j] = atof(vecInfo[j + 1].c_str()); - } - node_count++; - extern_nodes[node_count].init(512, -1); - cur_inst.syn_feats[syn_offset][5] = &extern_nodes[node_count]; - for (int j = 0; j < 512; j++) { - extern_nodes[node_count].val[j] = atof(vecInfo[j + 513].c_str()); - } node_count++; } From 59e6154d98c0440f052d56cebc1818c7ce5ac588 Mon Sep 17 00:00:00 2001 From: Mat Sipahi Date: Wed, 31 Aug 2022 00:15:21 +0430 Subject: [PATCH 08/13] cleanup the PR --- NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index d5336ef..8c544d3 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -461,7 +461,6 @@ void DisParser::getDepFeats(vector &vecInsts, const string &path) { dep_feat.heads[idx] = atoi(info[6].c_str()) - 1; dep_feat.dep_relations[idx] = info[7]; } - Instance &cur_inst = vecInsts[index]; cur_inst.dep_feats.push_back(dep_feat); if (cur_inst.sent_types.size() == cur_inst.dep_feats.size()) { @@ -579,6 +578,7 @@ void DisParser::getSynFeats(vector &vecInsts, const string &folder) { vecLine4.size() != vec_size || vecLine5.size() != vec_size) { std::cout << "extern feature input error" << std::endl; } + Instance &cur_inst = vecInsts[index]; const DepFeat &cur_dep_feat = cur_inst.dep_feats[num - offset]; if (num - offset == 0) From 0099b2b7c144d53dd9332615228b367d957bbc54 Mon Sep 17 00:00:00 2001 From: mat sipahi <34083477+mat-sipahi@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:02:50 +0430 Subject: [PATCH 09/13] Update NNDisParser.cpp --- NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index 8c544d3..ab10075 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -402,6 +402,7 @@ void DisParser::test(const string &testFile, const string &outputFile, const str //m_driver._hyperparams.etypeAlpha.initial(m_driver._hyperparams.etype_stat, 0); m_driver.initial(); + loadModelFile(modelFile); /* End of the block copied from `train` (Mat-sipahi) From d5e288e4bbbdd96a0645e4e7f51ec5fe63b023ab Mon Sep 17 00:00:00 2001 From: mat sipahi <34083477+mat-sipahi@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:09:32 +0430 Subject: [PATCH 10/13] Update NNDisParser.cpp --- .../src/NNDisParser-synmlp-do/NNDisParser.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index ab10075..2c37e25 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -350,7 +350,10 @@ void DisParser::test(const string &testFile, const string &outputFile, const str m_options.load(optionFile); m_options.showOptions(); m_driver._hyperparams.setRequared(m_options); - + + /% + IMPRTANT: the model is loaded in before everything; otherwise we used to get `Segmentation Fault` in the next initialization steps. + %/ loadModelFile(modelFile); vector testInsts; @@ -370,7 +373,7 @@ void DisParser::test(const string &testFile, const string &outputFile, const str /* NOTE (Mat-sipahi): The following lines (down to `m_driver.initial`) are copied from `train` function - in order to avoid segmentations faults we used to get because if uninitalized hyperparams in different parts of the code. + in order to avoid segmentations fault we used to get because if uninitalized hyperparams in different parts of the code. But I'm not sure which one of them is redundant or coming in incorrect order. */ addTestAlpha(testInsts); @@ -402,11 +405,15 @@ void DisParser::test(const string &testFile, const string &outputFile, const str //m_driver._hyperparams.etypeAlpha.initial(m_driver._hyperparams.etype_stat, 0); m_driver.initial(); - loadModelFile(modelFile); - + /* End of the block copied from `train` (Mat-sipahi) */ + + /* + IMPORTANT: model is loaded for the second time. The parser was predicting all relations as ENABLEMENT without it. + */ + loadModelFile(modelFile); vector decodeInstResults; int testNum = testInsts.size(); From 007ef0b63af448eb842f051cabea8287480f00d0 Mon Sep 17 00:00:00 2001 From: mat sipahi <34083477+mat-sipahi@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:17:02 +0430 Subject: [PATCH 11/13] Update NNDisParser.cpp --- NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index 2c37e25..415fd9f 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -352,7 +352,7 @@ void DisParser::test(const string &testFile, const string &outputFile, const str m_driver._hyperparams.setRequared(m_options); /% - IMPRTANT: the model is loaded in before everything; otherwise we used to get `Segmentation Fault` in the next initialization steps. + IMPRTANT: the model is loaded just in the beginning; otherwise we used to get `Segmentation Fault` in the next initialization steps. %/ loadModelFile(modelFile); @@ -372,7 +372,7 @@ void DisParser::test(const string &testFile, const string &outputFile, const str getSynFeats(testInsts, testFile + ".dump"); /* - NOTE (Mat-sipahi): The following lines (down to `m_driver.initial`) are copied from `train` function + NOTE (Mat-sipahi): The following lines (down to `m_driver.initial`) are copied from `train` function in order to avoid segmentations fault we used to get because if uninitalized hyperparams in different parts of the code. But I'm not sure which one of them is redundant or coming in incorrect order. */ @@ -407,11 +407,11 @@ void DisParser::test(const string &testFile, const string &outputFile, const str m_driver.initial(); /* - End of the block copied from `train` (Mat-sipahi) + End of the block copied from `train` (Mat-sipahi) */ /* - IMPORTANT: model is loaded for the second time. The parser was predicting all relations as ENABLEMENT without it. + IMPORTANT: model is loaded for the second time. The parser was predicting all relations as ENABLEMENT without it. */ loadModelFile(modelFile); From 084d54136f672536431847daad2a00e2639bde14 Mon Sep 17 00:00:00 2001 From: mat sipahi <34083477+mat-sipahi@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:20:49 +0430 Subject: [PATCH 12/13] Update ModelParams.h --- NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h b/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h index 0f7ee11..b4f7dac 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h +++ b/NNDisParser/src/NNDisParser-synmlp-do/model/ModelParams.h @@ -65,7 +65,8 @@ class ModelParams { edu_lstm_right_layer1_params.save(os); scored_action_table.save(os); } - + + // NOTE: the model should read data from the initailized hyperparams void loadModel(std::ifstream &is, HyperParams &opts) { edu_params.load(is); syn_params.load(is); From 5e685fd307c4176d0fa7e112b5da12a225c7f35f Mon Sep 17 00:00:00 2001 From: mat sipahi <34083477+mat-sipahi@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:25:38 +0430 Subject: [PATCH 13/13] Update NNDisParser.cpp --- NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp index 415fd9f..0019f3d 100644 --- a/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp +++ b/NNDisParser/src/NNDisParser-synmlp-do/NNDisParser.cpp @@ -373,8 +373,8 @@ void DisParser::test(const string &testFile, const string &outputFile, const str /* NOTE (Mat-sipahi): The following lines (down to `m_driver.initial`) are copied from `train` function - in order to avoid segmentations fault we used to get because if uninitalized hyperparams in different parts of the code. - But I'm not sure which one of them is redundant or coming in incorrect order. + in order to avoid segmentations fault caused by uninitalized hyperparams in different parts of the code. + But I've commented out unnecessary steps, but I'm not sure if there are any other redindant steps. */ addTestAlpha(testInsts); //createAlphabet(trainInsts);