shark::ConcatenatedModel< VectorType > 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< VectorType >:

Public Types

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

Public Member Functions

std::string name () const
 From INameable: return the class name. More...
 
Shape inputShape () const
 Returns the expected shape of the input. More...
 
Shape outputShape () const
 Returns the shape of the output. More...
 
void add (AbstractModel< VectorType, VectorType > *layer, bool optimize)
 
void enableModelOptimization (std::size_t index, bool opt)
 sets whether the parameters of the index-th model should be optimized More...
 
ParameterVectorType parameterVector () const
 Return the parameter vector. More...
 
void setParameterVector (ParameterVectorType const &newParameters)
 Set the parameter vector. More...
 
std::size_t numberOfParameters () const
 Return the number of parameters. More...
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
BatchOutputType const & hiddenResponses (State const &state, std::size_t index) const
 
State const & hiddenState (State const &state, std::size_t index) const
 
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 &outputs, 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 &outputs, BatchOutputType const &coefficients, State const &state, BatchOutputType &derivatives) const
 
virtual void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, RealVector &gradient, 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< VectorType, VectorType, VectorType >
 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 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 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...
 
- Public Member Functions inherited from shark::IParameterizable< VectorType >
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< VectorType, VectorType, VectorType >
Features m_features
 

Detailed Description

template<class VectorType>
class shark::ConcatenatedModel< VectorType >

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 56 of file ConcatenatedModel.h.

Member Typedef Documentation

◆ BatchInputType

Definition at line 60 of file ConcatenatedModel.h.

◆ BatchOutputType

Definition at line 61 of file ConcatenatedModel.h.

◆ ParameterVectorType

Definition at line 62 of file ConcatenatedModel.h.

Member Function Documentation

◆ add()

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::add ( AbstractModel< VectorType, VectorType > *  layer,
bool  optimize 
)
inline

◆ createState()

template<class VectorType>
boost::shared_ptr<State> shark::ConcatenatedModel< VectorType >::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< VectorType, VectorType, VectorType >.

Definition at line 145 of file ConcatenatedModel.h.

◆ enableModelOptimization()

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::enableModelOptimization ( std::size_t  index,
bool  opt 
)
inline

sets whether the parameters of the index-th model should be optimized

If the model has non-differentiable submodels disabling those will make the whole model differentiable. Note that the models are ordered as model0 >> model1>> model2>>...

Definition at line 89 of file ConcatenatedModel.h.

References shark::AbstractModel< VectorType, VectorType, VectorType >::HAS_FIRST_INPUT_DERIVATIVE, shark::AbstractModel< VectorType, VectorType, VectorType >::HAS_FIRST_PARAMETER_DERIVATIVE, shark::AbstractModel< VectorType, VectorType, VectorType >::m_features, shark::TypedFlags< Flag >::reset(), and SIZE_CHECK.

Referenced by shark::ConcatenatedModel< VectorType >::add(), and trainAutoencoderModel().

◆ eval() [1/2]

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::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< VectorType, VectorType, VectorType >.

Definition at line 165 of file ConcatenatedModel.h.

References shark::swap().

◆ eval() [2/2]

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::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< VectorType, VectorType, VectorType >.

Definition at line 173 of file ConcatenatedModel.h.

References shark::State::toState().

◆ hiddenResponses()

template<class VectorType>
BatchOutputType const& shark::ConcatenatedModel< VectorType >::hiddenResponses ( State const &  state,
std::size_t  index 
) const
inline

Definition at line 154 of file ConcatenatedModel.h.

References shark::State::toState().

◆ hiddenState()

template<class VectorType>
State const& shark::ConcatenatedModel< VectorType >::hiddenState ( State const &  state,
std::size_t  index 
) const
inline

◆ inputShape()

template<class VectorType>
Shape shark::ConcatenatedModel< VectorType >::inputShape ( ) const
inlinevirtual

Returns the expected shape of the input.

Implements shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 70 of file ConcatenatedModel.h.

◆ name()

template<class VectorType>
std::string shark::ConcatenatedModel< VectorType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 65 of file ConcatenatedModel.h.

Referenced by trainAutoencoderModel().

◆ numberOfParameters()

template<class VectorType>
std::size_t shark::ConcatenatedModel< VectorType >::numberOfParameters ( ) const
inlinevirtual

◆ outputShape()

template<class VectorType>
Shape shark::ConcatenatedModel< VectorType >::outputShape ( ) const
inlinevirtual

Returns the shape of the output.

Implements shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 74 of file ConcatenatedModel.h.

◆ parameterVector()

template<class VectorType>
ParameterVectorType shark::ConcatenatedModel< VectorType >::parameterVector ( ) const
inlinevirtual

Return the parameter vector.

Reimplemented from shark::IParameterizable< VectorType >.

Definition at line 114 of file ConcatenatedModel.h.

References shark::ConcatenatedModel< VectorType >::numberOfParameters().

◆ read()

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::read ( InArchive archive)
inlinevirtual

◆ setParameterVector()

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::setParameterVector ( ParameterVectorType const &  newParameters)
inlinevirtual

Set the parameter vector.

Reimplemented from shark::IParameterizable< VectorType >.

Definition at line 126 of file ConcatenatedModel.h.

Referenced by trainAutoencoderModel().

◆ weightedDerivatives()

template<class VectorType>
virtual void shark::ConcatenatedModel< VectorType >::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 from shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 244 of file ConcatenatedModel.h.

References shark::ConcatenatedModel< VectorType >::numberOfParameters(), shark::swap(), and shark::State::toState().

◆ weightedInputDerivative()

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

Definition at line 222 of file ConcatenatedModel.h.

References shark::swap(), and shark::State::toState().

◆ weightedParameterDerivative()

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::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 from shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 185 of file ConcatenatedModel.h.

References shark::ConcatenatedModel< VectorType >::numberOfParameters(), shark::swap(), and shark::State::toState().

◆ write()

template<class VectorType>
void shark::ConcatenatedModel< VectorType >::write ( OutArchive archive) const
inlinevirtual

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