ModErn Text Analysis
META Enumerates Textual Applications
|
A storage class for multiclass linear classifier models. More...
#include <linear_model.h>
Public Types | |
using | feature_id = FeatureId |
The identifier for features. | |
using | feature_value = FeatureValue |
The value type for features. More... | |
using | class_id = ClassId |
The ids for the classes. | |
using | weight_vector = util::sparse_vector< class_id, feature_value > |
A single weight vector for a specific feature id. More... | |
using | weight_vectors = std::unordered_map< feature_id, weight_vector > |
A collection of weight_vector by feature id. | |
using | scored_class = std::pair< class_id, feature_value > |
A class_id with an associated score. | |
using | scored_classes = std::vector< scored_class > |
A vector of class ids and associated scores. | |
using | exception = linear_model_exception |
The exception thrown during interactions with linear_models. | |
Public Member Functions | |
void | load (std::istream &is) |
Loads a linear model from a stream. More... | |
void | save (std::ostream &os) const |
Writes a linear model to a stream. More... | |
template<class FeatureVector , class Filter > | |
class_id | best_class (FeatureVector &&features, Filter &&filter) const |
Determines the highest scoring class that satisfies a filter predicate for a given feature vector. More... | |
template<class FeatureVector > | |
class_id | best_class (FeatureVector &&features) const |
Determines the highest scoring class for a given feature vector. More... | |
template<class FeatureVector , class Filter > | |
scored_classes | best_classes (FeatureVector &&features, size_t num, Filter &&filter) const |
Determines the top \(k\) classes for a given feature vector that satisfy the filter predicate. More... | |
template<class FeatureVector > | |
scored_classes | best_classes (FeatureVector &&features, size_t num) const |
Determines the top \(k\) classes for a given feature vector. More... | |
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 vectors, multiplied by a scalar value (default 1). More... | |
void | update (const class_id &cid, const feature_id &fid, feature_value delta) |
Updates a single weight in the model. More... | |
void | condense (bool log=false) |
Removes all weights that have value 0. More... | |
const weight_vectors & | weights () const |
template<class FeatureVector , class Filter > | |
auto | best_class (FeatureVector &&features, Filter &&filter) const -> class_id |
template<class FeatureVector > | |
auto | best_class (FeatureVector &&features) const -> class_id |
template<class FeatureVector , class Filter > | |
auto | best_classes (FeatureVector &&features, size_t num, Filter &&filter) const -> scored_classes |
template<class FeatureVector > | |
auto | best_classes (FeatureVector &&features, size_t num) const -> scored_classes |
Private Attributes | |
weight_vectors | weights_ |
The weights for the model. | |
A storage class for multiclass linear classifier models.
This class should be used to store classifiers that fit in memory.
using meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::feature_value = FeatureValue |
The value type for features.
Typically this is a double or a float, but it could in principle be any numeric type.
using meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::weight_vector = util::sparse_vector<class_id, feature_value> |
A single weight vector for a specific feature id.
This stores the weights that a given feature has for each class.
void meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::load | ( | std::istream & | is | ) |
Loads a linear model from a stream.
If the stream is from a file, we are assuming that it has been opened in binary mode.
is | The stream to read from |
void meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::save | ( | std::ostream & | os | ) | const |
Writes a linear model to a stream.
If the stream is from a file,w e are assuming it has been opened in binary mode.
os | The stream to write to |
class_id meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::best_class | ( | FeatureVector && | features, |
Filter && | filter | ||
) | const |
Determines the highest scoring class that satisfies a filter predicate for a given feature vector.
Any class id that does not return true when passed to the filter will not be considered. If no classes satisfy the filter predicate, the default class id will be returned.
features | The feature vector to score |
filter | The filter predicate to use |
class_id meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::best_class | ( | FeatureVector && | features | ) | const |
Determines the highest scoring class for a given feature vector.
features | The feature vector to score |
scored_classes meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::best_classes | ( | FeatureVector && | features, |
size_t | num, | ||
Filter && | filter | ||
) | const |
Determines the top \(k\) classes for a given feature vector that satisfy the filter predicate.
Any class id that does not return true when passed to the filter will not be considered. If no classes satisfy the filter predicate, the returned vector will be empty.
features | The feature vector to score |
num | The number of classes to return ( \(k\)) |
filter | The filter predicate |
scored_classes meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::best_classes | ( | FeatureVector && | features, |
size_t | num | ||
) | const |
Determines the top \(k\) classes for a given feature vector.
features | The feature vector to score |
num | The number of classes to return ( \(k\)) |
void meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::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 vectors, multiplied by a scalar value (default 1).
updates | The weight vector containing the updates |
scale | The scalar to multiply everything in the update by before adding it to the model |
void meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::update | ( | const class_id & | cid, |
const feature_id & | fid, | ||
feature_value | delta | ||
) |
Updates a single weight in the model.
cid | The class |
fid | The feature |
delta | The amount to be added to that weight |
void meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::condense | ( | bool | log = false | ) |
Removes all weights that have value 0.
log | Whether or not to log statistics about the condensed weight vector (info level) |
auto meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::weights | ( | ) | const |