From 8d900ca0af90dff71d68a7b596571df3e64c2101 Mon Sep 17 00:00:00 2001
From: Patrick Simianer
Date: Tue, 15 Sep 2015 15:32:29 +0200
Subject: dtrain: output data
---
training/dtrain/dtrain.cc | 39 +++++++++++++++++++++++++-----------
training/dtrain/dtrain.h | 43 +++++++++++++++++++++++++---------------
training/dtrain/update.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 105 insertions(+), 27 deletions(-)
(limited to 'training')
diff --git a/training/dtrain/dtrain.cc b/training/dtrain/dtrain.cc
index 9ca048c0..b39fff3e 100644
--- a/training/dtrain/dtrain.cc
+++ b/training/dtrain/dtrain.cc
@@ -12,17 +12,20 @@ main(int argc, char** argv)
po::variables_map conf;
if (!dtrain_init(argc, argv, &conf))
return 1;
- const size_t k = conf["k"].as();
- const string score_name = conf["score"].as();
- const size_t N = conf["N"].as();
- const size_t T = conf["iterations"].as();
- const weight_t eta = conf["learning_rate"].as();
- const weight_t margin = conf["margin"].as();
- const bool average = conf["average"].as();
- const bool structured = conf["struct"].as();
- const weight_t l1_reg = conf["l1_reg"].as();
- const bool keep = conf["keep"].as();
- const string output_fn = conf["output"].as();
+ const size_t k = conf["k"].as();
+ const string score_name = conf["score"].as();
+ const size_t N = conf["N"].as();
+ const size_t T = conf["iterations"].as();
+ const weight_t eta = conf["learning_rate"].as();
+ const weight_t margin = conf["margin"].as();
+ const bool average = conf["average"].as();
+ const bool structured = conf["struct"].as();
+ const weight_t l1_reg = conf["l1_reg"].as();
+ const bool keep = conf["keep"].as();
+ const bool noup = conf["disable_learning"].as();
+ const string output_fn = conf["output"].as();
+ const string output_data_which = conf["output_data"].as();
+ const bool output_data = output_data_which!="";
vector print_weights;
boost::split(print_weights, conf["print_weights"].as(),
boost::is_any_of(" "));
@@ -162,7 +165,19 @@ main(int argc, char** argv)
feature_count += observer->GetFeatureCount();
list_sz += observer->GetSize();
+ if (output_data) {
+ if (output_data_which == "kbest") {
+ OutputKbest(samples);
+ } else if (output_data_which == "default") {
+ OutputMultipartitePairs(samples, margin);
+ } else if (output_data_which == "all") {
+ OutputAllPairs(samples);
+ }
+ }
+
// get pairs and update
+ if (!noup) {
+
SparseVector updates;
if (structured)
num_up += CollectUpdatesStruct(samples, updates);
@@ -204,6 +219,8 @@ main(int argc, char** argv)
}
}
+ } // noup
+
i++;
} // input loop
diff --git a/training/dtrain/dtrain.h b/training/dtrain/dtrain.h
index 6633b4f9..0bbb5c9b 100644
--- a/training/dtrain/dtrain.h
+++ b/training/dtrain/dtrain.h
@@ -48,22 +48,24 @@ dtrain_init(int argc, char** argv, po::variables_map* conf)
{
po::options_description opts("Configuration File Options");
opts.add_options()
- ("bitext,b", po::value(), "bitext")
- ("decoder_conf,C", po::value(), "configuration file for decoder")
- ("iterations,T", po::value()->default_value(15), "number of iterations T (per shard)")
- ("k", po::value()->default_value(100), "size of kbest list")
- ("learning_rate,l", po::value()->default_value(0.00001), "learning rate")
- ("l1_reg,r", po::value()->default_value(0.), "l1 regularization strength")
- ("margin,m", po::value()->default_value(1.0), "margin for margin perceptron")
- ("score,s", po::value()->default_value("chiang"), "per-sentence BLEU approx.")
- ("N", po::value()->default_value(4), "N for BLEU approximation")
- ("input_weights,w", po::value(), "input weights file")
- ("average,a", po::bool_switch()->default_value(true), "output average weights")
- ("keep,K", po::bool_switch()->default_value(false), "output a weight file per iteration")
- ("struct,S", po::bool_switch()->default_value(false), "structured SGD with hope/fear")
- ("output,o", po::value()->default_value("-"), "output weights file, '-' for STDOUT")
- ("print_weights,P", po::value()->default_value("EgivenFCoherent SampleCountF CountEF MaxLexFgivenE MaxLexEgivenF IsSingletonF IsSingletonFE Glue WordPenalty PassThrough LanguageModel LanguageModel_OOV"),
- "list of weights to print after each iteration");
+ ("bitext,b", po::value(), "bitext")
+ ("decoder_conf,C", po::value(), "configuration file for decoder")
+ ("iterations,T", po::value()->default_value(15), "number of iterations T (per shard)")
+ ("k", po::value()->default_value(100), "size of kbest list")
+ ("learning_rate,l", po::value()->default_value(0.00001), "learning rate")
+ ("l1_reg,r", po::value()->default_value(0.), "l1 regularization strength")
+ ("margin,m", po::value()->default_value(1.0), "margin for margin perceptron")
+ ("score,s", po::value()->default_value("chiang"), "per-sentence BLEU approx.")
+ ("N", po::value()->default_value(4), "N for BLEU approximation")
+ ("input_weights,w", po::value(), "input weights file")
+ ("average,a", po::bool_switch()->default_value(true), "output average weights")
+ ("keep,K", po::bool_switch()->default_value(false), "output a weight file per iteration")
+ ("struct,S", po::bool_switch()->default_value(false), "structured SGD with hope/fear")
+ ("output,o", po::value()->default_value("-"), "output weights file, '-' for STDOUT")
+ ("disable_learning,X", po::bool_switch()->default_value(false), "disable learning")
+ ("output_data,D", po::value()->default_value(""), "output data to STDOUT; arg. is 'kbest', 'default' or 'all'")
+ ("print_weights,P", po::value()->default_value("EgivenFCoherent SampleCountF CountEF MaxLexFgivenE MaxLexEgivenF IsSingletonF IsSingletonFE Glue WordPenalty PassThrough LanguageModel LanguageModel_OOV"),
+ "list of weights to print after each iteration");
po::options_description clopts("Command Line Options");
clopts.add_options()
("conf,c", po::value(), "dtrain configuration file")
@@ -93,6 +95,15 @@ dtrain_init(int argc, char** argv, po::variables_map* conf)
return false;
}
+ if ((*conf)["output_data"].as() != "") {
+ if ((*conf)["output_data"].as() != "kbest" &&
+ (*conf)["output_data"].as() != "default" &&
+ (*conf)["output_data"].as() != "all") {
+ cerr << "Wrong 'output_data' argument: ";
+ cerr << (*conf)["output_data"].as() << endl;
+ return false;
+ }
+ }
return true;
}
diff --git a/training/dtrain/update.h b/training/dtrain/update.h
index 6f42e5bd..83dc3186 100644
--- a/training/dtrain/update.h
+++ b/training/dtrain/update.h
@@ -94,6 +94,56 @@ CollectUpdatesStruct(vector* s,
return updates.size();
}
+inline void
+OutputKbest(vector* s)
+{
+ sort(s->begin(), s->end(), _cmp);
+ size_t i = 0;
+ for (auto k: *s) {
+ cout << i << "\t" << k.gold << "\t" << k.model << " \t" << k.f << endl;
+ i++;
+ }
+}
+
+inline void
+OutputMultipartitePairs(vector* s,
+ weight_t margin=0.,
+ bool all=true)
+{
+ size_t sz = s->size();
+ sort(s->begin(), s->end(), _cmp);
+ size_t sep = round(sz*0.1);
+ for (size_t i = 0; i < sep; i++) {
+ for (size_t j = sep; j < sz; j++) {
+ if (!all && _good((*s)[i], (*s)[j], margin))
+ continue;
+ cout << (*s)[i].f-(*s)[j].f << endl;
+ }
+ }
+ size_t sep_lo = sz-sep;
+ for (size_t i = sep; i < sep_lo; i++) {
+ for (size_t j = sep_lo; j < sz; j++) {
+ if (!all && _good((*s)[i], (*s)[j], margin))
+ continue;
+ cout << (*s)[i].f-(*s)[j].f << endl;
+ }
+ }
+}
+
+inline void
+OutputAllPairs(vector* s)
+{
+ size_t sz = s->size();
+ sort(s->begin(), s->end(), _cmp);
+ for (size_t i = 0; i < sz-1; i++) {
+ for (size_t j = i+1; j < sz; j++) {
+ if ((*s)[i].gold == (*s)[j].gold)
+ continue;
+ cout << (*s)[i].f-(*s)[j].f << endl;
+ }
+ }
+}
+
} // namespace
#endif
--
cgit v1.2.3