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

Base class for all Models. More...

#include <shark/Models/AbstractModel.h>

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

Public Types

enum  Feature { HAS_FIRST_PARAMETER_DERIVATIVE = 1 , HAS_FIRST_INPUT_DERIVATIVE = 4 }
 
typedef InputTypeT InputType
 Defines the input type of the model.
 
typedef OutputTypeT OutputType
 Defines the output type of the model.
 
typedef AbstractModel< InputTypeT, OutputTypeT, ParameterVectorTypeModelBaseType
 Defines the BaseType used by the model (this type). Useful for creating derived models.
 
typedef Batch< InputType >::type BatchInputType
 defines the batch type of the input type.
 
typedef Batch< OutputType >::type BatchOutputType
 defines the batch type of the output type
 
typedef TypedFlags< FeatureFeatures
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 
- Public Types inherited from shark::IParameterizable< VectorType >
typedef VectorType 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.
 
bool hasFirstInputDerivative () const
 Returns true when the first input derivative is implemented.
 
virtual Shape inputShape () const =0
 Returns the expected shape of the input.
 
virtual Shape outputShape () const =0
 Returns the shape of the output.
 
virtual boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model.
 
virtual void read (InArchive &archive)
 From ISerializable, reads a model from an archive.
 
virtual void write (OutArchive &archive) const
 writes a model to an archive
 
virtual void eval (BatchInputType const &patterns, BatchOutputType &outputs) const
 Standard interface for evaluating the response of the model to a batch of patterns.
 
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.
 
virtual void eval (InputType const &pattern, OutputType &output) const
 Standard interface for evaluating the response of the model to a single pattern.
 
Data< OutputTypeoperator() (Data< InputType > const &patterns) const
 Model evaluation as an operator for a whole dataset. This is a convenience function.
 
OutputType operator() (InputType const &pattern) const
 Model evaluation as an operator for a single pattern. This is a convenience function.
 
BatchOutputType operator() (BatchInputType const &patterns) const
 Model evaluation as an operator for a single pattern. This is a convenience function.
 
virtual void weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, ParameterVectorType &derivative) const
 calculates the weighted sum of derivatives w.r.t the parameters.
 
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
 
virtual void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, ParameterVectorType &parameterDerivative, BatchInputType &inputDerivative) const
 calculates weighted input and parameter derivative at the same time
 
- Public Member Functions inherited from shark::IParameterizable< VectorType >
virtual ~IParameterizable ()
 
virtual ParameterVectorType parameterVector () const
 Return the parameter vector.
 
virtual void setParameterVector (ParameterVectorType const &newParameters)
 Set the parameter vector.
 
virtual std::size_t numberOfParameters () const
 Return the number of parameters.
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
virtual std::string name () const
 returns the name of the object
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor.
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...).
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...).
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Attributes

Features m_features
 

Detailed Description

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

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 81 of file AbstractModel.h.

Member Typedef Documentation

◆ BatchInputType

template<class InputTypeT , class OutputTypeT , class ParameterVectorType = RealVector>
typedef Batch<InputType>::type shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::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 97 of file AbstractModel.h.

◆ BatchOutputType

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

defines the batch type of the output type

Definition at line 99 of file AbstractModel.h.

◆ FeatureNotAvailableException

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

Definition at line 110 of file AbstractModel.h.

◆ Features

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

Definition at line 110 of file AbstractModel.h.

◆ InputType

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

Defines the input type of the model.

Definition at line 85 of file AbstractModel.h.

◆ ModelBaseType

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

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

Definition at line 92 of file AbstractModel.h.

◆ OutputType

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

Defines the output type of the model.

Definition at line 87 of file AbstractModel.h.

Member Enumeration Documentation

◆ Feature

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

Definition at line 106 of file AbstractModel.h.

Constructor & Destructor Documentation

◆ AbstractModel()

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

Definition at line 102 of file AbstractModel.h.

◆ ~AbstractModel()

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

Definition at line 104 of file AbstractModel.h.

Member Function Documentation

◆ createState()

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

◆ eval() [1/3]

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

◆ eval() [2/3]

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

◆ eval() [3/3]

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

◆ features()

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

Definition at line 110 of file AbstractModel.h.

◆ hasFirstInputDerivative()

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

◆ hasFirstParameterDerivative()

◆ inputShape()

◆ operator()() [1/3]

template<class InputTypeT , class OutputTypeT , class ParameterVectorType = RealVector>
BatchOutputType shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::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 209 of file AbstractModel.h.

References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::eval().

◆ operator()() [2/3]

template<class InputTypeT , class OutputTypeT , class ParameterVectorType = RealVector>
Data< OutputType > shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::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 191 of file AbstractModel.h.

References shark::transform().

◆ operator()() [3/3]

template<class InputTypeT , class OutputTypeT , class ParameterVectorType = RealVector>
OutputType shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::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 199 of file AbstractModel.h.

References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::eval().

◆ outputShape()

◆ read()

◆ updateFeatures()

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

Definition at line 110 of file AbstractModel.h.

◆ weightedDerivatives()

template<class InputTypeT , class OutputTypeT , class ParameterVectorType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::weightedDerivatives ( BatchInputType const &  patterns,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
ParameterVectorType 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 that just calculates the derivatives one after another.

Parameters
patternsthe patterns to evaluate
outputsthe target outputs
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 261 of file AbstractModel.h.

References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::weightedInputDerivative(), and shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::weightedParameterDerivative().

Referenced by shark::VariationalAutoencoderError< SearchPointType >::evalDerivative().

◆ weightedInputDerivative()

template<class InputTypeT , class OutputTypeT , class ParameterVectorType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::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
outputsthe target outputs
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::PoolingLayer< VectorType >, shark::Conv2DModel< VectorType, ActivationFunction >, shark::ResizeLayer< VectorType >, and shark::DropoutLayer< VectorType >.

Definition at line 239 of file AbstractModel.h.

References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_INPUT_DERIVATIVE, and SHARK_FEATURE_EXCEPTION.

Referenced by shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::weightedDerivatives().

◆ weightedParameterDerivative()

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

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

Parameters
patternthe patterns to evaluate
outputsthe target outputs
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::Conv2DModel< VectorType, ActivationFunction >, shark::NeuronLayer< NeuronType, VectorType >, shark::PoolingLayer< VectorType >, shark::ResizeLayer< VectorType >, shark::RBFLayer, shark::ConcatenatedModel< VectorType >, and shark::DropoutLayer< VectorType >.

Definition at line 222 of file AbstractModel.h.

References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_PARAMETER_DERIVATIVE, and SHARK_FEATURE_EXCEPTION.

Referenced by shark::NegativeLogLikelihood::evalDerivative(), shark::VariationalAutoencoderError< SearchPointType >::evalDerivative(), and shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::weightedDerivatives().

◆ write()

template<class InputTypeT , class OutputTypeT , class ParameterVectorType = RealVector>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::write ( OutArchive archive) const
inlinevirtual

Member Data Documentation

◆ m_features


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