shark::WeightedSumKernel< InputType > Class Template Reference

Weighted sum of kernel functions. More...

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

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

Classes

struct  tBase
 structure describing a single m_base kernel More...
 

Public Types

typedef base_type::BatchInputType BatchInputType
 
typedef base_type::ConstInputReference ConstInputReference
 
typedef base_type::ConstBatchInputReference ConstBatchInputReference
 
- Public Types inherited from shark::AbstractKernelFunction< InputType >
enum  Feature
 enumerations of kerneland metric features (flags) More...
 
typedef base_type::InputType InputType
 Input type of the Kernel. More...
 
typedef base_type::BatchInputType BatchInputType
 batch input type of the kernel More...
 
typedef base_type::ConstInputReference ConstInputReference
 Const references to InputType. More...
 
typedef base_type::ConstBatchInputReference ConstBatchInputReference
 Const references to BatchInputType. More...
 
typedef TypedFlags< FeatureFeatures
 This statement declares the member m_features. See Core/Flags.h for details. More...
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 
- Public Types inherited from shark::AbstractMetric< InputType >
typedef InputType InputType
 Input type of the Kernel. More...
 
typedef Batch< InputType >::type BatchInputType
 batch input type of the kernel More...
 
typedef ConstProxyReference< InputType const >::type ConstInputReference
 Const references to InputType. More...
 
typedef ConstProxyReference< BatchInputType const >::type ConstBatchInputReference
 Const references to BatchInputType. More...
 
- Public Types inherited from shark::IParameterizable<>
typedef RealVector ParameterVectorType
 

Public Member Functions

 WeightedSumKernel (std::vector< AbstractKernelFunction< InputType > * > const &base)
 
std::string name () const
 From INameable: return the class name. More...
 
bool isAdaptive (std::size_t index) const
 Check whether m_base kernel index is adaptive. More...
 
void setAdaptive (std::size_t index, bool b=true)
 Set adaptivity of m_base kernel index. More...
 
void setAdaptiveAll (bool b=true)
 Set adaptivity of all m_base kernels. More...
 
double weight (std::size_t index)
 Get the weight of a kernel. More...
 
void setAdaptiveWeights (bool b)
 
RealVector parameterVector () const
 
boost::shared_ptr< StatecreateState () const
 creates the internal state of the kernel More...
 
void setParameterVector (RealVector const &newParameters)
 
std::size_t numberOfParameters () const
 Return the number of parameters. More...
 
double eval (ConstInputReference x1, ConstInputReference x2) const
 
void eval (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix &result) const
 
void eval (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix &result, State &state) const
 
void weightedParameterDerivative (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix const &coefficients, State const &state, RealVector &gradient) const
 Computes the gradient of the parameters as a weighted sum over the gradient of all elements of the batch. More...
 
void weightedInputDerivative (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix const &coefficientsX2, State const &state, BatchInputType &gradient) const
 
void read (InArchive &ar)
 From ISerializable, reads a metric from an archive. More...
 
void write (OutArchive &ar) const
 From ISerializable, writes a metric to an archive. More...
 
- Public Member Functions inherited from shark::AbstractKernelFunction< InputType >
 AbstractKernelFunction ()
 
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasFirstParameterDerivative () const
 
bool hasFirstInputDerivative () const
 
bool isNormalized () const
 
bool supportsVariableInputSize () const
 
double operator() (ConstInputReference x1, ConstInputReference x2) const
 Convenience operator which evaluates the kernel function. More...
 
RealMatrix operator() (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2) const
 Evaluates the subset of the KernelGram matrix which is defined by X1(rows) and X2 (columns). More...
 
virtual double featureDistanceSqr (ConstInputReference x1, ConstInputReference x2) const
 Computes the squared distance in the kernel induced feature space. More...
 
virtual RealMatrix featureDistanceSqr (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2) const
 Computes the squared distance in the kernel induced feature space. More...
 
- Public Member Functions inherited from shark::AbstractMetric< InputType >
 AbstractMetric ()
 
virtual ~AbstractMetric ()
 
virtual double featureDistanceSqr (ConstInputReference x1, ConstInputReference x2) const=0
 Computes the squared distance in the kernel induced feature space. More...
 
virtual RealMatrix featureDistanceSqr (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2) const=0
 
double featureDistance (ConstInputReference x1, ConstInputReference x2) const
 Computes the distance in the kernel induced feature space. More...
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::IParameterizable<>
virtual ~IParameterizable ()
 
- 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 Member Functions

void updateNumberOfParameters ()
 
template<class T >
void weightedInputDerivativeImpl (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix const &coefficientsX2, State const &state, BatchInputType &gradient, typename boost::enable_if< boost::is_same< T, RealMatrix > >::type *dummy=0) const
 
