shark::ConvexCombination Class Reference

Models a convex combination of inputs. More...

#include <shark/Models/ConvexCombination.h>

+ Inheritance diagram for shark::ConvexCombination:

Public Member Functions

 ConvexCombination ()
 CDefault Constructor; use setStructure later. More...
 
 ConvexCombination (std::size_t inputs, std::size_t outputs=1)
 Constructor creating a model with given dimnsionalities and optional offset term. More...
 
 ConvexCombination (RealMatrix const &matrix)
 Construction from matrix. More...
 
std::string name () const
 From INameable: return the class name. More...
 
ConvexCombinationoperator= (ConvexCombination const &model)
 operator = More...
 
std::size_t inputSize () const
 obtain the input dimension More...
 
std::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...
 
std::size_t numberOfParameters () const
 return the number of parameter More...
 
void setStructure (std::size_t inputs, std::size_t outputs=1)
 overwrite structure and parameters More...
 
RealMatrix const & weights () const
 
RealMatrix & weights ()
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &inputs, BatchOutputType &outputs) const
 Evaluate the model: output = w * input. More...
 
void eval (BatchInputType const &inputs, BatchOutputType &outputs, State &state) const
 Evaluate the model: output = w *input. More...
 
void weightedParameterDerivative (BatchInputType const &patterns, 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 weightedInputDerivative (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const
 Calculates the first derivative w.r.t the inputs and summs them up over all patterns of the last computed batch. More...
 
void read (InArchive &archive)
 From ISerializable. More...
 
void write (OutArchive &archive) const
 From ISerializable. More...
 
- Public Member Functions inherited from shark::AbstractModel< RealVector, RealVector >
 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 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, 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::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 (ConvexCombination &model1, ConvexCombination &model2)
 swap More...
 

Additional Inherited Members

- Public Types inherited from shark::AbstractModel< RealVector, RealVector >
enum  Feature
 
typedef RealVector InputType
 Defines the input type of the model. More...
 
typedef RealVector 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
 
- Protected Attributes inherited from shark::AbstractModel< RealVector, RealVector >
Features m_features
 

Detailed Description

Models a convex combination of inputs.

For a given input vector x, the convex combination returns \( f_i(x) = sum_j w_{ij} x_j \), where \( w_i > 0 \) and \( sum_j w_{ij} = 1\), that is the outputs of the model are a convex combination of the inputs.

To ensure that the constraints are fulfilled, the model uses a different set of weights q_i and \( w_{ij} = exp(q_{ij})/sum_j exp(q_{ik}) \). As usual, this encoding is only used for the derivatives and the parameter vectors, not when the weights are explicitely set. In the latter case, the user must provide a set of suitable \( w_{ij} \).

Definition at line 51 of file ConvexCombination.h.

Constructor & Destructor Documentation

◆ ConvexCombination() [1/3]

◆ ConvexCombination() [2/3]

shark::ConvexCombination::ConvexCombination ( std::size_t  inputs,
std::size_t  outputs = 1 
)
inline

◆ ConvexCombination() [3/3]

shark::ConvexCombination::ConvexCombination ( RealMatrix const &  matrix)
inline

Member Function Documentation

◆ createState()

boost::shared_ptr<State> shark::ConvexCombination::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< RealVector, RealVector >.

Definition at line 135 of file ConvexCombination.h.

◆ eval() [1/2]

void shark::ConvexCombination::eval ( BatchInputType const &  inputs,
BatchOutputType outputs 
) const
inlinevirtual

Evaluate the model: output = w * input.

Reimplemented from shark::AbstractModel< RealVector, RealVector >.

Definition at line 140 of file ConvexCombination.h.

References remora::noalias(), remora::prod(), and remora::trans().

Referenced by eval().

◆ eval() [2/2]

void shark::ConvexCombination::eval ( BatchInputType const &  inputs,
BatchOutputType outputs,
State state 
) const
inlinevirtual

Evaluate the model: output = w *input.

Implements shark::AbstractModel< RealVector, RealVector >.

Definition at line 145 of file ConvexCombination.h.

References eval().

◆ inputSize()

std::size_t shark::ConvexCombination::inputSize ( ) const
inline

obtain the input dimension

Definition at line 93 of file ConvexCombination.h.

Referenced by weightedInputDerivative(), and weightedParameterDerivative().

◆ name()

std::string shark::ConvexCombination::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 77 of file ConvexCombination.h.

◆ numberOfParameters()

std::size_t shark::ConvexCombination::numberOfParameters ( ) const
inlinevirtual

return the number of parameter

Reimplemented from shark::IParameterizable.

Definition at line 117 of file ConvexCombination.h.

Referenced by weightedParameterDerivative().

◆ operator=()

ConvexCombination& shark::ConvexCombination::operator= ( ConvexCombination const &  model)
inline

operator =

Definition at line 86 of file ConvexCombination.h.

References swap.

◆ outputSize()

std::size_t shark::ConvexCombination::outputSize ( ) const
inline

obtain the output dimension

Definition at line 98 of file ConvexCombination.h.

Referenced by setParameterVector(), weightedInputDerivative(), and weightedParameterDerivative().

◆ parameterVector()

RealVector shark::ConvexCombination::parameterVector ( ) const
inlinevirtual

obtain the parameter vector

Reimplemented from shark::IParameterizable.

Definition at line 103 of file ConvexCombination.h.

References remora::to_vector().

◆ read()

void shark::ConvexCombination::read ( InArchive archive)
inlinevirtual

From ISerializable.

Reimplemented from shark::AbstractModel< RealVector, RealVector >.

Definition at line 188 of file ConvexCombination.h.

◆ setParameterVector()

void shark::ConvexCombination::setParameterVector ( RealVector const &  newParameters)
inlinevirtual

overwrite the parameter vector

Reimplemented from shark::IParameterizable.

Definition at line 108 of file ConvexCombination.h.

References remora::noalias(), outputSize(), remora::row(), remora::sum(), and remora::to_matrix().

◆ setStructure()

void shark::ConvexCombination::setStructure ( std::size_t  inputs,
std::size_t  outputs = 1 
)
inline

overwrite structure and parameters

Definition at line 122 of file ConvexCombination.h.

References swap.

Referenced by main().

◆ weightedInputDerivative()

void shark::ConvexCombination::weightedInputDerivative ( BatchInputType const &  patterns,
BatchOutputType const &  coefficients,
State const &  state,
BatchInputType derivative 
) const
inlinevirtual

Calculates the first derivative w.r.t the inputs and summs them up over all patterns of the last computed batch.

Reimplemented from shark::AbstractModel< RealVector, RealVector >.

Definition at line 174 of file ConvexCombination.h.

References inputSize(), remora::noalias(), outputSize(), remora::prod(), and SIZE_CHECK.

◆ weightedParameterDerivative()

void shark::ConvexCombination::weightedParameterDerivative ( BatchInputType const &  patterns,
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 150 of file ConvexCombination.h.

References remora::inner_prod(), inputSize(), remora::noalias(), numberOfParameters(), outputSize(), remora::prod(), remora::row(), SIZE_CHECK, remora::to_matrix(), and remora::trans().

◆ weights() [1/2]

RealMatrix const& shark::ConvexCombination::weights ( ) const
inline

Definition at line 127 of file ConvexCombination.h.

◆ weights() [2/2]

RealMatrix& shark::ConvexCombination::weights ( )
inline

Definition at line 131 of file ConvexCombination.h.

◆ write()

void shark::ConvexCombination::write ( OutArchive archive) const
inlinevirtual

From ISerializable.

Reimplemented from shark::AbstractModel< RealVector, RealVector >.

Definition at line 192 of file ConvexCombination.h.

Friends And Related Function Documentation

◆ swap

void swap ( ConvexCombination model1,
ConvexCombination model2 
)
friend

swap

Definition at line 81 of file ConvexCombination.h.

Referenced by operator=(), and setStructure().


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