shark::LinearModel< InputType, OutputType > Class Template Reference

Linear Prediction. More...

#include <shark/Models/LinearModel.h>

+ Inheritance diagram for shark::LinearModel< InputType, OutputType >:
+ Collaboration diagram for shark::LinearModel< InputType, OutputType >:

Public Types

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

Public Member Functions

 LinearModel ()
 Constructor of an invalid model; use setStructure later. More...
 
 LinearModel (const self_type &model)
 copy constructor More...
 
std::string name () const
 From INameable: return the class name. More...
 
const self_type operator= (const self_type &model)
 operator = More...
 
 LinearModel (unsigned int inputs, unsigned int outputs=1, bool offset=false, bool sparse=false)
 Constructor. More...
 
 LinearModel (RealMatrix const &matrix)
 Construction from matrix. More...
 
 LinearModel (RealMatrix const &matrix, OutputType offset)
 Construction from matrix and vector. More...
 
 LinearModel (CompressedRealMatrix const &matrix)
 Construction from matrix. More...
 
 LinearModel (CompressedRealMatrix const &matrix, RealVector offset)
 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 (RealMatrix const &matrix)
 overwrite structure and parameters More...
 
void setStructure (unsigned int inputs, unsigned int outputs=1, bool offset=false, bool sparse=false)
 overwrite structure and parameters More...
 
void setStructure (RealMatrix const &matrix, const RealVector &offset)
 overwrite structure and parameters More...
 
void setStructure (CompressedRealMatrix const &matrix)
 overwrite structure and parameters More...
 
void setStructure (CompressedRealMatrix const &matrix, const RealVector &offset)
 overwrite structure and parameters More...
 
RealMatrix matrix () const
 return a copy of the matrix in dense format More...
 
OutputType const & offset () const
 return the offset More...
 
RealVector matrixRow (size_t index) const
 return a copy of a row of the matrix in dense format More...
 
RealVector matrixColumn (size_t index) const
 return a copy of a column of the matrix in dense format More...
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &input, BatchOutputType &output) const
 Evaluate the model: output = matrix * input + offset. More...
 
void eval (BatchInputType const &input, BatchOutputType &output, State &state) const
 Evaluate the model: output = matrix * input + offset. More...
 
