39 #include "../proto/dataio.h"
46 using confusion_learning::FeatureMessage;
47 using confusion_learning::FeatureVecMessage;
48 using confusion_learning::SymbolTableMessage;
49 using confusion_learning::SymbolMessage;
53 ModelMessage *model_message,
54 bool write_features)
const {
57 model_message->set_identifier(perceptron_model->
name());
61 model_message->set_model_spec(perceptron_model->
model_spec());
67 FeatureVecMessage *raw_feature_vector_message =
68 model_message->mutable_raw_parameters();
70 FeatureMessage::BASIC,
71 raw_feature_vector_message);
72 FeatureVecMessage *avg_feature_vector_message =
73 model_message->mutable_avg_parameters();
75 FeatureMessage::BASIC,
76 avg_feature_vector_message);
79 if (perceptron_model->
symbols_ != NULL) {
80 SymbolTableMessage *symbol_table_message =
81 model_message->mutable_symbols();
86 SymbolMessage *symbol_message = symbol_table_message->add_symbol();
87 symbol_message->set_symbol(it->first);
88 symbol_message->set_index(it->second);
97 bool output_best_epoch,
100 const string separator)
102 ConfusionProtoIO proto_writer;
111 FeatureMessage feature_message;
113 it != raw_weights.
end();
115 feature_message.Clear();
117 fv_writer_.SerializeFeature(it->first, weight * it->second,
118 FeatureMessage::BASIC,
122 if (!avg_weights.
empty()) {
123 feature_message.set_avg_value(weight * avg_weights.
GetValue(it->first));
125 string encoded_message;
126 proto_writer.EncodeBase64(feature_message, &encoded_message);
128 const string &feat_name = symbols->
GetSymbol(it->first);
129 if (feat_name ==
"") {
132 os << ss.str() << separator;
134 os << feat_name << separator;
137 os << encoded_message;
virtual const string & model_spec() const
Returns the spec string for constructing a default instance of this model so it may be properly de-se...
Model is an interface for reranking models.
const string & name() const
Returns the unique name for this model instance.
TrainingVectorSet best_models_
The best models seen so far during training, according to evaluation on the held-out development test...
Serializer for reranker::PerceptronModel instances to ModelMessage instances.
Symbols * symbols() const
Returns the symbol table for this model.
virtual const_iterator end()=0
#define REGISTER_MODEL_PROTO_WRITER(TYPE)
Registers the ModelProtoWriter implementation with the specified subtype TYPE with the ModelProtoWri...
TrainingVectorSet models_
The feature vectors representing this model.
virtual const string & proto_reader_spec() const
Returns the spec string for contructing an instance of a ModelProtoReader capable of de-serializing t...
const_iterator end() const
Returns a const iterator pointing to the end of the feature-value pairs of this feature vector...
const_iterator begin() const
Returns a const iterator pointing to the first of the feature-value pairs of this feature vector...
virtual const string & GetSymbol(int index) const =0
Returns the unique symbol for the specified index, or the empty string if the specified index does no...
This class implements a perceptron model reranker.
virtual void WriteFeatures(const Model *model, ostream &os, bool output_best_epoch, double weight, bool output_key, const string separator) const
Writes out the features of this model to a series of FeatureMessage instances using the specified Con...
int num_training_errors() const
Returns the number of training errors made by this model.
Symbols * symbols_
The symbol table for this model (may be NULL).
virtual int best_model_epoch() const
Returns the epoch of the best models seen so far during training.
unordered_map< string, int >::const_iterator const_iterator
const FeatureVector< int, double > & average_weights() const
Returns the feature vector corresponding to the averaged perceptron.
An interface specifying a converter from symbols (strings) to int indices.
const FeatureVector< int, double > & weights() const
Returns the "raw" feature weights computed during training.
A class to construct a ModelMessage from a PerceptronModel instance.
V GetValue(const K &uid) const
Synonymous with GetWeight.
virtual const_iterator begin()=0