shark::KernelExpansion< InputType > Class Template Reference

Linear model in a kernel feature space. More...

#include <shark/Models/Kernels/KernelExpansion.h>

+ Inheritance diagram for shark::KernelExpansion< InputType >:

Public Types

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

Public Member Functions

 KernelExpansion ()
 
 KernelExpansion (KernelType *kernel)
 
 KernelExpansion (KernelType *kernel, Data< InputType > const &basis, bool offset, std::size_t outputs=1)
 
void setStructure (KernelType *kernel, Data< InputType > const &basis, bool offset, std::size_t outputs=1)
 
std::string name () const
 From INameable: return the class name. More...
 
Shape outputShape () const
 dimensionality of the output RealVector More...
 
Shape inputShape () const
 Returns the expected shape of the input. More...
 
KernelType const * kernel () const
 
KernelTypekernel ()
 
void setKernel (KernelType *kernel)
 
bool hasOffset () const
 
RealMatrix & alpha ()
 
RealMatrix const & alpha () const
 
double & alpha (std::size_t example, std::size_t cls)
 
double const & alpha (std::size_t example, std::size_t cls) const
 
RealVector & offset ()
 
RealVector const & offset () const
 
double & offset (std::size_t cls)
 
double const & offset (std::size_t cls) const
 
Data< InputType > const & basis () const
 
Data< InputType > & basis ()
 
void sparsify ()
 
RealVector parameterVector () const
 Return the parameter vector. More...
 
