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< State > | createState () 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 Features & | features () 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< OutputType > | operator() (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 ¶meterDerivative, 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< Feature > | Features |
| typedef TypedFeatureNotAvailableException < Feature > | FeatureNotAvailableException |
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.
|
inline |
Definition at line 36 of file LinearClassifier.h.
|
inline |
Definition at line 72 of file LinearClassifier.h.
References m_means, numberOfClasses(), shark::blas::row(), and SIZE_CHECK.
|
virtual |
Configures the component given a property tree.
| [in] | node | The root of the property tree. |
Reimplemented from shark::IConfigurable.
Definition at line 51 of file LinearClassifier.cpp.
References setStructure().
|
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.
|
virtual |
Standard interface for evaluating the response of the model to a batch of patterns.
| patterns | the inputs of the model |
| outputs | the predictions or response of the model to every pattern |
Reimplemented from shark::AbstractModel< RealVector, unsigned int >.
Referenced by eval().
|
inlinevirtual |
Standard interface for evaluating the response of the model to a batch of patterns.
| patterns | the inputs of the model |
| outputs | the 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 | ) |
Definition at line 84 of file LinearClassifier.cpp.
References shark::decomposedGeneralInverse(), shark::fast_prod(), m_classBias, m_inverseCholesky, m_means, m_transformedMeans, shark::mean(), shark::blas::norm_sqr(), numberOfClasses(), and shark::blas::row().
Referenced by shark::LDA::train().
|
inline |
Definition at line 47 of file LinearClassifier.h.
References m_inverseCholesky.
Referenced by numberOfParameters(), and softMembership().
|
inline |
Definition at line 65 of file LinearClassifier.h.
References m_inverseCholesky.
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 40 of file LinearClassifier.h.
|
inline |
Definition at line 53 of file LinearClassifier.h.
References m_means.
Referenced by classMean(), importCovarianceMatrix(), numberOfParameters(), setClassMean(), setParameterVector(), and softMembership().
|
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().
|
inline |
Definition at line 50 of file LinearClassifier.h.
References m_means.
|
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().
|
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 | ||
| ) |
Definition at line 72 of file LinearClassifier.cpp.
References shark::fast_prod(), m_classBias, m_inverseCholesky, m_means, m_transformedMeans, shark::mean(), shark::blas::noalias(), shark::blas::norm_sqr(), numberOfClasses(), shark::blas::row(), SIZE_CHECK, and shark::blas::trans().
Referenced by shark::LDA::train().
|
inline |
Definition at line 62 of file LinearClassifier.h.
References m_inverseCholesky.
|
virtual |
Set the parameter vector.
Reimplemented from shark::IParameterizable.
Definition at line 62 of file LinearClassifier.cpp.
References shark::fast_prod(), shark::init(), m_classBias, m_inverseCholesky, m_means, m_transformedMeans, shark::mean(), shark::blas::norm_sqr(), numberOfClasses(), shark::blas::row(), and shark::toVector().
| 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().
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().
|
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.
|
protected |
Definition at line 34 of file LinearClassifier.h.
|
protected |
Definition at line 31 of file LinearClassifier.h.
Referenced by importCovarianceMatrix(), setClassMean(), setParameterVector(), setStructure(), and softMembership().
|
protected |
Definition at line 28 of file LinearClassifier.h.
Referenced by importCovarianceMatrix(), inputSize(), inverseCholeskyMatrix(), parameterVector(), read(), setClassMean(), setInverseCholeskyMatrix(), setParameterVector(), setStructure(), softMembership(), and write().
|
protected |
Definition at line 29 of file LinearClassifier.h.
Referenced by classMean(), importCovarianceMatrix(), numberOfClasses(), outputSize(), parameterVector(), read(), setClassMean(), setParameterVector(), setStructure(), and write().
|
protected |
Definition at line 30 of file LinearClassifier.h.
Referenced by importCovarianceMatrix(), setClassMean(), setParameterVector(), setStructure(), and softMembership().