shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType > Class Template Referenceabstract

Base class for all Models. More...

#include <shark/Models/AbstractModel.h>

+ Inheritance diagram for shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >:

Public Types

enum  Feature { HAS_FIRST_PARAMETER_DERIVATIVE = 1, HAS_FIRST_INPUT_DERIVATIVE = 4 }
 
typedef InputTypeT InputType
 Defines the input type of the model. More...
 
typedef OutputTypeT OutputType
 Defines the output type of the model. More...
 
typedef AbstractModel< InputTypeT, OutputTypeT, ParameterType > ModelBaseType
 Defines the BaseType used by the model (this type). Useful for creating derived models. 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 Types inherited from shark::IParameterizable< ParameterType >
typedef ParameterType ParameterVectorType
 

Public Member Functions

 AbstractModel ()
 
virtual ~AbstractModel ()
 
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasFirstParameterDerivative () const
 Returns true when the first parameter derivative is implemented. More...
 
bool hasFirstInputDerivative () const
 Returns true when the first input derivative is implemented. More...
 
virtual Shape inputShape () const =0
 Returns the expected shape of the input. More...
 
virtual Shape outputShape () const =0
 Returns the shape of the output. More...
 
virtual boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
virtual void read (InArchive &archive)
 From ISerializable, reads a model from an archive. More...
 
virtual void write (OutArchive &archive) const
 writes a model to an archive More...
 
virtual void eval (BatchInputType const &patterns, BatchOutputType &outputs) const
 Standard interface for evaluating the response of the model to a batch of patterns. More...
 
virtual void eval (BatchInputType const &patterns, BatchOutputType &outputs, State &state) const =0
 Standard interface for evaluating the response of the model to a batch of patterns. More...
 
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 &outputs, 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 &outputs, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const
 calculates the weighted sum of derivatives w.r.t the inputs More...
 
virtual void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &outputs, 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< ParameterType >
virtual ~IParameterizable ()
 
virtual ParameterVectorType parameterVector () const
 Return the parameter vector. More...
 
virtual void setParameterVector (ParameterVectorType const &newParameters)
 Set the parameter vector. More...
 
virtual std::size_t numberOfParameters () const
 Return the number of parameters. More...
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
virtual std::string name () const
 returns the name of the object More...
 
- 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

Features m_features
 

Detailed Description

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
class shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >

Base class for all Models.

A model is one of the three fundaments of supervised learning: model, error measure and an optimization algorithm. It is a concept of a function which performs a mapping \( x \rightarrow f_w(x)\). In contrast to an error function it has two sets of parameters: The first is the current point to map \(x\), the others are the internal model parameters \(w\) which define the mapping. Often a model is used to find an optimal mapping for a problem, for example a function which best fits the points of a given dataset. Therefore, AbstractModel does not only offer the mapping itself, but also a set of special derivatives with respect to \( x \) and \( w \). Most of the time, only the derivative with respect to \( w \) is needed, but in some special problems, like finding optimal stimuli or stacking models, also the input derivative is needed.
Models are optimized for batch processing. This means, that instead of only one data point at a time, it can
evaluate a big set of inputs at the same time, using optimized routines for this task.
The derivatives are weighted, which means that the derivatives of every single output are added together weighted by coefficients (see weightedParameterDerivative). This is an optimization for the chain rule which is very efficient to calculate most of the time.
It is allowed to store intermediate values during eval and use them to speed up calculation of derivatives. Therefore it must be guaranteed that eval() is called before calculating derivatives. This is no restriction, since typical error measures need the mapping itself and not only the derivative.
Models have names and can be serialised and have parameters. The type of the parameter vector can be set as third argument. By default, this is RealVector.

Definition at line 80 of file AbstractModel.h.

Member Typedef Documentation

◆ BatchInputType

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
typedef Batch<InputType>::type shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::BatchInputType

defines the batch type of the input type.

This could for example be std::vector<InputType> but for example for RealVector it could be RealMatrix

Definition at line 96 of file AbstractModel.h.

◆ BatchOutputType

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
typedef Batch<OutputType>::type shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::BatchOutputType

defines the batch type of the output type

Definition at line 98 of file AbstractModel.h.

◆ FeatureNotAvailableException

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
typedef TypedFeatureNotAvailableException<Feature> shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::FeatureNotAvailableException

Definition at line 109 of file AbstractModel.h.

◆ Features

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
typedef TypedFlags<Feature> shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::Features

Definition at line 109 of file AbstractModel.h.

◆ InputType

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
typedef InputTypeT shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::InputType

Defines the input type of the model.

Definition at line 84 of file AbstractModel.h.

◆ ModelBaseType

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
typedef AbstractModel<InputTypeT,OutputTypeT,ParameterType> shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::ModelBaseType

Defines the BaseType used by the model (this type). Useful for creating derived models.

Definition at line 91 of file AbstractModel.h.

◆ OutputType

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
typedef OutputTypeT shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::OutputType

Defines the output type of the model.

Definition at line 86 of file AbstractModel.h.

Member Enumeration Documentation

