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>
Public Types | |
typedef CacheType | QpFloatType |
typedef KernelClassifier< InputType > | KeType |
typedef AbstractKernelFunction< InputType > | KernelType |
typedef CSvmTrainer< InputType, QpFloatType > | TrainerType |
Public Member Functions | |
CSvmDerivative (KeType *ke, TrainerType *trainer) | |
std::string | name () const |
From INameable: return the class name. More... | |
const KeType * | ke () |
const TrainerType * | trainer () |
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... | |
TrainerType * | mep_tr |
pointer to the SvmTrainer with which the above KernelExpansion has to have been trained More... | |
KernelType * | mep_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 |
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.
InputType | Same basis type as the kernel expansion's |
CacheType | While 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.
typedef AbstractKernelFunction<InputType> shark::CSvmDerivative< InputType, CacheType >::KernelType |
Definition at line 76 of file CSvmDerivative.h.
typedef KernelClassifier<InputType> shark::CSvmDerivative< InputType, CacheType >::KeType |
Definition at line 75 of file CSvmDerivative.h.
typedef CacheType shark::CSvmDerivative< InputType, CacheType >::QpFloatType |
Definition at line 74 of file CSvmDerivative.h.
typedef CSvmTrainer<InputType, QpFloatType> shark::CSvmDerivative< InputType, CacheType >::TrainerType |
Definition at line 77 of file CSvmDerivative.h.
|
inline |
Constructor. Only sets up the main pointers and references to the external instances and data, and performs basic sanity checks.
ke | pointer to the KernelExpansion which has to have been been trained by the below SvmTrainer |
trainer | pointer 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.
|
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.
|
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.
|
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.
|
inline |
Definition at line 142 of file CSvmDerivative.h.
References shark::CSvmDerivative< InputType, CacheType >::mep_ke.
|
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!
input | an example to be scored by the SVM |
derivative | a 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().
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 139 of file CSvmDerivative.h.
|
inlinevirtual |
From ISerializable, reads a network from an archive.
Reimplemented from shark::ISerializable.
Definition at line 211 of file CSvmDerivative.h.
|
inline |
Definition at line 143 of file CSvmDerivative.h.
References shark::CSvmDerivative< InputType, CacheType >::mep_tr.
|
inlinevirtual |
From ISerializable, writes a network to an archive.
Reimplemented from shark::ISerializable.
Definition at line 214 of file CSvmDerivative.h.
References shark::createBatch(), shark::AbstractKernelFunction< InputTypeT >::createState(), shark::Data< Type >::element(), shark::AbstractKernelFunction< InputTypeT >::eval(), shark::getBatchElement(), shark::CSvmDerivative< InputType, CacheType >::m_alpha, shark::CSvmDerivative< InputType, CacheType >::m_boundedAlphas, shark::CSvmDerivative< InputType, CacheType >::m_boundedLabels, shark::CSvmDerivative< InputType, CacheType >::m_freeAlphas, shark::CSvmDerivative< InputType, CacheType >::m_nkp, shark::CSvmDerivative< InputType, CacheType >::m_noofBoundedSVs, shark::CSvmDerivative< InputType, CacheType >::m_noofFreeSVs, shark::KernelExpansion< InputType >::offset(), SHARKEXCEPTION, and shark::AbstractKernelFunction< InputTypeT >::weightedParameterDerivative().
|
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().
|
protected |
convenience reference to the underlying data of the KernelExpansion
Definition at line 86 of file CSvmDerivative.h.
|
protected |
indices of bounded SVs
Definition at line 99 of file CSvmDerivative.h.
|
protected |
bounded non-SV alpha values
Definition at line 101 of file CSvmDerivative.h.
Referenced by shark::CSvmDerivative< InputType, CacheType >::write().
|
protected |
labels of bounded non-SVs
Definition at line 102 of file CSvmDerivative.h.
Referenced by shark::CSvmDerivative< InputType, CacheType >::write().
|
protected |
the regularization parameter value with which the SvmTrainer trained the KernelExpansion
Definition at line 90 of file CSvmDerivative.h.
|
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().
|
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.
|
protected |
indices of free SVs
Definition at line 98 of file CSvmDerivative.h.
|
protected |
free non-SV alpha values
Definition at line 100 of file CSvmDerivative.h.
Referenced by shark::CSvmDerivative< InputType, CacheType >::write().
|
protected |
convenience member holding the Number of Hyper Parameters.
Definition at line 93 of file CSvmDerivative.h.
|
protected |
convenience member holding the Number of Kernel Parameters.
Definition at line 92 of file CSvmDerivative.h.
Referenced by shark::CSvmDerivative< InputType, CacheType >::write().
|
protected |
number of bounded SVs
Definition at line 97 of file CSvmDerivative.h.
Referenced by shark::CSvmDerivative< InputType, CacheType >::write().
|
protected |
number of free SVs
Definition at line 96 of file CSvmDerivative.h.
Referenced by shark::CSvmDerivative< InputType, CacheType >::write().
|
protected |
is the unconstrained flag of the SvmTrainer set? Influences the derivatives!
Definition at line 91 of file CSvmDerivative.h.
|
protected |
convenience pointer to the underlying kernel function
Definition at line 84 of file CSvmDerivative.h.
|
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().
|
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().