ModErn Text Analysis
META Enumerates Textual Applications
linear_model.h
Go to the documentation of this file.
1 
9 #ifndef META_CLASSIFY_MODEL_LINEAR_MODEL_H_
10 #define META_CLASSIFY_MODEL_LINEAR_MODEL_H_
11 
12 #include <istream>
13 #include <ostream>
14 #include <unordered_map>
15 
16 #include "meta.h"
17 #include "util/sparse_vector.h"
18 
19 namespace meta
20 {
21 namespace classify
22 {
23 
27 class linear_model_exception : public std::runtime_error
28 {
29  public:
30  using std::runtime_error::runtime_error;
31 };
32 
37 template <class FeatureId, class FeatureValue, class ClassId>
39 {
40  public:
44  using feature_id = FeatureId;
45 
50  using feature_value = FeatureValue;
51 
55  using class_id = ClassId;
56 
62 
66  using weight_vectors = std::unordered_map<feature_id, weight_vector>;
67 
71  using scored_class = std::pair<class_id, feature_value>;
72 
76  using scored_classes = std::vector<scored_class>;
77 
82 
89  void load(std::istream& is);
90 
97  void save(std::ostream& os) const;
98 
110  template <class FeatureVector, class Filter>
111  class_id best_class(FeatureVector&& features, Filter&& filter) const;
112 
119  template <class FeatureVector>
120  class_id best_class(FeatureVector&& features) const;
121 
133  template <class FeatureVector, class Filter>
134  scored_classes best_classes(FeatureVector&& features, size_t num,
135  Filter&& filter) const;
136 
144  template <class FeatureVector>
145  scored_classes best_classes(FeatureVector&& features, size_t num) const;
146 
156  void update(const weight_vectors& updates, feature_value scale = 1);
157 
165  void update(const class_id& cid, const feature_id& fid,
166  feature_value delta);
167 
174  void condense(bool log = false);
175 
179  const weight_vectors& weights() const;
180 
181  private:
186 };
187 }
188 }
189 
191 #endif
double feature_value
The value type for features.
Definition: linear_model.h:50
Contains top-level namespace documentation for the META toolkit.
void update(const weight_vectors &updates, feature_value scale=1)
Updates all of the weights of this model by adding in the contribution from another set of weight vec...
Definition: linear_model.tcc:193
Exception thrown during interactions with linear_models.
Definition: linear_model.h:27
std::unordered_map< feature_id, weight_vector > weight_vectors
A collection of weight_vector by feature id.
Definition: linear_model.h:66
Represents a sparse vector, indexed by type Index and storing values of type Value.
Definition: sparse_vector.h:28
std::pair< class_id, feature_value > scored_class
A class_id with an associated score.
Definition: linear_model.h:71
label_id class_id
The ids for the classes.
Definition: linear_model.h:55
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
const weight_vectors & weights() const
Definition: linear_model.tcc:241
void save(std::ostream &os) const
Writes a linear model to a stream.
Definition: linear_model.tcc:57
std::vector< scored_class > scored_classes
A vector of class ids and associated scores.
Definition: linear_model.h:76
weight_vectors weights_
The weights for the model.
Definition: linear_model.h:185
A storage class for multiclass linear classifier models.
Definition: linear_model.h:38
scored_classes best_classes(FeatureVector &&features, size_t num, Filter &&filter) const
Determines the top classes for a given feature vector that satisfy the filter predicate.
void condense(bool log=false)
Removes all weights that have value 0.
Definition: linear_model.tcc:214
void load(std::istream &is)
Loads a linear model from a stream.
Definition: linear_model.tcc:21
feature_id feature_id
The identifier for features.
Definition: linear_model.h:44
class_id best_class(FeatureVector &&features, Filter &&filter) const
Determines the highest scoring class that satisfies a filter predicate for a given feature vector...