shark::LinearModel< InputType > Class Template Reference

Linear Prediction. More...

#include <shark/Models/LinearModel.h>

+ Inheritance diagram for shark::LinearModel< InputType >:

Public Types

typedef base_type::BatchInputType BatchInputType
 
typedef base_type::BatchOutputType BatchOutputType
 
- Public Types inherited from shark::AbstractModel< InputType, RealVector >
enum  Feature
 
typedef InputType InputType
 Defines the input type of the model. More...
 
typedef RealVector 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< FeatureFeatureNotAvailableException
 

Public Member Functions

 LinearModel ()
 CDefault Constructor; use setStructure later. More...
 
 LinearModel (std::size_t inputs, std::size_t outputs=1, bool offset=false)
 Constructor creating a model with given dimnsionalities and optional offset term. More...
 
 LinearModel (LinearModel const &model)
 copy constructor More...
 
std::string name () const
 From INameable: return the class name. More...
 
LinearModeloperator= (LinearModel const &model)
 operator = More...
 
 LinearModel (RealMatrix const &matrix, RealVector const &offset=RealVector())
 Construction from matrix (and vector) More...
 
bool hasOffset () const
 check for the presence of an offset term More...
 
size_t inputSize () const
 obtain the input dimension More...
 
size_t outputSize () const
 obtain the output dimension More...
 
RealVector parameterVector () const
 obtain the parameter vector More...
 
void setParameterVector (RealVector const &newParameters)
 overwrite the parameter vector More...
 
size_t numberOfParameters () const
 return the number of parameter More...
 
void setStructure (std::size_t inputs, std::size_t outputs=1, bool offset=false)
 overwrite structure and parameters More...
 
void setStructure (RealMatrix const &matrix, RealVector const &offset=RealVector())
 overwrite structure and parameters More...
 
RealMatrix const & matrix () const
 return a copy of the matrix in dense format More...
 
RealMatrix & matrix ()
 
RealVector const & offset () const
 return the offset More...
 
RealVector & offset ()
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &inputs, BatchOutputType &outputs) const
 Evaluate the model: output = matrix * input + offset. More...
 
void eval (BatchInputType const &inputs, BatchOutputType &outputs, State &state) const
 Evaluate the model: output = matrix * input + offset. More...
 
void weightedParameterDerivative (BatchInputType const &patterns, RealMatrix const &coefficients, State const &state, RealVector &gradient) const
 Calculates the first derivative w.r.t the parameters and summing them up over all patterns of the last computed batch. More...
 