void weightedParameterDerivative (BatchInputType const &pattern, 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 weightedParameterDerivative (BatchInputType const &patterns, 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...
 
void read (InArchive &archive)
 From ISerializable. More...
 
void write (OutArchive &archive) const
 From ISerializable. More...
 
- Public Member Functions inherited from shark::AbstractModel< InputType, OutputType >
 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 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...
 
virtual void configure (const PropertyTree &node)
 Configures the component given a property tree. 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 ()
 

Friends

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

Additional Inherited Members

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

Detailed Description

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

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.
Under the hood the class allows for dense and sparse representations of the matrix A. This is achieved by means of a type erasure.

Definition at line 57 of file LinearModel.h.

Member Typedef Documentation

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

Definition at line 66 of file LinearModel.h.

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

Definition at line 67 of file LinearModel.h.

Constructor & Destructor Documentation

template<class InputType = RealVector, class OutputType = InputType>
shark::LinearModel< InputType, OutputType >::LinearModel ( )
inline
template<class InputType = RealVector, class OutputType = InputType>
shark::LinearModel< InputType, OutputType >::LinearModel ( unsigned int  inputs,
unsigned int  outputs = 1,
bool  offset = false,
bool  sparse = false 
)
inline
template<class InputType = RealVector, class OutputType = InputType>
shark::LinearModel< InputType, OutputType >::LinearModel ( RealMatrix const &  matrix)
inline
template<class InputType = RealVector, class OutputType = InputType>
shark::LinearModel< InputType, OutputType >::LinearModel ( RealMatrix const &  matrix,
OutputType  offset 
)
inline
template<class InputType = RealVector, class OutputType = InputType>
shark::LinearModel< InputType, OutputType >::LinearModel ( CompressedRealMatrix const &  matrix)
inline
template<class InputType = RealVector, class OutputType = InputType>
shark::LinearModel< InputType, OutputType >::LinearModel ( CompressedRealMatrix const &  matrix,
RealVector  offset 
)
inline

Member Function Documentation

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

Definition at line 228 of file LinearModel.h.

References SHARK_CHECK.

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

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

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

Definition at line 236 of file LinearModel.h.

References SHARK_CHECK.

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

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

Implements shark::AbstractModel< InputType, OutputType >.

Definition at line 241 of file LinearModel.h.

References SHARK_CHECK.

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

return a copy of the matrix in dense format

Definition at line 201 of file LinearModel.h.

References SHARK_CHECK.

Referenced by main().

template<class InputType = RealVector, class OutputType = InputType>
RealVector shark::LinearModel< InputType, OutputType >::matrixColumn ( size_t  index) const
inline

return a copy of a column of the matrix in dense format

Definition at line 221 of file LinearModel.h.

References SHARK_CHECK.

template<class InputType = RealVector, class OutputType = InputType>
RealVector shark::LinearModel< InputType, OutputType >::matrixRow ( size_t  index) const
inline

return a copy of a row of the matrix in dense format

Definition at line 213 of file LinearModel.h.

References SHARK_CHECK.

Referenced by main().

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

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 81 of file LinearModel.h.

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

return the number of parameter

Reimplemented from shark::IParameterizable.

Definition at line 166 of file LinearModel.h.

References SHARK_CHECK.

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

operator =

Definition at line 90 of file LinearModel.h.

References shark::LinearModel< InputType, OutputType >::swap.

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

obtain the parameter vector

Reimplemented from shark::IParameterizable.

Definition at line 153 of file LinearModel.h.

References SHARK_CHECK.

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

From ISerializable.

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

Definition at line 264 of file LinearModel.h.

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

overwrite the parameter vector

Reimplemented from shark::IParameterizable.

Definition at line 159 of file LinearModel.h.

References SHARK_CHECK.

Referenced by main().

template<class InputType = RealVector, class OutputType = InputType>
void shark::LinearModel< InputType, OutputType >::setStructure ( unsigned int  inputs,
unsigned int  outputs = 1,
bool  offset = false,
bool  sparse = false 
)
inline

overwrite structure and parameters

Definition at line 177 of file LinearModel.h.

References shark::LinearModel< InputType, OutputType >::offset().

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

overwrite structure and parameters

Definition at line 185 of file LinearModel.h.

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

overwrite structure and parameters

Definition at line 190 of file LinearModel.h.

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

overwrite structure and parameters

Definition at line 195 of file LinearModel.h.

References SHARK_CHECK.

template<class InputType = RealVector, class OutputType = InputType>
void shark::LinearModel< InputType, OutputType >::weightedParameterDerivative ( BatchInputType const &  pattern,
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 247 of file LinearModel.h.

template<class InputType = RealVector, class OutputType = InputType>
void shark::LinearModel< InputType, OutputType >::weightedParameterDerivative ( BatchInputType const &  pattern,
BatchOutputType const &  coefficients,
Batch< RealMatrix >::type const &  errorHessian,
State const &  state,
RealVector &  derivative,
RealMatrix &  hessian 
) const
inlinevirtual

calculates the weighted sum of derivatives w.r.t the parameters

Parameters
patternthe patterns to evaluate
coefficientsthe coefficients which are used to calculate the weighted sum for every pattern
errorHessianthe second derivative of the error function for every pattern
stateintermediate results stored by eval to sped up calculations of the derivatives
derivativethe calculated derivative as sum over all derivates of all patterns
hessianthe calculated hessian as sum over all derivates of all patterns

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

Definition at line 252 of file LinearModel.h.

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

From ISerializable.

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

Definition at line 272 of file LinearModel.h.

Friends And Related Function Documentation

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

swap

Definition at line 85 of file LinearModel.h.

Referenced by shark::LinearModel< InputType, OutputType >::operator=().


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