Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
model-proto-writer.H
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
36 
37 #ifndef RERANKER_MODEL_PROTO_WRITER_H_
38 #define RERANKER_MODEL_PROTO_WRITER_H_
39 
40 #include <cstdio>
41 #include <iostream>
42 #include <memory>
43 
44 #include "../proto/model.pb.h"
45 #include "../proto/dataio.h"
46 #include "factory.H"
47 #include "feature-vector-writer.H"
48 #include "model.H"
49 
50 namespace reranker {
51 
52 using confusion_learning::FeatureVecMessage;
53 using confusion_learning::ModelMessage;
54 using std::cerr;
55 using std::endl;
56 using std::shared_ptr;
57 
62  public:
67  virtual ~ModelProtoWriter() { }
68 
78  virtual void Write(const Model *model, ModelMessage *model_message,
79  bool write_features = true) const = 0;
80 
98  virtual void WriteFeatures(const Model *model,
99  ostream &os,
100  bool output_best_epoch = false,
101  double weight = 1.0,
102  bool output_key = false,
103  const string separator = "\t") const = 0;
104 };
105 
110 #define REGISTER_NAMED_MODEL_PROTO_WRITER(TYPE,NAME) \
111  REGISTER_NAMED(TYPE,NAME,ModelProtoWriter)
112 
116 #define REGISTER_MODEL_PROTO_WRITER(TYPE) \
117  REGISTER_NAMED_MODEL_PROTO_WRITER(TYPE,TYPE)
118 
127  public:
142  EndOfEpochModelWriter(const string &model_file,
143  shared_ptr<ModelProtoWriter> writer,
144  bool compressed,
145  bool use_base64,
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) { }
150 
160  virtual void Do(Model *model) {
161  if (model->best_model_epoch() == prev_best_epoch_index_) {
162  return;
163  }
164  if (verbose_) {
165  cerr << "Writing out model from epoch " << model->best_model_epoch()
166  << " to file \"" << model_file_ << "\"...";
167  cerr.flush();
168  }
169  // First, serialize Model to a ModelMessage.
170  confusion_learning::ModelMessage model_message;
171  writer_->Write(model, &model_message);
172  // Next, write out ModelMessage to file.
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());
179  // Record which model epoch we've written out.
180  prev_best_epoch_index_ = model->best_model_epoch();
181  if (verbose_) {
182  cerr << " done." << endl;
183  }
184  }
185  private:
186  shared_ptr<ModelProtoWriter> writer_;
187  string model_file_;
188  bool compressed_;
189  bool use_base64_;
190  bool verbose_;
191  int prev_best_epoch_index_;
192 };
193 
194 } // namespace reranker
195 
196 #endif
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.
Definition: model.H:141
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...
Definition: factory.H:382
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.
Definition: model.H:223
Reranker model interface.