void weightedInputDerivative (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const
 Calculates the first derivative w.r.t the inputs and summs them up over all patterns of the last computed batch. More...
 
void read (InArchive &archive)
 From ISerializable. More...
 
void write (OutArchive &archive) const
 From ISerializable. More...
 
- Public Member Functions inherited from shark::AbstractModel< InputType, RealVector >
 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, 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::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 ()
 

Friends

void swap (LinearModel &model1, LinearModel &model2)
 swap More...
 

Additional Inherited Members

- Protected Attributes inherited from shark::AbstractModel< InputType, RealVector >
Features m_features
 

Detailed Description

template<class InputType = RealVector>
class shark::LinearModel< InputType >

Linear Prediction.

A linear model makes predictions according to \( y = f(x) = A x + b \). There are two important special cases: The output may be a single number, and the offset term b may be dropped.

The class allows for dense and sparse input vector types. However it assumes that the weight matrix and the ouputs are dense. There are some cases where this is not good behavior. Check for example Normalizer for a class which is designed for sparse inputs and outputs.

Definition at line 53 of file LinearModel.h.

Member Typedef Documentation

§ BatchInputType

template<class InputType = RealVector>
typedef base_type::BatchInputType shark::LinearModel< InputType >::BatchInputType

Definition at line 62 of file LinearModel.h.

§ BatchOutputType

template<class InputType = RealVector>
typedef base_type::BatchOutputType shark::LinearModel< InputType >::BatchOutputType

Definition at line 63 of file LinearModel.h.

Constructor & Destructor Documentation

§ LinearModel() [1/4]

template<class InputType = RealVector>
shark::LinearModel< InputType >::LinearModel ( )
inline

CDefault Constructor; use setStructure later.

Definition at line 66 of file LinearModel.h.

§ LinearModel() [2/4]

template<class InputType = RealVector>
shark::LinearModel< InputType >::LinearModel ( std::size_t  inputs,
std::size_t  outputs = 1,
bool  offset = false 
)
inline

Constructor creating a model with given dimnsionalities and optional offset term.

Definition at line 71 of file LinearModel.h.

§ LinearModel() [3/4]

template<class InputType = RealVector>
shark::LinearModel< InputType >::LinearModel ( LinearModel< InputType > const &  model)
inline

copy constructor

Definition at line 77 of file LinearModel.h.

§ LinearModel() [4/4]

template<class InputType = RealVector>
shark::LinearModel< InputType >::LinearModel ( RealMatrix const &  matrix,
RealVector const &  offset = RealVector() 
)
inline

Construction from matrix (and vector)

Definition at line 101 of file LinearModel.h.

Member Function Documentation

§ createState()

template<class InputType = RealVector>
boost::shared_ptr<State> shark::LinearModel< InputType >::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< InputType, RealVector >.

Definition at line 170 of file LinearModel.h.

§ eval() [1/2]

template<class InputType = RealVector>
void shark::LinearModel< InputType >::eval ( BatchInputType const &  inputs,
BatchOutputType outputs 
) const
inlinevirtual

Evaluate the model: output = matrix * input + offset.

Reimplemented from shark::AbstractModel< InputType, RealVector >.

Definition at line 177 of file LinearModel.h.

Referenced by shark::LinearModel< VectorType >::eval().

§ eval() [2/2]

template<class InputType = RealVector>
void shark::LinearModel< InputType >::eval ( BatchInputType const &  inputs,
BatchOutputType outputs,
State state 
) const
inlinevirtual

Evaluate the model: output = matrix * input + offset.

Implements shark::AbstractModel< InputType, RealVector >.

Definition at line 186 of file LinearModel.h.

§ hasOffset()

template<class InputType = RealVector>
bool shark::LinearModel< InputType >::hasOffset ( ) const
inline

check for the presence of an offset term

Definition at line 108 of file LinearModel.h.

Referenced by shark::LinearModel< VectorType >::eval(), and shark::LinearModel< VectorType >::weightedParameterDerivative().

§ inputSize()

template<class InputType = RealVector>
size_t shark::LinearModel< InputType >::inputSize ( ) const
inline

§ matrix() [1/2]

template<class InputType = RealVector>
RealMatrix const& shark::LinearModel< InputType >::matrix ( ) const
inline

return a copy of the matrix in dense format

Definition at line 154 of file LinearModel.h.

Referenced by main(), and shark::LinearModel< VectorType >::setStructure().

§ matrix() [2/2]

template<class InputType = RealVector>
RealMatrix& shark::LinearModel< InputType >::matrix ( )
inline

Definition at line 158 of file LinearModel.h.

§ name()

template<class InputType = RealVector>
std::string shark::LinearModel< InputType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 84 of file LinearModel.h.

§ numberOfParameters()

template<class InputType = RealVector>
size_t shark::LinearModel< InputType >::numberOfParameters ( ) const
inlinevirtual

return the number of parameter

Reimplemented from shark::IParameterizable.

Definition at line 137 of file LinearModel.h.

Referenced by shark::LinearModel< VectorType >::parameterVector(), and shark::LinearModel< VectorType >::weightedParameterDerivative().

§ offset() [1/2]

template<class InputType = RealVector>
RealVector const& shark::LinearModel< InputType >::offset ( ) const
inline

return the offset

Definition at line 163 of file LinearModel.h.

Referenced by main(), and shark::LinearModel< VectorType >::setStructure().

§ offset() [2/2]

template<class InputType = RealVector>
RealVector& shark::LinearModel< InputType >::offset ( )
inline

Definition at line 166 of file LinearModel.h.

§ operator=()

template<class InputType = RealVector>
LinearModel& shark::LinearModel< InputType >::operator= ( LinearModel< InputType > const &  model)
inline

operator =

Definition at line 94 of file LinearModel.h.

§ outputSize()

template<class InputType = RealVector>
size_t shark::LinearModel< InputType >::outputSize ( ) const
inline

§ parameterVector()

template<class InputType = RealVector>
RealVector shark::LinearModel< InputType >::parameterVector ( ) const
inlinevirtual

obtain the parameter vector

Reimplemented from shark::IParameterizable.

Definition at line 123 of file LinearModel.h.

§ read()

template<class InputType = RealVector>
void shark::LinearModel< InputType >::read ( InArchive archive)
inlinevirtual

From ISerializable.

Reimplemented from shark::AbstractModel< InputType, RealVector >.

Definition at line 226 of file LinearModel.h.

§ setParameterVector()

template<class InputType = RealVector>
void shark::LinearModel< InputType >::setParameterVector ( RealVector const &  newParameters)
inlinevirtual

overwrite the parameter vector

Reimplemented from shark::IParameterizable.

Definition at line 131 of file LinearModel.h.

Referenced by main().

§ setStructure() [1/2]

template<class InputType = RealVector>
void shark::LinearModel< InputType >::setStructure ( std::size_t  inputs,
std::size_t  outputs = 1,
bool  offset = false 
)
inline

§ setStructure() [2/2]

template<class InputType = RealVector>
void shark::LinearModel< InputType >::setStructure ( RealMatrix const &  matrix,
RealVector const &  offset = RealVector() 
)
inline

overwrite structure and parameters

Definition at line 148 of file LinearModel.h.

§ weightedInputDerivative()

template<class InputType = RealVector>
void shark::LinearModel< InputType >::weightedInputDerivative ( BatchInputType const &  patterns,
BatchOutputType const &  coefficients,
State const &  state,
BatchInputType derivative 
) const
inlinevirtual

Calculates the first derivative w.r.t the inputs and summs them up over all patterns of the last computed batch.

Reimplemented from shark::AbstractModel< InputType, RealVector >.

Definition at line 212 of file LinearModel.h.

§ weightedParameterDerivative()

template<class InputType = RealVector>
void shark::LinearModel< InputType >::weightedParameterDerivative ( BatchInputType const &  patterns,
RealMatrix const &  coefficients,
State const &  state,
RealVector &  gradient 
) const
inline

Calculates the first derivative w.r.t the parameters and summing them up over all patterns of the last computed batch.

Definition at line 191 of file LinearModel.h.

§ write()

template<class InputType = RealVector>
void shark::LinearModel< InputType >::write ( OutArchive archive) const
inlinevirtual

From ISerializable.

Reimplemented from shark::AbstractModel< InputType, RealVector >.

Definition at line 231 of file LinearModel.h.

Friends And Related Function Documentation

§ swap

template<class InputType = RealVector>
void swap ( LinearModel< InputType > &  model1,
LinearModel< InputType > &  model2 
)
friend

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