void setParameterVector (RealVector 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...
 
void eval (BatchInputType const &patterns, BatchOutputType &output) 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 read (InArchive &archive)
 From ISerializable, reads a model from an archive. More...
 
void write (OutArchive &archive) const
 From ISerializable, writes a model to an archive. More...
 
- Public Member Functions inherited from shark::AbstractModel< InputType, RealVector >
 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 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< RealVector >
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 ()
 

Protected Attributes

KernelTypemep_kernel
 kernel function used in the expansion More...
 
Data< InputTypem_basis
 "support" basis vectors More...
 
RealMatrix m_alpha
 kernel coefficients More...
 
RealVector m_b
 offset or bias term More...
 
- Protected Attributes inherited from shark::AbstractModel< InputType, RealVector >
Features m_features
 

Detailed Description

template<class InputType>
class shark::KernelExpansion< InputType >

Linear model in a kernel feature space.

An affine linear kernel expansion is a model of the type

\[ x : \mathbb{R}^d \to \mathbb{R}^d \enspace , \enspace x \mapsto \sum_{n=1}^{\ell} \alpha_n k(x_n, x) + b \enspace ,\]

with parameters \( \alpha_n \in \mathbb{R}^{d} \) for all \( n \in \{1, \dots, \ell\} \) and \( b \in \mathbb{R}^d \).

One way in which the possibility for vector-valued input and output of dimension \( d \) may be interpreted is as allowing for a KernelExpansion model for \( d \) different classes/outputs in multi-class problems. Then, the i-th column of the matrix m_alpha is the KernelExpansion for class/output i.

Template Parameters
InputTypeType of basis elements supplied to the kernel

Definition at line 67 of file KernelExpansion.h.

Member Typedef Documentation

◆ BatchInputType

Definition at line 72 of file KernelExpansion.h.

◆ BatchOutputType

Definition at line 73 of file KernelExpansion.h.

◆ KernelType

Definition at line 70 of file KernelExpansion.h.

Constructor & Destructor Documentation

◆ KernelExpansion() [1/3]

template<class InputType>
shark::KernelExpansion< InputType >::KernelExpansion ( )
inline

Definition at line 79 of file KernelExpansion.h.

◆ KernelExpansion() [2/3]

template<class InputType>
shark::KernelExpansion< InputType >::KernelExpansion ( KernelType kernel)
inline

Definition at line 81 of file KernelExpansion.h.

References SHARK_ASSERT.

◆ KernelExpansion() [3/3]

template<class InputType>
shark::KernelExpansion< InputType >::KernelExpansion ( KernelType kernel,
Data< InputType > const &  basis,
bool  offset,
std::size_t  outputs = 1 
)
inline

Member Function Documentation

◆ alpha() [1/4]

◆ alpha() [2/4]

template<class InputType>
RealMatrix const& shark::KernelExpansion< InputType >::alpha ( ) const
inline

Definition at line 137 of file KernelExpansion.h.

References shark::KernelExpansion< InputType >::m_alpha.

◆ alpha() [3/4]

template<class InputType>
double& shark::KernelExpansion< InputType >::alpha ( std::size_t  example,
std::size_t  cls 
)
inline

Definition at line 140 of file KernelExpansion.h.

References shark::KernelExpansion< InputType >::m_alpha.

◆ alpha() [4/4]

template<class InputType>
double const& shark::KernelExpansion< InputType >::alpha ( std::size_t  example,
std::size_t  cls 
) const
inline

Definition at line 143 of file KernelExpansion.h.

References shark::KernelExpansion< InputType >::m_alpha.

◆ basis() [1/2]

◆ basis() [2/2]

template<class InputType>
Data<InputType>& shark::KernelExpansion< InputType >::basis ( )
inline

Definition at line 172 of file KernelExpansion.h.

References shark::KernelExpansion< InputType >::m_basis.

◆ createState()

template<class InputType>
boost::shared_ptr<State> shark::KernelExpansion< InputType >::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, RealVector >.

Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.

Definition at line 229 of file KernelExpansion.h.

◆ eval() [1/2]

template<class InputType>
void shark::KernelExpansion< InputType >::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, RealVector >.

Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.

Definition at line 234 of file KernelExpansion.h.

References shark::batchSize(), shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_alpha, shark::KernelExpansion< InputType >::mep_kernel, and SHARK_ASSERT.

Referenced by shark::KernelExpansion< InputType >::eval().

◆ eval() [2/2]

template<class InputType>
void shark::KernelExpansion< InputType >::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, RealVector >.

Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.

Definition at line 259 of file KernelExpansion.h.

References shark::KernelExpansion< InputType >::eval().

◆ hasOffset()

◆ inputShape()

template<class InputType>
Shape shark::KernelExpansion< InputType >::inputShape ( ) const
inlinevirtual

Returns the expected shape of the input.

Implements shark::AbstractModel< InputType, RealVector >.

Definition at line 109 of file KernelExpansion.h.

◆ kernel() [1/2]

◆ kernel() [2/2]

template<class InputType>
KernelType* shark::KernelExpansion< InputType >::kernel ( )
inline

◆ name()

template<class InputType>
std::string shark::KernelExpansion< InputType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.

Definition at line 101 of file KernelExpansion.h.

◆ numberOfParameters()

template<class InputType>
std::size_t shark::KernelExpansion< InputType >::numberOfParameters ( ) const
inlinevirtual

◆ offset() [1/4]

◆ offset() [2/4]

template<class InputType>
RealVector const& shark::KernelExpansion< InputType >::offset ( ) const
inline

◆ offset() [3/4]

template<class InputType>
double& shark::KernelExpansion< InputType >::offset ( std::size_t  cls)
inline

◆ offset() [4/4]

template<class InputType>
double const& shark::KernelExpansion< InputType >::offset ( std::size_t  cls) const
inline

◆ outputShape()

template<class InputType>
Shape shark::KernelExpansion< InputType >::outputShape ( ) const
inlinevirtual

◆ parameterVector()

template<class InputType>
RealVector shark::KernelExpansion< InputType >::parameterVector ( ) const
inlinevirtual

Return the parameter vector.

Reimplemented from shark::IParameterizable< RealVector >.

Definition at line 201 of file KernelExpansion.h.

References shark::KernelExpansion< InputType >::hasOffset().

◆ read()

◆ setKernel()

template<class InputType>
void shark::KernelExpansion< InputType >::setKernel ( KernelType kernel)
inline

◆ setParameterVector()

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

◆ setStructure()

◆ sparsify()

template<class InputType>
void shark::KernelExpansion< InputType >::sparsify ( )
inline

The sparsify method removes non-support-vectors from its set of basis vectors and the coefficient matrix.

Definition at line 178 of file KernelExpansion.h.

References shark::KernelExpansion< InputType >::m_basis, and shark::Data< Type >::numberOfElements().

◆ write()

Member Data Documentation

◆ m_alpha

◆ m_b

◆ m_basis

◆ mep_kernel


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