SHOGUN
v1.1.0
|
The Kernel base class.
Non-mathematically spoken, a kernel is a function that given two input objects and
returns a score describing the similarity of the vectors. The score should be larger when the objects are more similar.
It can be defined as
where maps the objects into some potentially high dimensional feature space.
Apart from the input features, the base kernel takes only one argument (the size of the kernel cache) that is used to efficiently train kernel-machines like e.g. SVMs.
In case you would like to define your own kernel, you only have to define a new compute() function (and the kernel name via get_name() and the kernel type get_kernel_type()). A good example to look at is the GaussianKernel.
Public Member Functions | |
CKernel () | |
CKernel (int32_t size) | |
CKernel (CFeatures *l, CFeatures *r, int32_t size) | |
virtual | ~CKernel () |
float64_t | kernel (int32_t idx_a, int32_t idx_b) |
SGMatrix< float64_t > | get_kernel_matrix () |
virtual SGVector< float64_t > | get_kernel_col (int32_t j) |
virtual SGVector< float64_t > | get_kernel_row (int32_t i) |
template<class T > | |
SGMatrix< T > | get_kernel_matrix () |
virtual bool | init (CFeatures *lhs, CFeatures *rhs) |
virtual bool | set_normalizer (CKernelNormalizer *normalizer) |
virtual CKernelNormalizer * | get_normalizer () |
virtual bool | init_normalizer () |
virtual void | cleanup () |
void | load (CFile *loader) |
void | save (CFile *writer) |
CFeatures * | get_lhs () |
CFeatures * | get_rhs () |
virtual int32_t | get_num_vec_lhs () |
virtual int32_t | get_num_vec_rhs () |
virtual bool | has_features () |
bool | get_lhs_equals_rhs () |
virtual void | remove_lhs_and_rhs () |
virtual void | remove_lhs () |
virtual void | remove_rhs () |
takes all necessary steps if the rhs is removed from kernel | |
virtual EKernelType | get_kernel_type ()=0 |
virtual EFeatureType | get_feature_type ()=0 |
virtual EFeatureClass | get_feature_class ()=0 |
void | set_cache_size (int32_t size) |
int32_t | get_cache_size () |
void | list_kernel () |
bool | has_property (EKernelProperty p) |
virtual void | clear_normal () |
virtual void | add_to_normal (int32_t vector_idx, float64_t weight) |
EOptimizationType | get_optimization_type () |
virtual void | set_optimization_type (EOptimizationType t) |
bool | get_is_initialized () |
virtual bool | init_optimization (int32_t count, int32_t *IDX, float64_t *weights) |
virtual bool | delete_optimization () |
bool | init_optimization_svm (CSVM *svm) |
virtual float64_t | compute_optimized (int32_t vector_idx) |
virtual void | compute_batch (int32_t num_vec, int32_t *vec_idx, float64_t *target, int32_t num_suppvec, int32_t *IDX, float64_t *alphas, float64_t factor=1.0) |
float64_t | get_combined_kernel_weight () |
void | set_combined_kernel_weight (float64_t nw) |
virtual int32_t | get_num_subkernels () |
virtual void | compute_by_subkernel (int32_t vector_idx, float64_t *subkernel_contrib) |
virtual const float64_t * | get_subkernel_weights (int32_t &num_weights) |
virtual void | set_subkernel_weights (SGVector< float64_t > weights) |
![]() | |
CSGObject () | |
CSGObject (const CSGObject &orig) | |
virtual | ~CSGObject () |
virtual const char * | get_name () const =0 |
virtual bool | is_generic (EPrimitiveType *generic) const |
template<class T > | |
void | set_generic () |
void | unset_generic () |
virtual void | print_serializable (const char *prefix="") |
virtual bool | save_serializable (CSerializableFile *file, const char *prefix="") |
virtual bool | load_serializable (CSerializableFile *file, const char *prefix="") |
void | set_global_io (SGIO *io) |
SGIO * | get_global_io () |
void | set_global_parallel (Parallel *parallel) |
Parallel * | get_global_parallel () |
void | set_global_version (Version *version) |
Version * | get_global_version () |
SGVector< char * > | get_modelsel_names () |
char * | get_modsel_param_descr (const char *param_name) |
index_t | get_modsel_param_index (const char *param_name) |
Protected Member Functions | |
void | set_property (EKernelProperty p) |
void | unset_property (EKernelProperty p) |
void | set_is_initialized (bool p_init) |
virtual float64_t | compute (int32_t x, int32_t y)=0 |
int32_t | compute_row_start (int64_t offs, int32_t n, bool symmetric) |
virtual void | load_serializable_post () throw (ShogunException) |
virtual void | save_serializable_pre () throw (ShogunException) |
virtual void | save_serializable_post () throw (ShogunException) |
virtual void | register_params () |
![]() | |
virtual void | load_serializable_pre () throw (ShogunException) |
Static Protected Member Functions | |
template<class T > | |
static void * | get_kernel_matrix_helper (void *p) |
Protected Attributes | |
int32_t | cache_size |
cache_size in MB | |
KERNELCACHE_ELEM * | kernel_matrix |
CFeatures * | lhs |
feature vectors to occur on left hand side | |
CFeatures * | rhs |
feature vectors to occur on right hand side | |
bool | lhs_equals_rhs |
lhs | |
int32_t | num_lhs |
number of feature vectors on left hand side | |
int32_t | num_rhs |
number of feature vectors on right hand side | |
float64_t | combined_kernel_weight |
bool | optimization_initialized |
EOptimizationType | opt_type |
uint64_t | properties |
CKernelNormalizer * | normalizer |
Friends | |
class | CVarianceKernelNormalizer |
class | CSqrtDiagKernelNormalizer |
class | CAvgDiagKernelNormalizer |
class | CRidgeKernelNormalizer |
class | CFirstElementKernelNormalizer |
class | CMultitaskKernelNormalizer |
class | CMultitaskKernelMklNormalizer |
class | CMultitaskKernelMaskNormalizer |
class | CMultitaskKernelMaskPairNormalizer |
class | CTanimotoKernelNormalizer |
class | CDiceKernelNormalizer |
class | CZeroMeanCenterKernelNormalizer |
Additional Inherited Members | |
![]() | |
SGIO * | io |
Parallel * | parallel |
Version * | version |
Parameter * | m_parameters |
Parameter * | m_model_selection_parameters |
CKernel | ( | ) |
default constructor
Definition at line 38 of file Kernel.cpp.
CKernel | ( | int32_t | size | ) |
constructor
l | features for left-hand side |
r | features for right-hand side |
size | cache size |
Definition at line 56 of file Kernel.cpp.
|
virtual |
Definition at line 70 of file Kernel.cpp.
|
virtual |
add vector*factor to 'virtual' normal vector
vector_idx | index |
weight | weight |
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, CWeightedDegreePositionStringKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearKernel, CLinearStringKernel, and CWeightedCommWordStringKernel.
Definition at line 330 of file Kernel.cpp.
|
virtual |
clean up your kernel
base method only removes lhs and rhs overload to add further cleanup but make sure CKernel::cleanup() is called
Reimplemented in CANOVAKernel, CWeightedDegreePositionStringKernel, CWeightedDegreeStringKernel, CCommWordStringKernel, CPyramidChi2, CSpectrumMismatchRBFKernel, CWeightedCommWordStringKernel, CSalzbergWordStringKernel, COligoStringKernel, CCommUlongStringKernel, CGaussianKernel, CCustomKernel, CBesselKernel, CSNPStringKernel, CPolyKernel, CPolyMatchStringKernel, CPolyMatchWordStringKernel, CSimpleLocalityImprovedStringKernel, CSpectrumRBFKernel, CWaveletKernel, CCombinedKernel, CExponentialKernel, CGaussianMatchStringKernel, CFixedDegreeStringKernel, CSigmoidKernel, CSplineKernel, CHistogramWordStringKernel, CSparseSpatialSampleStringKernel, CLinearKernel, CLocalAlignmentStringKernel, and CLinearStringKernel.
Definition at line 138 of file Kernel.cpp.
|
virtual |
for optimizable kernels, i.e. kernels where the weight vector can be computed explicitly (if it fits into memory)
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, CWeightedDegreePositionStringKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearKernel, and CLinearStringKernel.
Definition at line 335 of file Kernel.cpp.
|
protectedpure virtual |
compute kernel function for features a and b idx_{a,b} denote the index of the feature vectors in the corresponding feature object
abstract base method
x | index a |
y | index b |
Implemented in CWeightedDegreeStringKernel, CWeightedDegreePositionStringKernel, CCombinedKernel, CCustomKernel, CCommWordStringKernel, CCommUlongStringKernel, CSpectrumMismatchRBFKernel, CBesselKernel, CSNPStringKernel, CGaussianKernel, CSpectrumRBFKernel, CCircularKernel, CSphericalKernel, CPyramidChi2, CDistanceKernel, CDotKernel, CANOVAKernel, CPolyMatchStringKernel, CSparseSpatialSampleStringKernel, CLinearStringKernel, CSimpleLocalityImprovedStringKernel, CTStudentKernel, CInverseMultiQuadricKernel, CSalzbergWordStringKernel, CTensorProductPairKernel, CHistogramIntersectionKernel, CRationalQuadraticKernel, CAUCKernel, CChi2Kernel, CExponentialKernel, CRegulatoryModulesStringKernel, CWeightedDegreeRBFKernel, CMultiquadricKernel, CDiagKernel, CGaussianMatchStringKernel, CConstKernel, CPolyKernel, CPolyMatchWordStringKernel, CCauchyKernel, CDistantSegmentsKernel, CFixedDegreeStringKernel, CLogKernel, CPowerKernel, CWaveKernel, CWaveletKernel, CGaussianShiftKernel, CLocalityImprovedStringKernel, CMatchWordStringKernel, CSigmoidKernel, CGaussianShortRealKernel, CLocalAlignmentStringKernel, CHistogramWordStringKernel, COligoStringKernel, and CSplineKernel.
|
virtual |
computes output for a batch of examples in an optimized fashion (favorable if kernel supports it, i.e. has KP_BATCHEVALUATION. to the outputvector target (of length num_vec elements) the output for the examples enumerated in vec_idx are added. therefore make sure that it is initialized with ZERO. the following num_suppvec, IDX, alphas arguments are the number of support vectors, their indices and weights
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, and CWeightedDegreePositionStringKernel.
Definition at line 323 of file Kernel.cpp.
|
virtual |
compute by subkernel
vector_idx | index |
subkernel_contrib | subkernel contribution |
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, and CWeightedDegreePositionStringKernel.
Definition at line 345 of file Kernel.cpp.
|
virtual |
compute optimized
vector_idx | index to compute |
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, CWeightedDegreePositionStringKernel, CCommWordStringKernel, CCommUlongStringKernel, CLinearKernel, CLinearStringKernel, and CWeightedCommWordStringKernel.
Definition at line 317 of file Kernel.cpp.
|
protected |
|
virtual |
delete optimization
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, CWeightedDegreePositionStringKernel, CCommWordStringKernel, CCommUlongStringKernel, CLinearKernel, and CLinearStringKernel.
Definition at line 311 of file Kernel.cpp.
int32_t get_cache_size | ( | ) |
float64_t get_combined_kernel_weight | ( | ) |
|
pure virtual |
return feature class the kernel can deal with
abstract base method
Implemented in CBesselKernel, CCustomKernel, CCombinedKernel, CTensorProductPairKernel, CDistanceKernel, CDotKernel, CSphericalKernel, CAUCKernel, CCircularKernel, CANOVAKernel, CTStudentKernel, CChi2Kernel, CConstKernel, CCauchyKernel, CLogKernel, CPowerKernel, CRationalQuadraticKernel, CWaveKernel, CInverseMultiQuadricKernel, CDiagKernel, CMultiquadricKernel, CSparseKernel< ST >, CStringKernel< ST >, CWeightedDegreeRBFKernel, CStringKernel< uint16_t >, CStringKernel< char >, and CStringKernel< uint64_t >.
|
pure virtual |
return feature type the kernel can deal with
abstract base method
Implemented in CCommUlongStringKernel, CCommWordStringKernel, CWeightedCommWordStringKernel, CSparseKernel< ST >, CStringKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CDotKernel, CTensorProductPairKernel, CBesselKernel, CCustomKernel, CAUCKernel, CCombinedKernel, CChi2Kernel, CDistanceKernel, CSphericalKernel, CCircularKernel, CSparseKernel< ST >, CANOVAKernel, CStringKernel< ST >, CWeightedDegreeRBFKernel, CStringKernel< uint16_t >, CStringKernel< char >, CStringKernel< uint64_t >, CTStudentKernel, CCauchyKernel, CLogKernel, CPowerKernel, CRationalQuadraticKernel, CWaveKernel, CConstKernel, CInverseMultiQuadricKernel, CMultiquadricKernel, and CDiagKernel.
bool get_is_initialized | ( | ) |
SGMatrix<T> get_kernel_matrix | ( | ) |
|
staticprotected |
|
pure virtual |
return what type of kernel we are, e.g. Linear,Polynomial, Gaussian,...
abstract base method
Implemented in CWeightedCommWordStringKernel, CWeightedDegreeStringKernel, CDotKernel, CWeightedDegreePositionStringKernel, CCommWordStringKernel, CSpectrumMismatchRBFKernel, CPyramidChi2, CSparseKernel< ST >, CStringKernel< ST >, CStringKernel< uint16_t >, CStringKernel< char >, CStringKernel< uint64_t >, CSalzbergWordStringKernel, CCommUlongStringKernel, CGaussianKernel, CBesselKernel, CCustomKernel, CSpectrumRBFKernel, CSNPStringKernel, CDiagKernel, CPolyMatchStringKernel, CPolyKernel, CPolyMatchWordStringKernel, CDistanceKernel, CSimpleLocalityImprovedStringKernel, CCombinedKernel, CGaussianMatchStringKernel, CWaveletKernel, CExponentialKernel, CFixedDegreeStringKernel, CGaussianShiftKernel, CSphericalKernel, CCircularKernel, CLocalityImprovedStringKernel, CMatchWordStringKernel, CRegulatoryModulesStringKernel, CTensorProductPairKernel, CAUCKernel, CChi2Kernel, CDistantSegmentsKernel, CHistogramIntersectionKernel, CSigmoidKernel, CANOVAKernel, COligoStringKernel, CGaussianShortRealKernel, CTStudentKernel, CSplineKernel, CCauchyKernel, CHistogramWordStringKernel, CLogKernel, CPowerKernel, CRationalQuadraticKernel, CSparseSpatialSampleStringKernel, CWaveKernel, CLinearKernel, CLocalAlignmentStringKernel, CInverseMultiQuadricKernel, CConstKernel, CLinearStringKernel, CMultiquadricKernel, and CWeightedDegreeRBFKernel.
CFeatures* get_lhs | ( | ) |
bool get_lhs_equals_rhs | ( | ) |
|
virtual |
obtain the current kernel normalizer
Definition at line 127 of file Kernel.cpp.
|
virtual |
get number of subkernels
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, and CWeightedDegreePositionStringKernel.
Definition at line 340 of file Kernel.cpp.
|
virtual |
get number of vectors of lhs features
Reimplemented in CCustomKernel.
|
virtual |
get number of vectors of rhs features
Reimplemented in CCustomKernel.
EOptimizationType get_optimization_type | ( | ) |
CFeatures* get_rhs | ( | ) |
|
virtual |
get subkernel weights
num_weights | number of weights will be stored here |
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, and CWeightedDegreePositionStringKernel.
Definition at line 351 of file Kernel.cpp.
|
virtual |
test whether features have been assigned to lhs and rhs
Reimplemented in CCustomKernel, and CCombinedKernel.
bool has_property | ( | EKernelProperty | p | ) |
initialize kernel e.g. setup lhs/rhs of kernel, precompute normalization constants etc. make sure to check that your kernel can deal with the supplied features (!)
lhs | features for left-hand side |
rhs | features for right-hand side |
Reimplemented in CWeightedDegreePositionStringKernel, CWeightedDegreeStringKernel, CCommWordStringKernel, CSpectrumMismatchRBFKernel, CPyramidChi2, CWeightedCommWordStringKernel, CSalzbergWordStringKernel, CCommUlongStringKernel, CGaussianKernel, CCustomKernel, CBesselKernel, CSNPStringKernel, CPolyKernel, CPolyMatchStringKernel, CPolyMatchWordStringKernel, CCircularKernel, CSphericalKernel, CWaveletKernel, CLocalityImprovedStringKernel, CMatchWordStringKernel, CRegulatoryModulesStringKernel, CSimpleLocalityImprovedStringKernel, CSpectrumRBFKernel, CTensorProductPairKernel, CANOVAKernel, CAUCKernel, CChi2Kernel, CCombinedKernel, CDistanceKernel, CDistantSegmentsKernel, CExponentialKernel, CGaussianMatchStringKernel, CDotKernel, CFixedDegreeStringKernel, COligoStringKernel, CTStudentKernel, CGaussianShortRealKernel, CHistogramIntersectionKernel, CSigmoidKernel, CCauchyKernel, CLogKernel, CPowerKernel, CRationalQuadraticKernel, CWaveKernel, CInverseMultiQuadricKernel, CSplineKernel, CHistogramWordStringKernel, CMultiquadricKernel, CSparseSpatialSampleStringKernel, CConstKernel, CDiagKernel, CLinearKernel, CLocalAlignmentStringKernel, CStringKernel< ST >, CStringKernel< uint16_t >, CStringKernel< char >, CStringKernel< uint64_t >, CLinearStringKernel, CSparseKernel< ST >, and CWeightedDegreeRBFKernel.
Definition at line 83 of file Kernel.cpp.
|
virtual |
initialize the current kernel normalizer
Definition at line 133 of file Kernel.cpp.
|
virtual |
initialize optimization
count | count |
IDX | index |
weights | weights |
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, CWeightedDegreePositionStringKernel, CCommWordStringKernel, CCommUlongStringKernel, CLinearKernel, and CLinearStringKernel.
Definition at line 304 of file Kernel.cpp.
bool init_optimization_svm | ( | CSVM * | svm | ) |
initialize optimization
svm | svm model |
Definition at line 366 of file Kernel.cpp.
float64_t kernel | ( | int32_t | idx_a, |
int32_t | idx_b | ||
) |
void list_kernel | ( | ) |
list kernel
Definition at line 200 of file Kernel.cpp.
void load | ( | CFile * | loader | ) |
load the kernel matrix
loader | File object via which to load data |
Definition at line 145 of file Kernel.cpp.
|
protectedvirtual |
Can (optionally) be overridden to post-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_POST is called.
ShogunException | Will be thrown if an error occurres. |
Reimplemented from CSGObject.
Reimplemented in CWeightedDegreePositionStringKernel, CGaussianKernel, CANOVAKernel, CInverseMultiQuadricKernel, CCircularKernel, and CExponentialKernel.
Definition at line 384 of file Kernel.cpp.
|
protectedvirtual |
Separate the function of parameter registration This can be the first stage of a general framework for cross-validation or other parameter-based operations
Reimplemented in CSpectrumMismatchRBFKernel, CSNPStringKernel, CANOVAKernel, CRegulatoryModulesStringKernel, CGaussianMatchStringKernel, CGaussianShortRealKernel, CTensorProductPairKernel, CDistanceKernel, and CHistogramIntersectionKernel.
Definition at line 407 of file Kernel.cpp.
|
virtual |
remove lhs from kernel
Reimplemented in CWeightedDegreeStringKernel, CWeightedDegreePositionStringKernel, CCombinedKernel, CSpectrumMismatchRBFKernel, CCommUlongStringKernel, and CSpectrumRBFKernel.
Definition at line 175 of file Kernel.cpp.
|
virtual |
remove lhs and rhs from kernel
Reimplemented in CCombinedKernel.
Definition at line 160 of file Kernel.cpp.
|
virtual |
takes all necessary steps if the rhs is removed from kernel
remove rhs from kernel
Reimplemented in CCombinedKernel, and CCommUlongStringKernel.
Definition at line 187 of file Kernel.cpp.
void save | ( | CFile * | writer | ) |
save kernel matrix
writer | File object via which to save data |
Definition at line 151 of file Kernel.cpp.
|
protectedvirtual |
Can (optionally) be overridden to post-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_POST is called.
ShogunException | Will be thrown if an error occurres. |
Reimplemented from CSGObject.
Definition at line 399 of file Kernel.cpp.
|
protectedvirtual |
Can (optionally) be overridden to pre-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_PRE is called.
ShogunException | Will be thrown if an error occurres. |
Reimplemented from CSGObject.
Definition at line 391 of file Kernel.cpp.
void set_cache_size | ( | int32_t | size | ) |
void set_combined_kernel_weight | ( | float64_t | nw | ) |
|
protected |
|
virtual |
set the current kernel normalizer
Reimplemented in CWeightedDegreeStringKernel.
Definition at line 115 of file Kernel.cpp.
|
virtual |
|
protected |
set subkernel weights
weights | new subkernel weights |
Reimplemented in CCombinedKernel, CWeightedDegreeStringKernel, and CWeightedDegreePositionStringKernel.
Definition at line 357 of file Kernel.cpp.
|
protected |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |