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

ConcatenatedModel concatenates two models such that the output of the first model is input to the second. More...

#include <shark/Models/ConcatenatedModel.h>

+ Inheritance diagram for shark::ConcatenatedModel< 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< FeatureFeatureNotAvailableException
 

Public Member Functions

template<class T >
 ConcatenatedModel (AbstractModel< InputType, T > *firstModel, AbstractModel< T, OutputType > *secondModel)
 creates a concatenated model using two base model. this is equivalent to concModel = *firstModel >> *secondModel; More...
 
 ConcatenatedModel (const detail::ConcatenatedModelWrapperBase< InputType, OutputType > &wrapper)
 copy constructor to allow ConcatenatedModel concModel = model1 >> model2 >> model3; More...
 
ConcatenatedModel< InputType, OutputType > & operator= (detail::ConcatenatedModelWrapperBase< InputType, OutputType > &wrapper)
 operator = to allow concModel = model1 >> model2 >> model3; for a previously declared concatenadel model More...
 
bool optimizeFirstModelParameters () const
 Returns whether the parameters of the first model are going to be optimized. More...
 
bool & optimizeFirstModelParameters ()
 Returns a variable indicting whether the parameters of the first model are going to be optimized. More...
 
bool optimizeSecondModelParameters () const
 Returns whether the parameters of the second model are going to be optimized. More...
 
bool & optimizeSecondModelParameters ()
 Returns a variable indicting whether the parameters of the second model are going to be optimized. More...
 
 ConcatenatedModel (const ConcatenatedModel< InputType, OutputType > &src)
 
std::string name () const
 From INameable: return the class name. More...
 
const ConcatenatedModel< InputType, OutputType > & operator= (const ConcatenatedModel< InputType, OutputType > &src)
 
RealVector parameterVector () const
 Return the parameter vector. More...
 
void setParameterVector (RealVector const &newParameters)
 Set the parameter vector. More...
 
size_t numberOfParameters () const
 Return the number of parameters. More...
 
boost::shared_ptr< StatecreateState () 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 weightedParameterDerivative (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, RealVector &gradient) const
 calculates the weighted sum of derivatives w.r.t the parameters. More...
 
void weightedInputDerivative (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, BatchOutputType &derivatives) const
 
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...
 
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, 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...
 
- 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 ()
 

Additional Inherited Members

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

Detailed Description

template<class InputType, class OutputType>
class shark::ConcatenatedModel< InputType, OutputType >

ConcatenatedModel concatenates two models such that the output of the first model is input to the second.

Sometimes a series of models is needed to generate the desired output. For example when input data needs to be normalized before it can be put into the trained model. In this case, the ConcatenatedModel can be used to represent this series as one model. The easiest way to do is is using the operator >> of AbstractModel: ConcatenatedModel<InputType,OutputType> model = model1>>model2; InputType must be the type of input model1 receives and model2 the output of model2. The output of model1 and input of model2 must match. Another way of construction is calling the constructor of ConcatenatedModel using the constructor: ConcatenatedModel<InputType,OutputType> model (&modell,&model2); warning: model1 and model2 must outlive model. When they are destroyed first, behavior is undefined.

Definition at line 362 of file ConcatenatedModel.h.

Member Typedef Documentation

§ BatchInputType

template<class InputType, class OutputType>
typedef base_type::BatchInputType shark::ConcatenatedModel< InputType, OutputType >::BatchInputType

Definition at line 368 of file ConcatenatedModel.h.

§ BatchOutputType

template<class InputType, class OutputType>
typedef base_type::BatchOutputType shark::ConcatenatedModel< InputType, OutputType >::BatchOutputType

Definition at line 369 of file ConcatenatedModel.h.

Constructor & Destructor Documentation

§ ConcatenatedModel() [1/3]

template<class InputType, class OutputType>
template<class T >
shark::ConcatenatedModel< InputType, OutputType >::ConcatenatedModel ( AbstractModel< InputType, T > *  firstModel,
AbstractModel< T, OutputType > *  secondModel 
)
inline

creates a concatenated model using two base model. this is equivalent to concModel = *firstModel >> *secondModel;

Definition at line 374 of file ConcatenatedModel.h.

§ ConcatenatedModel() [2/3]

template<class InputType, class OutputType>
shark::ConcatenatedModel< InputType, OutputType >::ConcatenatedModel ( const detail::ConcatenatedModelWrapperBase< InputType, OutputType > &  wrapper)
inline

copy constructor to allow ConcatenatedModel concModel = model1 >> model2 >> model3;

Definition at line 385 of file ConcatenatedModel.h.

§ ConcatenatedModel() [3/3]

template<class InputType, class OutputType>
shark::ConcatenatedModel< InputType, OutputType >::ConcatenatedModel ( const ConcatenatedModel< InputType, OutputType > &  src)
inline

Member Function Documentation

§ createState()

template<class InputType, class OutputType>
boost::shared_ptr<State> shark::ConcatenatedModel< 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 465 of file ConcatenatedModel.h.

§ eval() [1/2]

template<class InputType, class OutputType>
void shark::ConcatenatedModel< InputType, OutputType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs 
) const
inlinevirtual

Standard interface for evaluating the response of the model to a batch of patterns.

Parameters
patternsthe inputs of the model
outputsthe predictions or response of the model to every pattern

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

Definition at line 470 of file ConcatenatedModel.h.

§ eval() [2/2]

template<class InputType, class OutputType>
void shark::ConcatenatedModel< InputType, OutputType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs,
State state 
) const
inlinevirtual

Standard interface for evaluating the response of the model to a batch of patterns.

Parameters
patternsthe inputs of the model
outputsthe predictions or response of the model to every pattern
stateintermediate results stored by eval which can be reused for derivative computation.

Implements shark::AbstractModel< InputType, OutputType >.

Definition at line 473 of file ConcatenatedModel.h.

§ name()

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

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 443 of file ConcatenatedModel.h.

Referenced by trainAutoencoderModel().

§ numberOfParameters()

template<class InputType, class OutputType>
size_t shark::ConcatenatedModel< InputType, OutputType >::numberOfParameters ( ) const
inlinevirtual

Return the number of parameters.

Reimplemented from shark::IParameterizable.

Definition at line 461 of file ConcatenatedModel.h.

§ operator=() [1/2]

template<class InputType, class OutputType>
ConcatenatedModel<InputType,OutputType>& shark::ConcatenatedModel< InputType, OutputType >::operator= ( detail::ConcatenatedModelWrapperBase< InputType, OutputType > &  wrapper)
inline

operator = to allow concModel = model1 >> model2 >> model3; for a previously declared concatenadel model

Definition at line 396 of file ConcatenatedModel.h.

§ operator=() [2/2]

template<class InputType, class OutputType>
const ConcatenatedModel<InputType,OutputType>& shark::ConcatenatedModel< InputType, OutputType >::operator= ( const ConcatenatedModel< InputType, OutputType > &  src)
inline

§ optimizeFirstModelParameters() [1/2]

template<class InputType, class OutputType>
bool shark::ConcatenatedModel< InputType, OutputType >::optimizeFirstModelParameters ( ) const
inline

Returns whether the parameters of the first model are going to be optimized.

Remember that concatModel = first >> second, so it is the lower layer.

Definition at line 412 of file ConcatenatedModel.h.

§ optimizeFirstModelParameters() [2/2]

template<class InputType, class OutputType>
bool& shark::ConcatenatedModel< InputType, OutputType >::optimizeFirstModelParameters ( )
inline

Returns a variable indicting whether the parameters of the first model are going to be optimized.

Remember that concatModel = first >> second, so it is the lower layer.

Definition at line 419 of file ConcatenatedModel.h.

§ optimizeSecondModelParameters() [1/2]

template<class InputType, class OutputType>
bool shark::ConcatenatedModel< InputType, OutputType >::optimizeSecondModelParameters ( ) const
inline

Returns whether the parameters of the second model are going to be optimized.

Remember that concatModel = first >> second, so it is the upper layer.

Definition at line 426 of file ConcatenatedModel.h.

§ optimizeSecondModelParameters() [2/2]

template<class InputType, class OutputType>
bool& shark::ConcatenatedModel< InputType, OutputType >::optimizeSecondModelParameters ( )
inline

Returns a variable indicting whether the parameters of the second model are going to be optimized.

Remember that concatModel = first >> second, so it is the upper layer.

Definition at line 433 of file ConcatenatedModel.h.

§ parameterVector()

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

Return the parameter vector.

Reimplemented from shark::IParameterizable.

Definition at line 453 of file ConcatenatedModel.h.

§ read()

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

From ISerializable.

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

Definition at line 500 of file ConcatenatedModel.h.

§ setParameterVector()

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

Set the parameter vector.

Reimplemented from shark::IParameterizable.

Definition at line 457 of file ConcatenatedModel.h.

Referenced by trainAutoencoderModel().

§ weightedDerivatives()

template<class InputType, class OutputType>
virtual void shark::ConcatenatedModel< InputType, OutputType >::weightedDerivatives ( BatchInputType const &  patterns,
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 from shark::AbstractModel< InputType, OutputType >.

Definition at line 489 of file ConcatenatedModel.h.

§ weightedInputDerivative()

template<class InputType, class OutputType>
void shark::ConcatenatedModel< InputType, OutputType >::weightedInputDerivative ( BatchInputType const &  patterns,
BatchOutputType const &  coefficients,
State const &  state,
BatchOutputType derivatives 
) const
inline

Definition at line 483 of file ConcatenatedModel.h.

§ weightedParameterDerivative()

template<class InputType, class OutputType>
void shark::ConcatenatedModel< InputType, OutputType >::weightedParameterDerivative ( BatchInputType const &  pattern,
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 from shark::AbstractModel< InputType, OutputType >.

Definition at line 477 of file ConcatenatedModel.h.

§ write()

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

From ISerializable.

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

Definition at line 505 of file ConcatenatedModel.h.


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