template<class T >
void weightedInputDerivativeImpl (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix const &coefficientsX2, State const &state, BatchInputType &gradient, typename boost::disable_if< boost::is_same< T, RealMatrix > >::type *dummy=0) const
 

Protected Attributes

std::vector< tBasem_base
 collection of m_base kernels More...
 
double m_weightsum
 sum of all weights More...
 
std::size_t m_numParameters
 total number of parameters More...
 
bool m_adaptWeights
 whether the weights should be adapted More...
 
- Protected Attributes inherited from shark::AbstractKernelFunction< InputType >
Features m_features
 

Detailed Description

template<class InputType = RealVector>
class shark::WeightedSumKernel< InputType >

Weighted sum of kernel functions.

For a set of positive definite kernels \( k_1, \dots, k_n \) with positive coeffitients \( w_1, \dots, w_n \) the sum

\[ \tilde k(x_1, x_2) := \sum_{i=1}^{n} w_i \cdot k_i(x_1, x_2) \]

is again a positive definite kernel function. Internally, the weights are represented as \( w_i = \exp(\xi_i) \) to allow for unconstrained optimization.

This variant of the weighted sum kernel eleminates one redundant degree of freedom by fixing the first kernel weight to 1.0.

The result of the kernel evaluation is devided by the sum of the kernel weights, so that in total, this amounts to fixing the sum of the of the weights to one.

Definition at line 63 of file WeightedSumKernel.h.

Member Typedef Documentation

◆ BatchInputType

template<class InputType = RealVector>
typedef base_type::BatchInputType shark::WeightedSumKernel< InputType >::BatchInputType

Definition at line 84 of file WeightedSumKernel.h.

◆ ConstBatchInputReference

Definition at line 86 of file WeightedSumKernel.h.

◆ ConstInputReference

template<class InputType = RealVector>
typedef base_type::ConstInputReference shark::WeightedSumKernel< InputType >::ConstInputReference

Definition at line 85 of file WeightedSumKernel.h.

Constructor & Destructor Documentation

◆ WeightedSumKernel()

Member Function Documentation

◆ createState()

template<class InputType = RealVector>
boost::shared_ptr<State> shark::WeightedSumKernel< InputType >::createState ( ) const
inlinevirtual

creates the internal state of the kernel

Reimplemented from shark::AbstractKernelFunction< InputType >.

Definition at line 177 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base.

◆ eval() [1/3]

template<class InputType = RealVector>
double shark::WeightedSumKernel< InputType >::eval ( ConstInputReference  x1,
ConstInputReference  x2 
) const
inlinevirtual

Evaluate the weighted sum kernel (according to the following equation:) \( k(x, y) = \frac{\sum_i \exp(w_i) k_i(x, y)}{sum_i exp(w_i)} \)

Reimplemented from shark::AbstractKernelFunction< InputType >.

Definition at line 213 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::m_weightsum.

Referenced by main().

◆ eval() [2/3]

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::eval ( ConstBatchInputReference  batchX1,
ConstBatchInputReference  batchX2,
RealMatrix &  result 
) const
inlinevirtual

Evaluate the kernel according to the equation: \( k(x, y) = \frac{\sum_i \exp(w_i) k_i(x, y)}{sum_i exp(w_i)} \) for two batches of inputs.

Reimplemented from shark::AbstractKernelFunction< InputType >.

Definition at line 225 of file WeightedSumKernel.h.

References shark::batchSize(), shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::m_weightsum.

◆ eval() [3/3]

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::eval ( ConstBatchInputReference  batchX1,
ConstBatchInputReference  batchX2,
RealMatrix &  result,
State state 
) const
inlinevirtual

Evaluate the kernel according to the equation: \( k(x, y) = \frac{\sum_i \exp(w_i) k_i(x, y)}{sum_i exp(w_i)} \) for two batches of inputs. (see the documentation of numberOfIntermediateValues for the workings of the intermediates)

Implements shark::AbstractKernelFunction< InputType >.

Definition at line 243 of file WeightedSumKernel.h.

References shark::batchSize(), shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_weightsum, and shark::State::toState().

◆ isAdaptive()

template<class InputType = RealVector>
bool shark::WeightedSumKernel< InputType >::isAdaptive ( std::size_t  index) const
inline

Check whether m_base kernel index is adaptive.

Definition at line 130 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base.

Referenced by main().

◆ name()

template<class InputType = RealVector>
std::string shark::WeightedSumKernel< InputType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 126 of file WeightedSumKernel.h.

◆ numberOfParameters()

template<class InputType = RealVector>
std::size_t shark::WeightedSumKernel< InputType >::numberOfParameters ( ) const
inlinevirtual

◆ parameterVector()

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

