ModErn Text Analysis
META Enumerates Textual Applications
Public Types | Public Member Functions | Private Attributes | List of all members
meta::classify::linear_model< FeatureId, FeatureValue, ClassId > Class Template Reference

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_vectorsweights () 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.
 

Detailed Description

template<class FeatureId, class FeatureValue, class ClassId>
class meta::classify::linear_model< FeatureId, FeatureValue, ClassId >

A storage class for multiclass linear classifier models.

This class should be used to store classifiers that fit in memory.

Member Typedef Documentation

template<class FeatureId, class FeatureValue, class ClassId>
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.

template<class FeatureId, class FeatureValue, class ClassId>
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.

Member Function Documentation

template<class FeatureId , class FeatureValue , class ClassId >
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.

Parameters
isThe stream to read from
template<class FeatureId , class FeatureValue , class ClassId >
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.

Parameters
osThe stream to write to
template<class FeatureId, class FeatureValue, class ClassId>
template<class FeatureVector , class Filter >
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.

Parameters
featuresThe feature vector to score
filterThe filter predicate to use
Returns
the highest scoring class that passes the filter predicate
template<class FeatureId, class FeatureValue, class ClassId>
template<class FeatureVector >
class_id meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::best_class ( FeatureVector &&  features) const

Determines the highest scoring class for a given feature vector.

Parameters
featuresThe feature vector to score
Returns
the highest scoring class
template<class FeatureId, class FeatureValue, class ClassId>
template<class FeatureVector , class Filter >
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.

Parameters
featuresThe feature vector to score
numThe number of classes to return ( \(k\))
filterThe filter predicate
Returns
a vector of (up to) \(k\) scored classes sorted by score
template<class FeatureId, class FeatureValue, class ClassId>
template<class FeatureVector >
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.

Parameters
featuresThe feature vector to score
numThe number of classes to return ( \(k\))
Returns
a vector of (up to) \(k\) scored classes sorted by score
template<class FeatureId , class FeatureValue , class ClassId >
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).

Parameters
updatesThe weight vector containing the updates
scaleThe scalar to multiply everything in the update by before adding it to the model
template<class FeatureId , class FeatureValue , class ClassId >
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.

Parameters
cidThe class
fidThe feature
deltaThe amount to be added to that weight
template<class FeatureId , class FeatureValue , class ClassId >
void meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::condense ( bool  log = false)

Removes all weights that have value 0.

Parameters
logWhether or not to log statistics about the condensed weight vector (info level)
template<class FeatureId , class FeatureValue , class ClassId >
auto meta::classify::linear_model< FeatureId, FeatureValue, ClassId >::weights ( ) const
Returns
the weights

The documentation for this class was generated from the following files: