Weighted sum of kernel functions. More...
#include <shark/Models/Kernels/WeightedSumKernel.h>
Classes | |
struct | tBase |
structure describing a single m_base kernel More... | |
Public Member Functions | |
WeightedSumKernel (std::vector< AbstractKernelFunction< InputType > * > const &base) | |
std::string | name () const |
From INameable: return the class name. | |
bool | isAdaptive (std::size_t index) const |
Check whether m_base kernel index is adaptive. | |
void | setAdaptive (std::size_t index, bool b=true) |
Set adaptivity of m_base kernel index. | |
void | setAdaptiveAll (bool b=true) |
Set adaptivity of all m_base kernels. | |
double | weight (std::size_t index) |
Get the weight of a kernel. | |
void | setAdaptiveWeights (bool b) |
RealVector | parameterVector () const |
boost::shared_ptr< State > | createState () const |
creates the internal state of the kernel | |
void | setParameterVector (RealVector const &newParameters) |
std::size_t | numberOfParameters () const |
Return the number of parameters. | |
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 |
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. | |
void | write (OutArchive &ar) const |
From ISerializable, writes a metric to an archive. | |
Public Member Functions inherited from shark::AbstractKernelFunction< InputTypeT > | |
AbstractKernelFunction () | |
const Features & | features () const |
virtual void | updateFeatures () |
bool | hasFirstParameterDerivative () const |
bool | hasFirstInputDerivative () const |
bool | isNormalized () const |
bool | supportsVariableInputSize () const |
virtual double | eval (ConstInputReference x1, ConstInputReference x2) const |
Evaluates the kernel function. | |
double | operator() (ConstInputReference x1, ConstInputReference x2) const |
Convenience operator which evaluates the kernel function. | |
virtual void | eval (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix &result, State &state) const =0 |
Evaluates the subset of the KernelGram matrix which is defined by X1(rows) and X2 (columns). | |
virtual void | eval (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix &result) const |
Evaluates the subset of the KernelGram matrix which is defined by X1(rows) and X2 (columns). | |
RealMatrix | operator() (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2) const |
Evaluates the subset of the KernelGram matrix which is defined by X1(rows) and X2 (columns). | |
virtual 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. | |
virtual void | weightedInputDerivative (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix const &coefficientsX2, State const &state, BatchInputType &gradient) const |
Calculates the derivative of the inputs X1 (only x1!). | |
virtual double | featureDistanceSqr (ConstInputReference x1, ConstInputReference x2) const |
Computes the squared distance in the kernel induced feature space. | |
virtual RealMatrix | featureDistanceSqr (ConstBatchInputReference batchX1, ConstBatchInputReference batchX2) const |
Computes the squared distance in the kernel induced feature space. | |
Public Member Functions inherited from shark::AbstractMetric< InputTypeT > | |
AbstractMetric () | |
virtual | ~AbstractMetric () |
double | featureDistance (ConstInputReference x1, ConstInputReference x2) const |
Computes the distance in the kernel induced feature space. | |
Public Member Functions inherited from shark::INameable | |
virtual | ~INameable () |
Public Member Functions inherited from shark::IParameterizable< VectorType > | |
virtual | ~IParameterizable () |
Public Member Functions inherited from shark::ISerializable | |
virtual | ~ISerializable () |
Virtual d'tor. | |
void | load (InArchive &archive, unsigned int version) |
Versioned loading of components, calls read(...). | |
void | save (OutArchive &archive, unsigned int version) const |
Versioned storing of components, calls write(...). | |
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 | |
double | m_weightsum |
sum of all weights | |
std::size_t | m_numParameters |
total number of parameters | |
bool | m_adaptWeights |
whether the weights should be adapted | |
Protected Attributes inherited from shark::AbstractKernelFunction< InputTypeT > | |
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 64 of file WeightedSumKernel.h.
typedef base_type::BatchInputType shark::WeightedSumKernel< InputType >::BatchInputType |
Definition at line 85 of file WeightedSumKernel.h.
typedef base_type::ConstBatchInputReference shark::WeightedSumKernel< InputType >::ConstBatchInputReference |
Definition at line 87 of file WeightedSumKernel.h.
typedef base_type::ConstInputReference shark::WeightedSumKernel< InputType >::ConstInputReference |
Definition at line 86 of file WeightedSumKernel.h.
|
inline |
Definition at line 89 of file WeightedSumKernel.h.
References shark::AbstractKernelFunction< InputType >::HAS_FIRST_INPUT_DERIVATIVE, shark::AbstractKernelFunction< InputType >::HAS_FIRST_PARAMETER_DERIVATIVE, shark::AbstractKernelFunction< InputTypeT >::hasFirstInputDerivative(), shark::AbstractKernelFunction< InputTypeT >::hasFirstParameterDerivative(), shark::WeightedSumKernel< InputType >::m_adaptWeights, shark::WeightedSumKernel< InputType >::m_base, shark::AbstractKernelFunction< InputTypeT >::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< InputTypeT >.
Definition at line 178 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base.
|
inline |
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.
Definition at line 226 of file WeightedSumKernel.h.
References shark::batchSize(), shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::m_weightsum.
|
inline |
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)
Definition at line 244 of file WeightedSumKernel.h.
References shark::batchSize(), shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_weightsum, and shark::State::toState().
|
inline |
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)} \)
Definition at line 214 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::m_weightsum.
Referenced by main().
|
inline |
Check whether m_base kernel index is adaptive.
Definition at line 131 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base.
Referenced by main(), and shark::WeightedSumKernel< InputType >::weightedParameterDerivative().
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 127 of file WeightedSumKernel.h.
|
inlinevirtual |
Return the number of parameters.
Reimplemented from shark::IParameterizable< VectorType >.
Definition at line 208 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< VectorType >.
Definition at line 158 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< InputTypeT >.
Definition at line 319 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 135 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 140 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base, and shark::WeightedSumKernel< InputType >::updateNumberOfParameters().
Referenced by main().
|
inline |
Definition at line 152 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< VectorType >.
Definition at line 188 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_weightsum, shark::WeightedSumKernel< InputType >::numberOfParameters(), and SIZE_CHECK.
Referenced by main().
|
inlineprotected |
Definition at line 347 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 147 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base, and RANGE_CHECK.
Referenced by shark::WeightedSumKernel< InputType >::parameterVector().
|
inline |
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
Definition at line 307 of file WeightedSumKernel.h.
References shark::batchSize(), and SIZE_CHECK.
|
inlineprotected |
Definition at line 382 of file WeightedSumKernel.h.
References SHARKEXCEPTION.
|
inlineprotected |
Definition at line 359 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::m_base, shark::WeightedSumKernel< InputType >::m_weightsum, and shark::State::toState().
|
inline |
Definition at line 262 of file WeightedSumKernel.h.
References shark::WeightedSumKernel< InputType >::isAdaptive(), shark::WeightedSumKernel< InputType >::m_adaptWeights, 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< InputTypeT >.
Definition at line 328 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 396 of file WeightedSumKernel.h.
Referenced by shark::WeightedSumKernel< InputType >::setAdaptiveWeights(), shark::WeightedSumKernel< InputType >::updateNumberOfParameters(), shark::WeightedSumKernel< InputType >::weightedParameterDerivative(), and shark::WeightedSumKernel< InputType >::WeightedSumKernel().
|
protected |
collection of m_base kernels
Definition at line 393 of file WeightedSumKernel.h.
Referenced by shark::WeightedSumKernel< InputType >::createState(), shark::WeightedSumKernel< InputType >::eval(), shark::WeightedSumKernel< InputType >::eval(), 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 395 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 394 of file WeightedSumKernel.h.
Referenced by shark::WeightedSumKernel< InputType >::eval(), shark::WeightedSumKernel< InputType >::eval(), 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().