◆ Feature

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
enum shark::AbstractModel::Feature
Enumerator
HAS_FIRST_PARAMETER_DERIVATIVE 
HAS_FIRST_INPUT_DERIVATIVE 

Definition at line 105 of file AbstractModel.h.

Constructor & Destructor Documentation

◆ AbstractModel()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::AbstractModel ( )
inline

Definition at line 101 of file AbstractModel.h.

◆ ~AbstractModel()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::~AbstractModel ( )
inlinevirtual

Definition at line 103 of file AbstractModel.h.

Member Function Documentation

◆ createState()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual boost::shared_ptr<State> shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::createState ( ) const
inlinevirtual

◆ eval() [1/3]

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs 
) const
inlinevirtual

◆ eval() [2/3]

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs,
State state 
) const
pure virtual

◆ eval() [3/3]

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::eval ( InputType const &  pattern,
OutputType output 
) const
inlinevirtual

◆ features()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
const Features& shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::features ( ) const
inline

Definition at line 109 of file AbstractModel.h.

◆ hasFirstInputDerivative()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
bool shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::hasFirstInputDerivative ( ) const
inline

Returns true when the first input derivative is implemented.

Definition at line 116 of file AbstractModel.h.

Referenced by shark::AbstractModel< InputT, unsigned int >::createState().

◆ hasFirstParameterDerivative()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
bool shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::hasFirstParameterDerivative ( ) const
inline

◆ inputShape()

◆ operator()() [1/3]

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
Data<OutputType> shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::operator() ( Data< InputType > const &  patterns) const
inline

Model evaluation as an operator for a whole dataset. This is a convenience function.

Parameters
patternsthe input of the model
Returns
the responses of the model

Definition at line 190 of file AbstractModel.h.

◆ operator()() [2/3]

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
OutputType shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::operator() ( InputType const &  pattern) const
inline

Model evaluation as an operator for a single pattern. This is a convenience function.

Parameters
patternthe input of the model
Returns
the response of the model

Definition at line 198 of file AbstractModel.h.

◆ operator()() [3/3]

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
BatchOutputType shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::operator() ( BatchInputType const &  patterns) const
inline

Model evaluation as an operator for a single pattern. This is a convenience function.

Parameters
patternsthe input of the model
Returns
the response of the model

Definition at line 208 of file AbstractModel.h.

◆ outputShape()

◆ read()

◆ updateFeatures()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::updateFeatures ( )
inlinevirtual

Definition at line 109 of file AbstractModel.h.

◆ weightedDerivatives()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::weightedDerivatives ( BatchInputType const &  patterns,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
RealVector &  parameterDerivative,
BatchInputType inputDerivative 
) const
inlinevirtual

calculates weighted input and parameter derivative at the same time

Sometimes, both derivatives are needed at the same time. But sometimes, when calculating the weighted parameter derivative, the input derivative can be calculated for free. This is for example true for the feed-forward neural networks. However, there exists the obvious default implementation to just calculate the derivatives one after another.

Parameters
patternsthe patterns to evaluate
coefficientsthe coefficients which are used to calculate the weighted sum
stateintermediate results stored by eval to sped up calculations of the derivatives
parameterDerivativethe calculated parameter derivative as sum over all derivates of all patterns
inputDerivativethe calculated derivative for every pattern

Reimplemented in shark::ConcatenatedModel< VectorType >.

Definition at line 257 of file AbstractModel.h.

◆ weightedInputDerivative()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::weightedInputDerivative ( BatchInputType const &  pattern,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
BatchInputType derivative 
) const
inlinevirtual

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

Parameters
patternthe patterns to evaluate
coefficientsthe coefficients which are used to calculate the weighted sum for every pattern
stateintermediate results stored by eval to sped up calculations of the derivatives
derivativethe calculated derivative for every pattern

Reimplemented in shark::NeuronLayer< NeuronType, VectorType >, shark::Conv2DModel< VectorType, ActivationFunction >, and shark::DropoutLayer< VectorType >.

Definition at line 236 of file AbstractModel.h.

Referenced by shark::AbstractModel< InputT, unsigned int >::weightedDerivatives().

◆ weightedParameterDerivative()

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::weightedParameterDerivative ( BatchInputType const &  pattern,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
RealVector &  derivative 
) 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
stateintermediate results stored by eval to speed up calculations of the derivatives
derivativethe calculated derivative as sum over all derivates of all patterns

Reimplemented in shark::NeuronLayer< NeuronType, VectorType >, shark::Conv2DModel< VectorType, ActivationFunction >, shark::ConcatenatedModel< VectorType >, shark::DropoutLayer< VectorType >, and shark::RBFLayer.

Definition at line 220 of file AbstractModel.h.

Referenced by shark::AbstractModel< InputT, unsigned int >::weightedDerivatives().

◆ write()

Member Data Documentation

◆ m_features

template<class InputTypeT, class OutputTypeT, class ParameterType = RealVector>
Features shark::AbstractModel< InputTypeT, OutputTypeT, ParameterType >::m_features
protected

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