Weighted sum of kernel functions. More...
#include <shark/Models/Kernels/WeightedSumKernel.h>
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< Feature > | Features |
This statement declares the member m_features. See Core/Flags.h for details. More... | |
typedef TypedFeatureNotAvailableException< Feature > | FeatureNotAvailableException |
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< State > | createState () 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 Features & | features () 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< tBase > | m_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 |
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.
typedef base_type::BatchInputType shark::WeightedSumKernel< InputType >::BatchInputType |
Definition at line 84 of file WeightedSumKernel.h.
typedef base_type::ConstBatchInputReference shark::WeightedSumKernel< InputType >::ConstBatchInputReference |
Definition at line 86 of file WeightedSumKernel.h.
typedef base_type::ConstInputReference shark::WeightedSumKernel< InputType >::ConstInputReference |
Definition at line 85 of file WeightedSumKernel.h.
|
inline |
Definition at line 88 of file WeightedSumKernel.h.
References shark::AbstractKernelFunction< InputType >::HAS_FIRST_INPUT_DERIVATIVE, shark::AbstractKernelFunction< InputType >::HAS_FIRST_PARAMETER_DERIVATIVE, shark::AbstractKernelFunction< InputType >::hasFirstInputDerivative(), shark::AbstractKernelFunction< InputType >::hasFirstParameterDerivative(), shark::WeightedSumKernel< InputType >::m_adaptWeights, shark::WeightedSumKernel< InputType >::m_base, shark::AbstractKernelFunction< InputType >::m_features, shark::WeightedSumKernel< InputType >::m_numParameters, shark::WeightedSumKernel< InputType >::m_weightsum, SHARK_ASSERT, and SHARK_RUNTIME_CHECK.
|
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.
|
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().
|
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.
|
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().
|
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().
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 126 of file WeightedSumKernel.h.
|
inlinevirtual |
Return the number of parameters.
Reimplemented from shark::IParameterizable<>.
Definition at line 207 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_numParameters.
Referenced by main(), shark::WeightedSumKernel< InputType >::parameterVector(), shark::WeightedSumKernel< InputType >::setParameterVector(), and shark::WeightedSumKernel< InputType >::weightedParameterDerivative().
|
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().
|
inlinevirtual |
From ISerializable, reads a metric from an archive.
Reimplemented from shark::AbstractMetric< InputType >.
Definition at line 318 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_numParameters, and shark::WeightedSumKernel< InputType >::m_weightsum.
|
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().
|
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().
|
inline |
Definition at line 151 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_adaptWeights.
|
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().
|
inlineprotected |
Definition at line 346 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_adaptWeights, shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::m_numParameters.
Referenced by shark::WeightedSumKernel< InputType >::setAdaptive(), and shark::WeightedSumKernel< InputType >::setAdaptiveAll().
|
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().
|
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.
|
inlineprotected |
Definition at line 358 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_weightsum, and shark::State::toState().
|
inlineprotected |
Definition at line 381 of file WeightedSumKernel.h.
References SHARKEXCEPTION.
|
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().
|
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.
|
protected |
whether the weights should be adapted
Definition at line 395 of file WeightedSumKernel.h.
Referenced by shark::WeightedSumKernel< InputType >::setAdaptiveWeights(), shark::WeightedSumKernel< InputType >::updateNumberOfParameters(), and shark::WeightedSumKernel< InputType >::WeightedSumKernel().
|
protected |
collection of m_base kernels
Definition at line 392 of file WeightedSumKernel.h.
Referenced by shark::WeightedSumKernel< InputType >::createState(), shark::WeightedSumKernel< InputType >::eval(), shark::WeightedSumKernel< InputType >::isAdaptive(), shark::WeightedSumKernel< InputType >::parameterVector(), shark::WeightedSumKernel< InputType >::read(), shark::WeightedSumKernel< InputType >::setAdaptive(), shark::WeightedSumKernel< InputType >::setAdaptiveAll(), shark::WeightedSumKernel< InputType >::setParameterVector(), shark::WeightedSumKernel< InputType >::updateNumberOfParameters(), shark::WeightedSumKernel< InputType >::weight(), shark::WeightedSumKernel< InputType >::weightedInputDerivativeImpl(), shark::WeightedSumKernel< InputType >::weightedParameterDerivative(), shark::WeightedSumKernel< InputType >::WeightedSumKernel(), and shark::WeightedSumKernel< InputType >::write().
|
protected |
total number of parameters
Definition at line 394 of file WeightedSumKernel.h.
Referenced by shark::WeightedSumKernel< InputType >::numberOfParameters(), shark::WeightedSumKernel< InputType >::read(), shark::WeightedSumKernel< InputType >::updateNumberOfParameters(), shark::WeightedSumKernel< InputType >::WeightedSumKernel(), and shark::WeightedSumKernel< InputType >::write().
|
protected |
sum of all weights
Definition at line 393 of file WeightedSumKernel.h.
Referenced by shark::WeightedSumKernel< InputType >::eval(), shark::WeightedSumKernel< InputType >::read(), shark::WeightedSumKernel< InputType >::setParameterVector(), shark::WeightedSumKernel< InputType >::weightedInputDerivativeImpl(), shark::WeightedSumKernel< InputType >::weightedParameterDerivative(), shark::WeightedSumKernel< InputType >::WeightedSumKernel(), and shark::WeightedSumKernel< InputType >::write().