shark::CSvmDerivative< InputType, CacheType > Class Template Reference

This class provides two main member functions for computing the derivative of a C-SVM hypothesis w.r.t. its hyperparameters. The constructor takes a pointer to a KernelClassifier and an SvmTrainer, in the assumption that the former was trained by the latter. It heavily accesses their members to calculate the derivative of the alpha and offset values w.r.t. the SVM hyperparameters, that is, the regularization parameter C and the kernel parameters. This is done in the member function prepareCSvmParameterDerivative called by the constructor. After this initial, heavier computation step, modelCSvmParameterDerivative can be called on an input sample to the SVM model, and the method will yield the derivative of the hypothesis w.r.t. the SVM hyperparameters. More...

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

+ Inheritance diagram for shark::CSvmDerivative< InputType, CacheType >:

Public Types

typedef CacheType QpFloatType
 
typedef KernelClassifier< InputTypeKeType
 
typedef AbstractKernelFunction< InputTypeKernelType
 
typedef CSvmTrainer< InputType, QpFloatTypeTrainerType
 

Public Member Functions

 CSvmDerivative (KeType *ke, TrainerType *trainer)
 
std::string name () const
 From INameable: return the class name. More...
 
const KeTypeke ()
 
const TrainerTypetrainer ()
 
void modelCSvmParameterDerivative (const InputType &input, RealVector &derivative)
 
bool hasFreeSVs ()
 
bool hasBoundedSVs ()
 
const RealMatrix & get_dalphab_dtheta ()
 Access to the matrix of SVM coefficients' derivatives. Derivative w.r.t. C is last. More...
 
virtual void read (InArchive &archive)
 From ISerializable, reads a network from an archive. More...
 
virtual void write (OutArchive &archive) const
 From ISerializable, writes a network to an archive. More...
 
- 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 ()
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 

Protected Attributes

KernelExpansion< InputType > * mep_ke
 pointer to the KernelExpansion which has to have been been trained by the below SvmTrainer More...
 
TrainerTypemep_tr
 pointer to the SvmTrainer with which the above KernelExpansion has to have been trained More...
 
KernelTypemep_k
 convenience pointer to the underlying kernel function More...
 
RealMatrix & m_alpha
 convenience reference to the alpha values of the KernelExpansion More...
 
const Data< InputType > & m_basis
 convenience reference to the underlying data of the KernelExpansion More...
 
const RealVector & m_db_dParams_from_solver
 convenience access to the correction term from the solver, for the rare case that there are no free SVs More...
 
double m_C
 the regularization parameter value with which the SvmTrainer trained the KernelExpansion More...
 
bool m_unconstrained
 is the unconstrained flag of the SvmTrainer set? Influences the derivatives! More...
 
std::size_t m_nkp
 convenience member holding the Number of Kernel Parameters. More...
 
std::size_t m_nhp
 convenience member holding the Number of Hyper Parameters. More...
 
std::size_t m_noofFreeSVs
 number of free SVs More...
 
std::size_t m_noofBoundedSVs
 number of bounded SVs More...
 
std::vector< std::size_t > m_freeAlphaIndices
 indices of free SVs More...
 
std::vector< std::size_t > m_boundedAlphaIndices
 indices of bounded SVs More...
 
RealVector m_freeAlphas
 free non-SV alpha values More...
 
RealVector m_boundedAlphas
 bounded non-SV alpha values More...
 
RealVector m_boundedLabels
 labels of bounded non-SVs More...
 
RealMatrix m_d_alphab_d_theta
 

Detailed Description

template<class InputType, class CacheType = double>
class shark::CSvmDerivative< InputType, CacheType >

This class provides two main member functions for computing the derivative of a C-SVM hypothesis w.r.t. its hyperparameters. The constructor takes a pointer to a KernelClassifier and an SvmTrainer, in the assumption that the former was trained by the latter. It heavily accesses their members to calculate the derivative of the alpha and offset values w.r.t. the SVM hyperparameters, that is, the regularization parameter C and the kernel parameters. This is done in the member function prepareCSvmParameterDerivative called by the constructor. After this initial, heavier computation step, modelCSvmParameterDerivative can be called on an input sample to the SVM model, and the method will yield the derivative of the hypothesis w.r.t. the SVM hyperparameters.

Template Parameters
InputTypeSame basis type as the kernel expansion's
CacheTypeWhile the basic cache type defaults to float in the QP algorithms, it here defaults to double, because the SVM derivative benefits a lot from higher precision.

Definition at line 71 of file CSvmDerivative.h.

Member Typedef Documentation

◆ KernelType

template<class InputType, class CacheType = double>
typedef AbstractKernelFunction<InputType> shark::CSvmDerivative< InputType, CacheType >::KernelType

Definition at line 76 of file CSvmDerivative.h.

◆ KeType

template<class InputType, class CacheType = double>
typedef KernelClassifier<InputType> shark::CSvmDerivative< InputType, CacheType >::KeType

Definition at line 75 of file CSvmDerivative.h.

◆ QpFloatType

template<class InputType, class CacheType = double>
typedef CacheType shark::CSvmDerivative< InputType, CacheType >::QpFloatType

Definition at line 74 of file CSvmDerivative.h.

◆ TrainerType

template<class InputType, class CacheType = double>
typedef CSvmTrainer<InputType, QpFloatType> shark::CSvmDerivative< InputType, CacheType >::TrainerType

Definition at line 77 of file CSvmDerivative.h.

Constructor & Destructor Documentation

◆ CSvmDerivative()

template<class InputType, class CacheType = double>
shark::CSvmDerivative< InputType, CacheType >::CSvmDerivative ( KeType ke,
TrainerType trainer 
)
inline

Constructor. Only sets up the main pointers and references to the external instances and data, and performs basic sanity checks.

Parameters
kepointer to the KernelExpansion which has to have been been trained by the below SvmTrainer
trainerpointer to the SvmTrainer with which the above KernelExpansion has to have been trained

Definition at line 118 of file CSvmDerivative.h.

References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::kernel(), shark::AbstractSvmTrainer< InputType, LabelType, Model, Trainer >::kernel(), shark::KernelExpansion< InputType >::outputShape(), and SHARK_RUNTIME_CHECK.

Member Function Documentation

◆ get_dalphab_dtheta()

template<class InputType, class CacheType = double>
const RealMatrix& shark::CSvmDerivative< InputType, CacheType >::get_dalphab_dtheta ( )
inline

Access to the matrix of SVM coefficients' derivatives. Derivative w.r.t. C is last.

Definition at line 206 of file CSvmDerivative.h.

References shark::CSvmDerivative< InputType, CacheType >::m_d_alphab_d_theta.

◆ hasBoundedSVs()

template<class InputType, class CacheType = double>
bool shark::CSvmDerivative< InputType, CacheType >::hasBoundedSVs ( )
inline

Whether there are bounded SVs in the solution. Useful to monitor for degenerate solutions with only bounded and no free SVs. Be sure to call prepareCSvmParameterDerivative after SVM training and before calling this function.

Definition at line 203 of file CSvmDerivative.h.

◆ hasFreeSVs()

template<class InputType, class CacheType = double>
bool shark::CSvmDerivative< InputType, CacheType >::hasFreeSVs ( )
inline

Whether there are free SVs in the solution. Useful to monitor for degenerate solutions with only bounded and no free SVs. Be sure to call prepareCSvmParameterDerivative after SVM training and before calling this function.

Definition at line 199 of file CSvmDerivative.h.

◆ ke()

template<class InputType, class CacheType = double>
const KeType* shark::CSvmDerivative< InputType, CacheType >::ke ( )
inline

◆ modelCSvmParameterDerivative()

template<class InputType, class CacheType = double>
void shark::CSvmDerivative< InputType, CacheType >::modelCSvmParameterDerivative ( const InputType input,
RealVector &  derivative 
)
inline

Computes the derivative of the model w.r.t. regularization and kernel parameters. Be sure to call prepareCSvmParameterDerivative after SVM training and before calling this function!

Parameters
inputan example to be scored by the SVM
derivativea vector of derivatives of the score. The last entry is w.r.t. C.

Definition at line 149 of file CSvmDerivative.h.

References shark::createBatch(), shark::AbstractKernelFunction< InputTypeT >::createState(), and shark::getBatchElement().

◆ name()

template<class InputType, class CacheType = double>
std::string shark::CSvmDerivative< InputType, CacheType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 139 of file CSvmDerivative.h.

◆ read()

template<class InputType, class CacheType = double>
virtual void shark::CSvmDerivative< InputType, CacheType >::read ( InArchive archive)
inlinevirtual

From ISerializable, reads a network from an archive.

Reimplemented from shark::ISerializable.

Definition at line 211 of file CSvmDerivative.h.

◆ trainer()

template<class InputType, class CacheType = double>
const TrainerType* shark::CSvmDerivative< InputType, CacheType >::trainer ( )
inline

◆ write()

Member Data Documentation

◆ m_alpha

template<class InputType, class CacheType = double>
RealMatrix& shark::CSvmDerivative< InputType, CacheType >::m_alpha
protected

convenience reference to the alpha values of the KernelExpansion

Definition at line 85 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::write().

◆ m_basis

template<class InputType, class CacheType = double>
const Data<InputType>& shark::CSvmDerivative< InputType, CacheType >::m_basis
protected

