Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneplus committed Jun 3, 2015
1 parent 96ed927 commit 377e8aa
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 101 deletions.
17 changes: 10 additions & 7 deletions src/ner/ner_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NamedEntityRecognizerFrontend::NamedEntityRecognizerFrontend(
INFO_LOG("||| ltp ner, trainig ...");
INFO_LOG("report: reference file = %s", train_opt.train_file.c_str());
INFO_LOG("report: holdout file = %s", train_opt.holdout_file.c_str());
INFO_LOG("report: algorith = %s", train_opt.algorithm.c_str());
INFO_LOG("report: algorithm = %s", train_opt.algorithm.c_str());
INFO_LOG("report: model name = %s", train_opt.model_name.c_str());
INFO_LOG("report: maximum iteration = %d", train_opt.max_iter);
INFO_LOG("report: rare threshold = %d", train_opt.rare_feature_threshold);
Expand Down Expand Up @@ -157,6 +157,7 @@ void NamedEntityRecognizerFrontend::build_feature_space(void) {
model->space.set_num_labels(L);

size_t interval = train_dat.size() / 10;
if (0 == interval) { interval = 1; }
for (size_t i = 0; i < train_dat.size(); ++ i) {
NamedEntityRecognizer::extract_features((*train_dat[i]), NULL, true);
if ((i+ 1) % interval == 0) {
Expand Down Expand Up @@ -214,6 +215,7 @@ void NamedEntityRecognizerFrontend::train(void) {
INFO_LOG("Training iteration #%d", (iter + 1));

size_t interval = train_dat.size() / 10;
if (interval == 0) { interval = 1; }
for (size_t i = 0; i < train_dat.size(); ++ i) {
Instance* inst = train_dat[i];
NamedEntityRecognizer::extract_features((*inst), &ctx, false);
Expand Down Expand Up @@ -252,18 +254,19 @@ void NamedEntityRecognizerFrontend::train(void) {
double f_score;
evaluate(f_score);

std::swap(model, new_model);

if(f_score > best_f_score){
best_f_score = f_score;
best_iteration = iter;
}

std::string saved_model_file = (train_opt.model_name+ "."+ to_str(iter));
std::ofstream ofs(saved_model_file.c_str(), std::ofstream::binary);
std::swap(model, new_model);
new_model->save(model_header, Parameters::kDumpAveraged, ofs);
std::ofstream ofs(train_opt.model_name.c_str(), std::ofstream::binary);
new_model->save(model_header, Parameters::kDumpAveraged, ofs);
INFO_LOG("trace: model for iteration #%d is saved to %s",
iter+1, train_opt.model_name.c_str());
}
delete new_model;

INFO_LOG("trace: model for iteration #%d is saved to %s", iter+1, saved_model_file.c_str());
}
INFO_LOG("Best result (iteration = %d) : F-score = %lf", best_iteration, best_f_score);
}
Expand Down
3 changes: 2 additions & 1 deletion src/parser.n/parser_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void NeuralNetworkParserFrontend::build_cluster(void) {

std::string line;
size_t interval = LineCountsReader(ifs).number_of_lines() / 10;
if (interval == 0) { interval = 1; }
size_t nr_lines = 1;

while (std::getline(ifs, line)) {
Expand Down Expand Up @@ -176,7 +177,7 @@ void NeuralNetworkParserFrontend::collect_precomputed_features() {

size_t nr_processed = 0;
nr_feature_types = 0;
size_t interval = train_dat.size() / 10; if (interval == 0) { interval = 10; }
size_t interval = train_dat.size() / 10; if (interval == 0) { interval = 1; }
for (size_t d = 0; d < train_dat.size(); ++ d) {
Instance* inst = train_dat[d];
if (!inst->is_tree() || !inst->is_projective()) { continue; }
Expand Down
17 changes: 10 additions & 7 deletions src/postagger/postagger_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ PostaggerFrontend::PostaggerFrontend(const std::string& reference_file,
INFO_LOG("||| ltp postagger, training ...");
INFO_LOG("report: reference file = %s", train_opt.train_file.c_str());
INFO_LOG("report: holdout file = %s", train_opt.holdout_file.c_str());
INFO_LOG("report: algorith = %s", train_opt.algorithm.c_str());
INFO_LOG("report: algorithm = %s", train_opt.algorithm.c_str());
INFO_LOG("report: model name = %s", train_opt.model_name.c_str());
INFO_LOG("report: maximum iteration = %d", train_opt.max_iter);
INFO_LOG("report: rare threshold = %d", train_opt.rare_feature_threshold);
Expand Down Expand Up @@ -111,6 +111,7 @@ void PostaggerFrontend::build_feature_space(void) {
model->space.set_num_labels(L);

size_t interval = train_dat.size() / 10;
if (interval == 0) { interval = 1; }
for (size_t i = 0; i < train_dat.size(); ++ i) {
Postagger::extract_features((*train_dat[i]), NULL, true);
if ((i+ 1) % interval == 0) {
Expand Down Expand Up @@ -163,6 +164,7 @@ void PostaggerFrontend::train(void) {
INFO_LOG("Training iteraition #%d", (iter + 1));

size_t interval= train_dat.size() / 10;
if (interval == 0) { interval = 1; }
for (size_t i = 0; i < train_dat.size(); ++ i) {
Instance* inst = train_dat[i];
extract_features((*inst), &ctx, false);
Expand Down Expand Up @@ -199,19 +201,20 @@ void PostaggerFrontend::train(void) {
std::swap(model, new_model);
double p;
evaluate(p);
std::swap(model, new_model);

if(p > best_p){
best_p = p;
best_iteration = iter;

std::ofstream ofs(train_opt.model_name.c_str(), std::ofstream::binary);
new_model->save(model_header, Parameters::kDumpAveraged, ofs);

INFO_LOG("trace: model for iteration #%d is saved to %s",
iter+1, train_opt.model_name.c_str());
}

std::string saved_model_file = (train_opt.model_name+ "."+ to_str(iter));
std::ofstream ofs(saved_model_file.c_str(), std::ofstream::binary);
std::swap(model, new_model);
new_model->save(model_header, Parameters::kDumpAveraged, ofs);
delete new_model;

INFO_LOG("trace: model for iteration #%d is saved to %s", iter+1, saved_model_file.c_str());
}

INFO_LOG("Best result (iteration = %d) : P = %lf", best_iteration, best_p);
Expand Down
24 changes: 12 additions & 12 deletions src/segmentor/segmentor_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ SegmentorFrontend::SegmentorFrontend(const std::string& reference_file,
INFO_LOG("||| ltp segmentor, training ...");
INFO_LOG("report: reference file = %s", train_opt.train_file.c_str());
INFO_LOG("report: holdout file = %s", train_opt.holdout_file.c_str());
INFO_LOG("report: algorith = %s", train_opt.algorithm.c_str());
INFO_LOG("report: algorithm = %s", train_opt.algorithm.c_str());
INFO_LOG("report: model name = %s", train_opt.model_name.c_str());
INFO_LOG("report: maximum iteration = %d", train_opt.max_iter);
INFO_LOG("report: rare threshold = %d", train_opt.rare_feature_threshold);
Expand Down Expand Up @@ -176,6 +176,7 @@ void SegmentorFrontend::build_feature_space(void) {
model->space.set_num_labels(L);

size_t interval = train_dat.size() / 10;
if (0 == interval) { interval = 1; }

for (size_t i = 0; i < train_dat.size(); ++ i) {
build_lexicon_match_state(lexicons, train_dat[i]);
Expand Down Expand Up @@ -261,6 +262,7 @@ void SegmentorFrontend::train(void) {
INFO_LOG("Training iteration #%d", (iter + 1));

size_t interval = train_dat.size()/ 10;
if (interval == 0) { interval = 1; }
for (size_t i = 0; i < train_dat.size(); ++ i) {
increase_timestamp();

Expand All @@ -270,7 +272,6 @@ void SegmentorFrontend::train(void) {

con.regist(&(inst->chartypes));
decoder.decode(scm, con, inst->predict_tagsidx);
//decoder->decode((*scm), inst->predict_tagsidx);

collect_features((*inst));

Expand Down Expand Up @@ -303,21 +304,20 @@ void SegmentorFrontend::train(void) {
double p, r, f;
evaluate(p,r,f);

std::swap(model, new_model);

if (f > best_f) {
best_p = p; best_r = r; best_f = f;
best_iteration = iter;
}

std::string saved_model_file = (train_opt.model_name + "." + strutils::to_str(iter));
std::ofstream ofs(saved_model_file.c_str(), std::ofstream::binary);

std::swap(model, new_model);
new_model->save(model_header.c_str(),
(train_opt.dump_model_details? Parameters::kDumpDetails: Parameters::kDumpAveraged),
ofs);
std::ofstream ofs(train_opt.model_name.c_str(), std::ofstream::binary);
new_model->save(model_header.c_str(),
(train_opt.dump_model_details? Parameters::kDumpDetails: Parameters::kDumpAveraged),
ofs);

INFO_LOG("Model for iteration #%d is saved to [%s]", iter+1, train_opt.model_name.c_str());
}
delete new_model;
INFO_LOG("Model for iteration #%d is saved to [%s]", iter+1, saved_model_file.c_str());
}

INFO_LOG("Best result (iteratin = %d) P = %lf | R = %lf | F = %lf",
Expand Down Expand Up @@ -351,7 +351,7 @@ void SegmentorFrontend::evaluate(double &p, double &r, double &f) {
build_lexicon_match_state(lexicons, inst);
extract_features((*inst), false);
calculate_scores((*inst), true);

con.regist(&(inst->chartypes));
decoder.decode(scm, con, inst->predict_tagsidx);
ctx.clear();
Expand Down
104 changes: 30 additions & 74 deletions tools/train/rock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

ROOT=
BUILD_DIR=build
CONF_DIR=conf
LOG_DIR=log

# create model output folder
Expand All @@ -28,10 +27,7 @@ mkdir -p $LOG_DIR

# cws dir config
CWS_MODEL_DIR=$BUILD_DIR/cws
CWS_MODEL_PATH=$CWS_MODEL_DIR/example-seg.0.model

CWS_CONF_DIR=$CONF_DIR/cws
CWS_CONF_TRAIN_PATH=$CWS_CONF_DIR/cws.cnf
CWS_MODEL_PATH=$CWS_MODEL_DIR/example-seg.model

CWS_LOG_DIR=$LOG_DIR/cws
CWS_LOG_TRAIN_PATH=$CWS_LOG_DIR/example-seg.train.log
Expand All @@ -43,9 +39,12 @@ mkdir -p $CWS_MODEL_DIR
mkdir -p $CWS_LOG_DIR

# execute the example training process
$CWS_EXE $CWS_CONF_TRAIN_PATH >& $CWS_LOG_TRAIN_PATH
$CWS_EXE learn --model $CWS_MODEL_PATH \
--reference sample/seg/example-train.seg \
--development sample/seg/example-holdout.seg \
--max-iter 1

if [ ! -f $CWS_MODEL_PATH ]; then
if [ ! -f $CWS_MODEL_PATH ]; then
echo "[1] ERROR: CWS model is not detected!"
else
echo "[1] TRACE: CWS train model test is passed."
Expand All @@ -56,10 +55,7 @@ fi
#################################################

POS_MODEL_DIR=$BUILD_DIR/pos
POS_MODEL_PATH=$POS_MODEL_DIR/example-pos.0.model

POS_CONF_DIR=$CONF_DIR/pos
POS_CONF_TRAIN_PATH=$POS_CONF_DIR/pos.cnf
POS_MODEL_PATH=$POS_MODEL_DIR/example-pos.model

POS_LOG_DIR=$LOG_DIR/pos
POS_LOG_TRAIN_PATH=$CWS_LOG_DIR/example-pos.train.log
Expand All @@ -70,7 +66,10 @@ POS_EXE=./otpos
mkdir -p $POS_MODEL_DIR
mkdir -p $POS_LOG_DIR

$POS_EXE $POS_CONF_TRAIN_PATH >& $POS_LOG_TRAIN_PATH
$POS_EXE learn --model $POS_MODEL_PATH \
--reference sample/pos/example-train.pos \
--development sample/pos/example-holdout.pos \
--max-iter 1

if [ ! -f $CWS_MODEL_PATH ]; then
echo "[2] ERROR: POS model is not detected!"
Expand All @@ -84,10 +83,7 @@ fi

# ner dir config
NER_MODEL_DIR=$BUILD_DIR/ner
NER_MODEL_PATH=$NER_MODEL_DIR/example-ner.0.model

NER_CONF_DIR=$CONF_DIR/ner
NER_CONF_TRAIN_PATH=$NER_CONF_DIR/ner.cnf
NER_MODEL_PATH=$NER_MODEL_DIR/example-ner.model

NER_LOG_DIR=$LOG_DIR/ner
NER_LOG_TRAIN_PATH=$NER_LOG_DIR/example-ner.train.log
Expand All @@ -99,7 +95,10 @@ mkdir -p $NER_MODEL_DIR
mkdir -p $NER_LOG_DIR

# execute the example training process
$NER_EXE $NER_CONF_TRAIN_PATH >& $NER_LOG_TRAIN_PATH
$NER_EXE learn --model $NER_MODEL_PATH \
--reference sample/ner/example-train.ner \
--development sample/ner/example-holdout.ner \
--max-iter 1

if [ ! -f $NER_MODEL_PATH ]; then
echo "[3] ERROR: NER model is not detected!"
Expand All @@ -112,71 +111,28 @@ fi
#################################################

PARSER_MODEL_DIR=$BUILD_DIR/parser
PARSER_MODEL_O1_PATH=$PARSER_MODEL_DIR/example-parser-o1.0.model

PARSER_CONF_DIR=$CONF_DIR/parser
PARSER_CONF_TRAIN_O1_PATH=$PARSER_CONF_DIR/parser-o1.cnf
PARSER_MODEL_PATH=$PARSER_MODEL_DIR/example-parser.model

PARSER_LOG_DIR=$LOG_DIR/parser
PARSER_LOG_TRAIN_O1_PATH=$PARSER_LOG_DIR/example-train.conll
PARSER_LOG_TRAIN_PATH=$PARSER_LOG_DIR/example-train.conll

PARSER_EXE=./lgdpj
PARSER_EXE=./nndepparser

mkdir -p $PARSER_MODEL_DIR
mkdir -p $PARSER_LOG_DIR

$PARSER_EXE $PARSER_CONF_TRAIN_O1_PATH >& $PARSER_LOG_TRAIN_O1_PATH

if [ ! -f $PARSER_MODEL_O1_PATH ]; then
echo "[4.1] ERROR: Parser-o1 model is not detected!"
else
echo "[4.1] TRACE: Parser-o1 train model test is passed."
fi

#################################################
# THE PARSER O2 sibling SESSION #
#################################################

PARSER_MODEL_DIR=$BUILD_DIR/parser
PARSER_MODEL_O2SIB_PATH=$PARSER_MODEL_DIR/example-parser-o2sib.0.model

PARSER_CONF_DIR=$CONF_DIR/parser
PARSER_CONF_TRAIN_O2SIB_PATH=$PARSER_CONF_DIR/parser-o2sib.cnf

PARSER_LOG_DIR=$LOG_DIR/parser
PARSER_LOG_TRAIN_O2SIB_PATH=$PARSER_LOG_DIR/example-train.conll

PARSER_EXE=./lgdpj

$PARSER_EXE $PARSER_CONF_TRAIN_O2SIB_PATH >& $PARSER_LOG_TRAIN_O2SIB_PATH

if [ ! -f $PARSER_MODEL_O2SIB_PATH ]; then
echo "[4.2] ERROR: Parser-o2sib model is not detected!"
else
echo "[4.2] TRACE: Parser-o2sib train model test is passed."
fi

#################################################
# THE PARSER O2 carreras SESSION #
#################################################

PARSER_MODEL_DIR=$BUILD_DIR/parser
PARSER_MODEL_O2CARRERAS_PATH=$PARSER_MODEL_DIR/example-parser-o2carreras.0.model

PARSER_CONF_DIR=$CONF_DIR/parser
PARSER_CONF_TRAIN_O2CARRERAS_PATH=$PARSER_CONF_DIR/parser-o2carreras.cnf

PARSER_LOG_DIR=$LOG_DIR/parser
PARSER_LOG_TRAIN_O2CARRERAS_PATH=$PARSER_LOG_DIR/example-train.conll

PARSER_EXE=./lgdpj

$PARSER_EXE $PARSER_CONF_TRAIN_O2CARRERAS_PATH >& $PARSER_LOG_TRAIN_O2CARRERAS_PATH
./nndepparser learn \
--model $PARSER_MODEL_PATH \
--reference sample/parser/example-train.conll \
--development sample/parser/example-holdout.conll \
--embedding sample/parser/example.bin \
--root HED \
--max-iter 10

if [ ! -f $PARSER_MODEL_O2SIB_PATH ]; then
echo "[4.3] ERROR: Parser-o2carreras model is not detected!"
echo "[4] ERROR: neural network parser model is not detected!"
else
echo "[4.3] TRACE: Parser-o2carreras train model test is passed."
echo "[4] TRACE: neural network parser model test is passed."
fi

#################################################
Expand All @@ -199,7 +155,7 @@ mkdir -p $SRL_PRG_INSTANCE_DIR

SRL_PRG_EXE=./lgsrl

$SRL_PRG_EXE $SRL_PRG_CONF_TRAIN_PATH >& $SRL_PRG_LOG_TRAIN_PATH
$SRL_PRG_EXE $SRL_PRG_CONF_TRAIN_PATH

if [ ! -f $SRL_PRG_MODEL_PATH ]; then
echo "[5.1] ERROR: PRG model is not detected!"
Expand Down Expand Up @@ -229,7 +185,7 @@ mkdir -p $SRL_SRL_INSTANCE_DIR

SRL_SRL_EXE=./lgsrl

$SRL_SRL_EXE $SRL_SRL_CONF_TRAIN_PATH >& $SRL_SRL_LOG_TRAIN_PATH
$SRL_SRL_EXE $SRL_SRL_CONF_TRAIN_PATH

if [ ! -f $SRL_SRL_MODEL_PATH ]; then
echo "[5.2] ERROR: SRL model is not detected!"
Expand Down

0 comments on commit 377e8aa

Please sign in to comment.