shark::LinearClassifier Class Reference

Basic linear classifier. More...

#include <shark/Models/LinearClassifier.h>

+ Inheritance diagram for shark::LinearClassifier:
+ Collaboration diagram for shark::LinearClassifier:

Public Member Functions

 LinearClassifier ()
 
std::string name () const
 From INameable: return the class name. More...
 
void setStructure (std::size_t inputSize, std::size_t classes)
 
void configure (PropertyTree const &node)
 Configures the component given a property tree. More...
 
std::size_t inputSize () const
 
std::size_t outputSize () const
 
std::size_t numberOfClasses () const
 
RealVector parameterVector () const
 Return the parameter vector. More...
 
void setParameterVector (RealVector const &newParameters)
 Set the parameter vector. More...
 
std::size_t numberOfParameters () const
 Return the number of parameters. More...
 
void setInverseCholeskyMatrix (const RealMatrix &matrix)
 
const RealMatrix & inverseCholeskyMatrix () const
 
void importCovarianceMatrix (RealMatrix const &covariance)
 
void setClassMean (std::size_t classID, RealVector const &mean)
 
ConstRealMatrixRow classMean (std::size_t classID) const
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &patterns, BatchOutputType &outputs) const
 Standard interface for evaluating the response of the model to a batch of patterns. More...
 
void eval (BatchInputType const &patterns, BatchOutputType &outputs, State &state) const
 Standard interface for evaluating the response of the model to a batch of patterns. More...
 
void read (InArchive &archive)
 From ISerializable, reads a model from an archive. More...
 
void write (OutArchive &archive) const
 From ISerializable, writes a model to an archive. More...
 
Data< RealVector > softMembership (Data< InputType > const &patterns) const
 Return memberships to the different classes, the memberships sum to unity. More...
 
void softMembership (BatchInputType const &patterns, Batch< RealVector >::type &output) const
 Compute memberships to the different classes, the memberships sum to unity. More...
 
- Public Member Functions inherited from shark::AbstractModel< RealVector, unsigned int >
 AbstractModel ()
 
virtual ~AbstractModel ()
 
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasFirstParameterDerivative () const
 Returns true when the first parameter derivative is implemented. More...
 
bool hasSecondParameterDerivative () const
 Returns true when the second parameter derivative is implemented. More...
 
bool hasFirstInputDerivative () const
 Returns true when the first input derivative is implemented. More...
 
bool hasSecondInputDerivative () const
 Returns true when the second parameter derivative is implemented. More...
 
bool isSequential () const
 
virtual void eval (InputType const &pattern, OutputType &output) const
 Standard interface for evaluating the response of the model to a single pattern. More...
 
Data< OutputTypeoperator() (Data< InputType > const &patterns) const
 Model evaluation as an operator for a whole dataset. This is a convenience function. More...
 
OutputType operator() (InputType const &pattern) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
BatchOutputType operator() (BatchInputType const &patterns) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
virtual void weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, RealVector &derivative) const
 calculates the weighted sum of derivatives w.r.t the parameters. More...
 
virtual void weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, Batch< RealMatrix >::type const &errorHessian, State const &state, RealVector &derivative, RealMatrix &hessian) const
 calculates the weighted sum of derivatives w.r.t the parameters More...
 
virtual void weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const
 calculates the weighted sum of derivatives w.r.t the inputs More...
 
virtual void weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, typename Batch< RealMatrix >::type const &errorHessian, State const &state, RealMatrix &derivative, Batch< RealMatrix >::type &hessian) const
 calculates the weighted sum of derivatives w.r.t the inputs More...
 
virtual void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, RealVector &parameterDerivative, BatchInputType &inputDerivative) const
 calculates weighted input and parameter derivative at the same time More...
 
- Public Member Functions inherited from shark::IParameterizable
virtual ~IParameterizable ()
 
- Public Member Functions inherited from shark::IConfigurable
virtual ~IConfigurable ()
 Virtual d'tor. More...
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor. More...
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...). More...
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...). More...
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Attributes

RealMatrix m_inverseCholesky
 
RealMatrix m_means
 
RealMatrix m_transformedMeans
 
RealVector m_classBias
 
bool m_changed
 
- Protected Attributes inherited from shark::AbstractModel< RealVector, unsigned int >
Features m_features
 

Additional Inherited Members

- Public Types inherited from shark::AbstractModel< RealVector, unsigned int >
enum  Feature
 
typedef RealVector InputType
 Defines the input type of the model. More...
 
typedef unsigned int OutputType
 Defines the output type of the model. More...
 
typedef Batch< InputType >::type BatchInputType
 defines the batch type of the input type. More...
 
typedef Batch< OutputType >::type BatchOutputType
 defines the batch type of the output type More...
 
typedef TypedFlags< FeatureFeatures
 
typedef
TypedFeatureNotAvailableException
< Feature
FeatureNotAvailableException
 

Detailed Description

Basic linear classifier.

The LinearClassifier class is a multi class classifier model suited for linear discriminant analysis. For c classes \( 0, \dots, c-1 \) the model holds class mean vectors \( m_c \) and a shared data scatter matrix \( M \). It predicts the class of a vector x according to the rule \( \textrm{argmin}_{c} (x - m_c)^T M^{-1} (x - m_c) \). The output is an integer in the range [0,c-1] telling to which class the point belongs

As default, this class uses the class means and the inverse covariance matrix. So these are also the parameters the vector returned by parameterVector consists of. However there exists a convenience function importCovarianceMatrix which automatically inverts the covariance matrix for use of the class. Be warned, that this may take a lot of time

Definition at line 25 of file LinearClassifier.h.

Constructor & Destructor Documentation

