37 #ifndef RERANKER_MODEL_PROTO_WRITER_H_
38 #define RERANKER_MODEL_PROTO_WRITER_H_
44 #include "../proto/model.pb.h"
45 #include "../proto/dataio.h"
52 using confusion_learning::FeatureVecMessage;
53 using confusion_learning::ModelMessage;
56 using std::shared_ptr;
78 virtual void Write(
const Model *model, ModelMessage *model_message,
79 bool write_features =
true)
const = 0;
100 bool output_best_epoch =
false,
102 bool output_key =
false,
103 const string separator =
"\t")
const = 0;
110 #define REGISTER_NAMED_MODEL_PROTO_WRITER(TYPE,NAME) \
111 REGISTER_NAMED(TYPE,NAME,ModelProtoWriter)
116 #define REGISTER_MODEL_PROTO_WRITER(TYPE) \
117 REGISTER_NAMED_MODEL_PROTO_WRITER(TYPE,TYPE)
143 shared_ptr<ModelProtoWriter> writer,
146 bool verbose =
true) :
147 writer_(writer), model_file_(model_file),
148 compressed_(compressed),
149 use_base64_(use_base64), verbose_(verbose), prev_best_epoch_index_(-1) { }
166 <<
" to file \"" << model_file_ <<
"\"...";
170 confusion_learning::ModelMessage model_message;
171 writer_->Write(model, &model_message);
173 string tmp_file = model_file_ +
".tmp";
174 ConfusionProtoIO proto_writer(tmp_file, ConfusionProtoIO::WRITE,
175 compressed_, use_base64_);
176 proto_writer.Write(model_message);
177 proto_writer.Close();
178 rename(tmp_file.c_str(), model_file_.c_str());
182 cerr <<
" done." << endl;
186 shared_ptr<ModelProtoWriter> writer_;
191 int prev_best_epoch_index_;
virtual void Write(const Model *model, ModelMessage *model_message, bool write_features=true) const =0
Serializes a Model instance to a ModelMessage.
Model is an interface for reranking models.
virtual ~ModelProtoWriter()
Destroys this writer.
An end-of-epoch hook for writing out the best model so far to file after each epoch (if the best mode...
virtual int best_model_epoch() const =0
EndOfEpochModelWriter(const string &model_file, shared_ptr< ModelProtoWriter > writer, bool compressed, bool use_base64, bool verbose=true)
Constructs a new instance to write out the best model to the specified file.
A class to construct a ModelMessage from a Model instance.
ModelProtoWriter()
Constructs a new instance that can serialize Model instances to ModelMessage protocol buffer messages...
virtual void WriteFeatures(const Model *model, ostream &os, bool output_best_epoch=false, double weight=1.0, bool output_key=false, const string separator="\t") const =0
Writes out the features of this model to a series of FeatureMessage instances using the specified Con...
Serializer for reranker::FeatureVector instances to FeatureVecMessage instances.
An interface to make it easier to implement Factory-constructible types by implementing both required...
virtual void Do(Model *model)
Executes this end-of-epoch hook, which writes out the best model so far if its epoch differs from tha...
Provides a generic dynamic object factory.
An interface for specifying a hook to be run by a Model instance.
Reranker model interface.