convenience reference to the underlying data of the KernelExpansion

Definition at line 86 of file CSvmDerivative.h.

◆ m_boundedAlphaIndices

template<class InputType, class CacheType = double>
std::vector< std::size_t > shark::CSvmDerivative< InputType, CacheType >::m_boundedAlphaIndices
protected

indices of bounded SVs

Definition at line 99 of file CSvmDerivative.h.

◆ m_boundedAlphas

template<class InputType, class CacheType = double>
RealVector shark::CSvmDerivative< InputType, CacheType >::m_boundedAlphas
protected

bounded non-SV alpha values

Definition at line 101 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::write().

◆ m_boundedLabels

template<class InputType, class CacheType = double>
RealVector shark::CSvmDerivative< InputType, CacheType >::m_boundedLabels
protected

labels of bounded non-SVs

Definition at line 102 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::write().

◆ m_C

template<class InputType, class CacheType = double>
double shark::CSvmDerivative< InputType, CacheType >::m_C
protected

the regularization parameter value with which the SvmTrainer trained the KernelExpansion

Definition at line 90 of file CSvmDerivative.h.

◆ m_d_alphab_d_theta

template<class InputType, class CacheType = double>
RealMatrix shark::CSvmDerivative< InputType, CacheType >::m_d_alphab_d_theta
protected

Main member and result, computed in the prepareDerivative-step: Stores the derivative of the free alphas w.r.t. SVM hyperparameters as obtained through the CSvmTrainer (for C) and through the kernel (for the kernel parameters). Each row corresponds to one free alpha, each column to one hyperparameter. The last column is the derivative of (free_alphas, b) w.r.t C. All previous columns are w.r.t. the kernel parameters.

Definition at line 110 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::get_dalphab_dtheta().

◆ m_db_dParams_from_solver

template<class InputType, class CacheType = double>
const RealVector& shark::CSvmDerivative< InputType, CacheType >::m_db_dParams_from_solver
protected

convenience access to the correction term from the solver, for the rare case that there are no free SVs

Definition at line 87 of file CSvmDerivative.h.

◆ m_freeAlphaIndices

template<class InputType, class CacheType = double>
std::vector< std::size_t > shark::CSvmDerivative< InputType, CacheType >::m_freeAlphaIndices
protected

indices of free SVs

Definition at line 98 of file CSvmDerivative.h.

◆ m_freeAlphas

template<class InputType, class CacheType = double>
RealVector shark::CSvmDerivative< InputType, CacheType >::m_freeAlphas
protected

free non-SV alpha values

Definition at line 100 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::write().

◆ m_nhp

template<class InputType, class CacheType = double>
std::size_t shark::CSvmDerivative< InputType, CacheType >::m_nhp
protected

convenience member holding the Number of Hyper Parameters.

Definition at line 93 of file CSvmDerivative.h.

◆ m_nkp

template<class InputType, class CacheType = double>
std::size_t shark::CSvmDerivative< InputType, CacheType >::m_nkp
protected

convenience member holding the Number of Kernel Parameters.

Definition at line 92 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::write().

◆ m_noofBoundedSVs

template<class InputType, class CacheType = double>
std::size_t shark::CSvmDerivative< InputType, CacheType >::m_noofBoundedSVs
protected

number of bounded SVs

Definition at line 97 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::write().

◆ m_noofFreeSVs

template<class InputType, class CacheType = double>
std::size_t shark::CSvmDerivative< InputType, CacheType >::m_noofFreeSVs
protected

number of free SVs

Definition at line 96 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::write().

◆ m_unconstrained

template<class InputType, class CacheType = double>
bool shark::CSvmDerivative< InputType, CacheType >::m_unconstrained
protected

is the unconstrained flag of the SvmTrainer set? Influences the derivatives!

Definition at line 91 of file CSvmDerivative.h.

◆ mep_k

template<class InputType, class CacheType = double>
KernelType* shark::CSvmDerivative< InputType, CacheType >::mep_k
protected

convenience pointer to the underlying kernel function

Definition at line 84 of file CSvmDerivative.h.

◆ mep_ke

template<class InputType, class CacheType = double>
KernelExpansion<InputType>* shark::CSvmDerivative< InputType, CacheType >::mep_ke
protected

pointer to the KernelExpansion which has to have been been trained by the below SvmTrainer

Definition at line 82 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::ke().

◆ mep_tr

template<class InputType, class CacheType = double>
TrainerType* shark::CSvmDerivative< InputType, CacheType >::mep_tr
protected

pointer to the SvmTrainer with which the above KernelExpansion has to have been trained

Definition at line 83 of file CSvmDerivative.h.

Referenced by shark::CSvmDerivative< InputType, CacheType >::trainer().


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