classias::classify::linear_multi< model_tmpl > Class Template Reference

Inheritance diagram for classias::classify::linear_multi< model_tmpl >:

classias::classify::linear_multi_logistic< model_tmpl >

Detailed Description

template<class model_tmpl>
class classias::classify::linear_multi< model_tmpl >

A template class for linear multi-class classifiers.

This template class implements a multi classifier with a linear discriminant model. The types of features, feature generators, and models are customizable. Any type that yields a feature weight with operator [] is acceptable for a model. A feature generator must convert a pair of an attribute and a label into a feature identifier that is compatible with the argument type of the operator [] of the model.

An instance is designed to have multiple candidates, and the candidate that yields the highest score is chosen as a classification result. The number of candidates must be set by resize() before computing candidate scores. Call set(), inner_product(), or scale() functions to compute the score of a candidate. After computing the score of all candidates, call finalize() function to determine the candidate with the highest score.

Parameters:
model_tmpl The type of a model (container of feature weights). Any type that yields a feature weight with operator [] is usable (e.g., arrays, std::vector, std::map).
See also:
thru_feature_generator_base, dense_feature_generator_base, sparse_feature_generator_base.


Public Types

typedef model_tmpl model_type
 The type of a model.
typedef model_type::value_type value_type
 The type of a feature weight.

Public Member Functions

 linear_multi (const model_type &model)
 Constructs an instance.
virtual ~linear_multi ()
 Destructs an object.
void clear ()
 Resets the classification result.
void resize (int n)
 Reserves the working space for storing n candidates.
int size () const
 Returns the number of candidates.
int argmax () const
 Returns the argmax index.
value_type score (int i)
 Returns the score of a candidate.
void scale (int i, const value_type &scale)
 Applies a scaling factor to a score.
template<class feature_generator_type>
void set (int i, feature_generator_type &fgen, const typename feature_generator_type::attribute_type &a, const typename feature_generator_type::label_type &l, const value_type &value)
 Sets an attribute for a candidate.
template<class feature_generator_type, class iterator_type>
void inner_product (int i, feature_generator_type &fgen, iterator_type first, iterator_type last, const typename feature_generator_type::label_type &l)
 Computes the inner product between an attribute vector and the model.
void finalize ()
 Finalize the classification.

Static Public Member Functions

static const char * name ()
 Returns the name of this classifier.

Protected Types

typedef std::vector< value_typescores_type
 The type representing an array of scores.

Protected Attributes

const model_typem_model
 The model.
scores_type m_scores
 The scores of labels.
int m_argmax
 The index of the label that gives the highest score.


Constructor & Destructor Documentation

template<class model_tmpl>
classias::classify::linear_multi< model_tmpl >::linear_multi ( const model_type model  )  [inline]

Constructs an instance.

Parameters:
model The model associated with the classifier.


Member Function Documentation

template<class model_tmpl>
void classias::classify::linear_multi< model_tmpl >::clear (  )  [inline]

Resets the classification result.

Call this function before sending a next classification instance.

Reimplemented in classias::classify::linear_multi_logistic< model_tmpl >.

template<class model_tmpl>
void classias::classify::linear_multi< model_tmpl >::resize ( int  n  )  [inline]

Reserves the working space for storing n candidates.

Call this function to allocate a working space for n candidates.

Parameters:
n The number of candidates.

template<class model_tmpl>
int classias::classify::linear_multi< model_tmpl >::size (  )  const [inline]

Returns the number of candidates.

Returns:
int The number of candidates.

template<class model_tmpl>
int classias::classify::linear_multi< model_tmpl >::argmax (  )  const [inline]

Returns the argmax index.

Returns:
int The index of the candidate that yields the highest score. Call finalize() before using this function.
See also:
finalize()

template<class model_tmpl>
value_type classias::classify::linear_multi< model_tmpl >::score ( int  i  )  [inline]

Returns the score of a candidate.

Parameters:
i The index for the candidate.
Returns:
value_type The score.

template<class model_tmpl>
void classias::classify::linear_multi< model_tmpl >::scale ( int  i,
const value_type scale 
) [inline]

Applies a scaling factor to a score.

Parameters:
i The index for the candidate.
scale The scaling factor.

template<class model_tmpl>
template<class feature_generator_type>
void classias::classify::linear_multi< model_tmpl >::set ( int  i,
feature_generator_type &  fgen,
const typename feature_generator_type::attribute_type &  a,
const typename feature_generator_type::label_type &  l,
const value_type value 
) [inline]

Sets an attribute for a candidate.

This function calls fgen.forward(a, l, f) to translate the pairs of the attribute (a) and label (l) to the feature (f), and adds (m_model[f] * value) to the score for the candidate i.

Parameters:
i The index for the candidate.
fgen The feature generator.
a The attribute identifier.
l The label for the candidate.
value The attribute value.

template<class model_tmpl>
template<class feature_generator_type, class iterator_type>
void classias::classify::linear_multi< model_tmpl >::inner_product ( int  i,
feature_generator_type &  fgen,
iterator_type  first,
iterator_type  last,
const typename feature_generator_type::label_type &  l 
) [inline]

Computes the inner product between an attribute vector and the model.

An attribute vector is represented by a range of iterators [first, last), whose element *it is compatible with std::pair. The member it->first presents an attribute identifier, and the member it->second presents the attribute value. The feature generator (fgen) is used to convert a pair of attribute (a) and label (l) into a feature identifier.

Parameters:
i The index for the candidate.
fgen The feature generator.
first The iterator for the first element of attributes.
last The iterator for the element just beyond the last element of attributes.
l The label for the candidate.

template<class model_tmpl>
void classias::classify::linear_multi< model_tmpl >::finalize (  )  [inline]

Finalize the classification.

Call this function before using argmax() function.

Reimplemented in classias::classify::linear_multi_logistic< model_tmpl >.

template<class model_tmpl>
static const char* classias::classify::linear_multi< model_tmpl >::name (  )  [inline, static]

Returns the name of this classifier.

Returns:
const char* The name of the classifier.

Reimplemented in classias::classify::linear_multi_logistic< model_tmpl >.


Copyright (c) 2002-2009 by Naoaki Okazaki
Mon Dec 28 23:41:06 2009