return the parameter vector. The first N-1 entries are the (log-encoded) kernel weights, the sub-kernel's parameters are stacked behind each other after that.

Reimplemented from shark::IParameterizable<>.

Definition at line 157 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::numberOfParameters(), and shark::WeightedSumKernel< InputType >::weight().

Referenced by main().

◆ read()

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::read ( InArchive archive)
inlinevirtual

◆ setAdaptive()

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::setAdaptive ( std::size_t  index,
bool  b = true 
)
inline

Set adaptivity of m_base kernel index.

Definition at line 134 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::updateNumberOfParameters().

Referenced by main().

◆ setAdaptiveAll()

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::setAdaptiveAll ( bool  b = true)
inline

Set adaptivity of all m_base kernels.

Definition at line 139 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::updateNumberOfParameters().

Referenced by main().

◆ setAdaptiveWeights()

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::setAdaptiveWeights ( bool  b)
inline

◆ setParameterVector()

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

set the parameter vector. The first N-1 entries are the (log-encoded) kernel weights, the sub-kernel's parameters are stacked behind each other after that.

Reimplemented from shark::IParameterizable<>.

Definition at line 187 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_weightsum, shark::WeightedSumKernel< InputType >::numberOfParameters(), SIZE_CHECK, and w.

Referenced by main().

◆ updateNumberOfParameters()

◆ weight()

template<class InputType = RealVector>
double shark::WeightedSumKernel< InputType >::weight ( std::size_t  index)
inline

Get the weight of a kernel.

Definition at line 146 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, and RANGE_CHECK.

Referenced by shark::WeightedSumKernel< InputType >::parameterVector().

◆ weightedInputDerivative()

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::weightedInputDerivative ( ConstBatchInputReference  batchX1,
ConstBatchInputReference  batchX2,
RealMatrix const &  coefficientsX2,
State const &  state,
BatchInputType gradient 
) const
inlinevirtual

Input derivative, calculated according to the equation:
\( \frac{\partial k(x, y)}{\partial x} \frac{\sum_i \exp(w_i) \frac{\partial k_i(x, y)}{\partial x}} {\sum_i exp(w_i)} \) and summed up over all of the second batch

Reimplemented from shark::AbstractKernelFunction< InputType >.

Definition at line 306 of file WeightedSumKernel.h.

References shark::batchSize(), and SIZE_CHECK.

◆ weightedInputDerivativeImpl() [1/2]

template<class InputType = RealVector>
template<class T >
void shark::WeightedSumKernel< InputType >::weightedInputDerivativeImpl ( ConstBatchInputReference  batchX1,
ConstBatchInputReference  batchX2,
RealMatrix const &  coefficientsX2,
State const &  state,
BatchInputType gradient,
typename boost::enable_if< boost::is_same< T, RealMatrix > >::type *  dummy = 0 
) const
inlineprotected

◆ weightedInputDerivativeImpl() [2/2]

template<class InputType = RealVector>
template<class T >
void shark::WeightedSumKernel< InputType >::weightedInputDerivativeImpl ( ConstBatchInputReference  batchX1,
ConstBatchInputReference  batchX2,
RealMatrix const &  coefficientsX2,
State const &  state,
BatchInputType gradient,
typename boost::disable_if< boost::is_same< T, RealMatrix > >::type *  dummy = 0 
) const
inlineprotected

Definition at line 381 of file WeightedSumKernel.h.

References SHARKEXCEPTION.

◆ weightedParameterDerivative()

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::weightedParameterDerivative ( ConstBatchInputReference  batchX1,
ConstBatchInputReference  batchX2,
RealMatrix const &  coefficients,
State const &  state,
RealVector &  gradient 
) const
inlinevirtual

Computes the gradient of the parameters as a weighted sum over the gradient of all elements of the batch.

The default implementation throws a "not implemented" exception.

Reimplemented from shark::AbstractKernelFunction< InputType >.

Definition at line 261 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_weightsum, shark::WeightedSumKernel< InputType >::numberOfParameters(), shark::sqr(), and shark::State::toState().

◆ write()

template<class InputType = RealVector>
void shark::WeightedSumKernel< InputType >::write ( OutArchive archive) const
inlinevirtual

From ISerializable, writes a metric to an archive.

The default implementation just saves the parameters.

Reimplemented from shark::AbstractMetric< InputType >.

Definition at line 327 of file WeightedSumKernel.h.

References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_numParameters, and shark::WeightedSumKernel< InputType >::m_weightsum.

Member Data Documentation

◆ m_adaptWeights

template<class InputType = RealVector>
bool shark::WeightedSumKernel< InputType >::m_adaptWeights
protected

◆ m_base

◆ m_numParameters

◆ m_weightsum


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