shark::LinearClassifier::LinearClassifier ( )
inline

Definition at line 36 of file LinearClassifier.h.

Member Function Documentation

ConstRealMatrixRow shark::LinearClassifier::classMean ( std::size_t  classID) const
inline

Definition at line 72 of file LinearClassifier.h.

References m_means, numberOfClasses(), shark::blas::row(), and SIZE_CHECK.

void LinearClassifier::configure ( PropertyTree const &  node)
virtual

Configures the component given a property tree.

Parameters
[in]nodeThe root of the property tree.

Reimplemented from shark::IConfigurable.

Definition at line 51 of file LinearClassifier.cpp.

References setStructure().

boost::shared_ptr<State> shark::LinearClassifier::createState ( ) const
inlinevirtual

Creates an internal state of the model.

The state is needed when the derivatives are to be calculated. Eval can store a state which is then reused to speed up the calculations of the derivatives. This also allows eval to be evaluated in parallel!

Reimplemented from shark::AbstractModel< RealVector, unsigned int >.

Definition at line 77 of file LinearClassifier.h.

void shark::LinearClassifier::eval ( BatchInputType const &  patterns,
BatchOutputType outputs 
) const
virtual

Standard interface for evaluating the response of the model to a batch of patterns.

Parameters
patternsthe inputs of the model
outputsthe predictions or response of the model to every pattern

Reimplemented from shark::AbstractModel< RealVector, unsigned int >.

Referenced by eval().

void shark::LinearClassifier::eval ( BatchInputType const &  patterns,
BatchOutputType outputs,
State state 
) const
inlinevirtual

Standard interface for evaluating the response of the model to a batch of patterns.

Parameters
patternsthe inputs of the model
outputsthe predictions or response of the model to every pattern

Implements shark::AbstractModel< RealVector, unsigned int >.

Definition at line 82 of file LinearClassifier.h.

References eval().

void LinearClassifier::importCovarianceMatrix ( RealMatrix const &  covariance)
std::size_t shark::LinearClassifier::inputSize ( ) const
inline

Definition at line 47 of file LinearClassifier.h.

References m_inverseCholesky.

Referenced by numberOfParameters(), and softMembership().

const RealMatrix& shark::LinearClassifier::inverseCholeskyMatrix ( ) const
inline

Definition at line 65 of file LinearClassifier.h.

References m_inverseCholesky.

std::string shark::LinearClassifier::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 40 of file LinearClassifier.h.

std::size_t shark::LinearClassifier::numberOfClasses ( ) const
inline
std::size_t LinearClassifier::numberOfParameters ( ) const
virtual

Return the number of parameters.

Reimplemented from shark::IParameterizable.

Definition at line 79 of file LinearClassifier.cpp.

References inputSize(), and numberOfClasses().

Referenced by parameterVector().

std::size_t shark::LinearClassifier::outputSize ( ) const
inline

Definition at line 50 of file LinearClassifier.h.

References m_means.

RealVector LinearClassifier::parameterVector ( ) const
virtual

Return the parameter vector.

Reimplemented from shark::IParameterizable.

Definition at line 57 of file LinearClassifier.cpp.

References shark::init(), m_inverseCholesky, m_means, numberOfParameters(), shark::parameters(), and shark::toVector().

void LinearClassifier::read ( InArchive archive)
virtual

From ISerializable, reads a model from an archive.

Reimplemented from shark::AbstractModel< RealVector, unsigned int >.

Definition at line 136 of file LinearClassifier.cpp.

References m_inverseCholesky, and m_means.

void LinearClassifier::setClassMean ( std::size_t  classID,
RealVector const &  mean 
)
void shark::LinearClassifier::setInverseCholeskyMatrix ( const RealMatrix &  matrix)
inline

Definition at line 62 of file LinearClassifier.h.

References m_inverseCholesky.

void LinearClassifier::setParameterVector ( RealVector const &  newParameters)
virtual
void LinearClassifier::setStructure ( std::size_t  inputSize,
std::size_t  classes 
)

Definition at line 44 of file LinearClassifier.cpp.

References m_classBias, m_inverseCholesky, m_means, and m_transformedMeans.

Referenced by configure(), and shark::LDA::train().

Data< RealVector > LinearClassifier::softMembership ( Data< InputType > const &  patterns) const

Return memberships to the different classes, the memberships sum to unity.

Definition at line 148 of file LinearClassifier.cpp.

References shark::Data< Type >::batch(), and shark::Data< Type >::numberOfBatches().

void LinearClassifier::softMembership ( BatchInputType const &  patterns,
Batch< RealVector >::type &  output 
) const

Compute memberships to the different classes, the memberships sum to unity.

Definition at line 156 of file LinearClassifier.cpp.

References shark::fast_prod(), shark::blas::inner_prod(), inputSize(), m_classBias, m_inverseCholesky, m_transformedMeans, shark::blas::norm_sqr(), numberOfClasses(), shark::blas::row(), and shark::blas::sum().

void LinearClassifier::write ( OutArchive archive) const
virtual

From ISerializable, writes a model to an archive.

Reimplemented from shark::AbstractModel< RealVector, unsigned int >.

Definition at line 142 of file LinearClassifier.cpp.

References m_inverseCholesky, and m_means.

Member Data Documentation

bool shark::LinearClassifier::m_changed
protected

Definition at line 34 of file LinearClassifier.h.

RealVector shark::LinearClassifier::m_classBias
protected
RealMatrix shark::LinearClassifier::m_inverseCholesky
protected
RealMatrix shark::LinearClassifier::m_means
protected
RealMatrix shark::LinearClassifier::m_transformedMeans
protected

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