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.
Definition at line 147 of file Kernel.h.
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) |
void | get_kernel_matrix (float64_t **dst, int32_t *m, int32_t *n) |
template<class T > | |
T * | get_kernel_matrix (int32_t &m, int32_t &n, T *target) |
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 | 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 (float64_t *weights, int32_t num_weights) |
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) |
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 | |
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 |
CKernel | ( | ) |
default constructor
Definition at line 44 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 74 of file Kernel.cpp.
~CKernel | ( | ) | [virtual] |
Definition at line 91 of file Kernel.cpp.
void add_to_normal | ( | int32_t | vector_idx, | |
float64_t | weight | |||
) | [virtual] |
add vector*factor to 'virtual' normal vector
vector_idx | index | |
weight | weight |
Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedCommWordStringKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 433 of file Kernel.cpp.
void cleanup | ( | ) | [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 CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CCustomKernel, CFixedDegreeStringKernel, CGaussianMatchStringKernel, CHistogramWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CLocalAlignmentStringKernel, COligoStringKernel, CPolyKernel, CPolyMatchStringKernel, CPolyMatchWordStringKernel, CPyramidChi2, CSalzbergWordStringKernel, CSigmoidKernel, CSimpleLocalityImprovedStringKernel, CSNPStringKernel, CSparseGaussianKernel, CSparseLinearKernel, CSparsePolyKernel, CSpectrumMismatchRBFKernel, CSpectrumRBFKernel, CWeightedCommWordStringKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 184 of file Kernel.cpp.
void clear_normal | ( | ) | [virtual] |
for optimizable kernels, i.e. kernels where the weight vector can be computed explicitly (if it fits into memory)
Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 438 of file Kernel.cpp.
virtual float64_t compute | ( | int32_t | x, | |
int32_t | y | |||
) | [protected, pure 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 CAUCKernel, CChi2Kernel, CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CConstKernel, CCustomKernel, CDiagKernel, CDistanceKernel, CFixedDegreeStringKernel, CGaussianKernel, CGaussianMatchStringKernel, CGaussianShiftKernel, CGaussianShortRealKernel, CHistogramWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CLocalAlignmentStringKernel, CLocalityImprovedStringKernel, CMatchWordStringKernel, COligoStringKernel, CPolyKernel, CPolyMatchStringKernel, CPolyMatchWordStringKernel, CPyramidChi2, CRegulatoryModulesStringKernel, CSalzbergWordStringKernel, CSigmoidKernel, CSimpleLocalityImprovedStringKernel, CSNPStringKernel, CSparseGaussianKernel, CSparseLinearKernel, CSparsePolyKernel, CSpectrumMismatchRBFKernel, CSpectrumRBFKernel, CTensorProductPairKernel, CWeightedDegreePositionStringKernel, CWeightedDegreeRBFKernel, and CWeightedDegreeStringKernel.
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 | |||
) | [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, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 426 of file Kernel.cpp.
void compute_by_subkernel | ( | int32_t | vector_idx, | |
float64_t * | subkernel_contrib | |||
) | [virtual] |
compute by subkernel
vector_idx | index | |
subkernel_contrib | subkernel contribution |
Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 448 of file Kernel.cpp.
float64_t compute_optimized | ( | int32_t | vector_idx | ) | [virtual] |
compute optimized
vector_idx | index to compute |
Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedCommWordStringKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 420 of file Kernel.cpp.
int32_t compute_row_start | ( | int64_t | offs, | |
int32_t | n, | |||
bool | symmetric | |||
) | [protected] |
bool delete_optimization | ( | ) | [virtual] |
delete optimization
Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 414 of file Kernel.cpp.
int32_t get_cache_size | ( | ) |
float64_t get_combined_kernel_weight | ( | ) |
virtual EFeatureClass get_feature_class | ( | ) | [pure virtual] |
return feature class the kernel can deal with
abstract base method
Implemented in CCombinedKernel, CConstKernel, CCustomKernel, CDiagKernel, CDistanceKernel, CSimpleKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CSimpleKernel< float64_t >, CSimpleKernel< float32_t >, CSimpleKernel< int32_t >, CSimpleKernel< uint8_t >, CSimpleKernel< uint16_t >, CSparseKernel< float64_t >, CStringKernel< char >, CStringKernel< uint16_t >, and CStringKernel< uint64_t >.
virtual EFeatureType get_feature_type | ( | ) | [pure virtual] |
return feature type the kernel can deal with
abstract base method
Implemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CConstKernel, CCustomKernel, CDiagKernel, CDistanceKernel, CSimpleKernel< ST >, CSparseGaussianKernel, CSparseKernel< ST >, CSparsePolyKernel, CStringKernel< ST >, CWeightedCommWordStringKernel, CSimpleKernel< float64_t >, CSimpleKernel< float32_t >, CSimpleKernel< int32_t >, CSimpleKernel< uint8_t >, CSimpleKernel< uint16_t >, CSparseKernel< float64_t >, CStringKernel< char >, CStringKernel< uint16_t >, CStringKernel< uint64_t >, CSimpleKernel< ST >, CSimpleKernel< ST >, CSimpleKernel< ST >, CSimpleKernel< ST >, CSimpleKernel< ST >, CSimpleKernel< ST >, CSimpleKernel< ST >, CSimpleKernel< ST >, CSparseKernel< ST >, CSparseKernel< ST >, CSparseKernel< ST >, CSparseKernel< ST >, CSparseKernel< ST >, CSparseKernel< ST >, CSparseKernel< ST >, CStringKernel< ST >, CStringKernel< ST >, CStringKernel< ST >, CStringKernel< ST >, CStringKernel< ST >, CStringKernel< ST >, and CStringKernel< ST >.
bool get_is_initialized | ( | ) |
void get_kernel_matrix | ( | float64_t ** | dst, | |
int32_t * | m, | |||
int32_t * | n | |||
) |
get kernel matrix
dst | destination where matrix will be stored | |
m | dimension m of matrix | |
n | dimension n of matrix |
Definition at line 102 of file Kernel.cpp.
T* get_kernel_matrix | ( | int32_t & | m, | |
int32_t & | n, | |||
T * | target | |||
) |
static void* get_kernel_matrix_helper | ( | void * | p | ) | [static, protected] |
virtual EKernelType get_kernel_type | ( | ) | [pure virtual] |
return what type of kernel we are, e.g. Linear,Polynomial, Gaussian,...
abstract base method
Implemented in CAUCKernel, CChi2Kernel, CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CConstKernel, CCustomKernel, CDiagKernel, CDistanceKernel, CFixedDegreeStringKernel, CGaussianKernel, CGaussianMatchStringKernel, CGaussianShiftKernel, CGaussianShortRealKernel, CHistogramWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CLocalAlignmentStringKernel, CLocalityImprovedStringKernel, CMatchWordStringKernel, COligoStringKernel, CPolyKernel, CPolyMatchStringKernel, CPolyMatchWordStringKernel, CPyramidChi2, CRegulatoryModulesStringKernel, CSalzbergWordStringKernel, CSigmoidKernel, CSimpleLocalityImprovedStringKernel, CSNPStringKernel, CSparseGaussianKernel, CSparseLinearKernel, CSparsePolyKernel, CSpectrumMismatchRBFKernel, CSpectrumRBFKernel, CTensorProductPairKernel, CWeightedCommWordStringKernel, CWeightedDegreePositionStringKernel, CWeightedDegreeRBFKernel, and CWeightedDegreeStringKernel.
CFeatures* get_lhs | ( | ) |
CKernelNormalizer * get_normalizer | ( | ) | [virtual] |
obtain the current kernel normalizer
Definition at line 173 of file Kernel.cpp.
int32_t get_num_subkernels | ( | ) | [virtual] |
get number of subkernels
Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 443 of file Kernel.cpp.
virtual int32_t get_num_vec_lhs | ( | ) | [virtual] |
get number of vectors of lhs features
Reimplemented in CCustomKernel.
virtual int32_t get_num_vec_rhs | ( | ) | [virtual] |
get number of vectors of rhs features
Reimplemented in CCustomKernel.
EOptimizationType get_optimization_type | ( | ) |
CFeatures* get_rhs | ( | ) |
const float64_t * get_subkernel_weights | ( | int32_t & | num_weights | ) | [virtual] |
get subkernel weights
num_weights | number of weights will be stored here |
Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 454 of file Kernel.cpp.
virtual bool has_features | ( | ) | [virtual] |
test whether features have been assigned to lhs and rhs
Reimplemented in CCombinedKernel, and CCustomKernel.
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 CAUCKernel, CChi2Kernel, CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CConstKernel, CCustomKernel, CDistanceKernel, CFixedDegreeStringKernel, CGaussianKernel, CGaussianMatchStringKernel, CGaussianShortRealKernel, CHistogramWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CLocalAlignmentStringKernel, CLocalityImprovedStringKernel, CMatchWordStringKernel, COligoStringKernel, CPolyKernel, CPolyMatchStringKernel, CPolyMatchWordStringKernel, CPyramidChi2, CRegulatoryModulesStringKernel, CSalzbergWordStringKernel, CSigmoidKernel, CSimpleKernel< ST >, CSimpleLocalityImprovedStringKernel, CSNPStringKernel, CSparseGaussianKernel, CSparseKernel< ST >, CSparseLinearKernel, CSparsePolyKernel, CSpectrumMismatchRBFKernel, CSpectrumRBFKernel, CStringKernel< ST >, CTensorProductPairKernel, CWeightedCommWordStringKernel, CWeightedDegreePositionStringKernel, CWeightedDegreeRBFKernel, CWeightedDegreeStringKernel, CSimpleKernel< float64_t >, CSimpleKernel< float32_t >, CSimpleKernel< int32_t >, CSimpleKernel< uint8_t >, CSimpleKernel< uint16_t >, CSparseKernel< float64_t >, CStringKernel< char >, CStringKernel< uint16_t >, and CStringKernel< uint64_t >.
Definition at line 131 of file Kernel.cpp.
bool init_normalizer | ( | ) | [virtual] |
initialize the current kernel normalizer
Definition at line 179 of file Kernel.cpp.
bool init_optimization | ( | int32_t | count, | |
int32_t * | IDX, | |||
float64_t * | weights | |||
) | [virtual] |
initialize optimization
count | count | |
IDX | index | |
weights | weights |
Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 407 of file Kernel.cpp.
bool init_optimization_svm | ( | CSVM * | svm | ) |
initialize optimization
svm | svm model |
Definition at line 467 of file Kernel.cpp.
float64_t kernel | ( | int32_t | idx_a, | |
int32_t | idx_b | |||
) |
bool lhs_equals_rhs | ( | ) |
void list_kernel | ( | ) |
list kernel
Definition at line 240 of file Kernel.cpp.
void load | ( | CFile * | loader | ) |
load the kernel matrix
loader | File object via which to load data |
Definition at line 191 of file Kernel.cpp.
void remove_lhs | ( | ) | [virtual] |
remove lhs from kernel
Reimplemented in CCombinedKernel, CCommUlongStringKernel, CSpectrumMismatchRBFKernel, CSpectrumRBFKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 217 of file Kernel.cpp.
void remove_lhs_and_rhs | ( | ) | [virtual] |
remove lhs and rhs from kernel
Reimplemented in CCombinedKernel.
Definition at line 203 of file Kernel.cpp.
void remove_rhs | ( | ) | [virtual] |
takes all necessary steps if the rhs is removed from kernel
remove rhs from kernel
Reimplemented in CCombinedKernel, and CCommUlongStringKernel.
Definition at line 229 of file Kernel.cpp.
void save | ( | CFile * | writer | ) |
save kernel matrix
writer | File object via which to save data |
Definition at line 195 of file Kernel.cpp.
void set_cache_size | ( | int32_t | size | ) |
void set_combined_kernel_weight | ( | float64_t | nw | ) |
void set_is_initialized | ( | bool | p_init | ) | [protected] |
bool set_normalizer | ( | CKernelNormalizer * | normalizer | ) | [virtual] |
set the current kernel normalizer
Reimplemented in CWeightedDegreeStringKernel.
Definition at line 161 of file Kernel.cpp.
virtual void set_optimization_type | ( | EOptimizationType | t | ) | [virtual] |
void set_property | ( | EKernelProperty | p | ) | [protected] |
void set_subkernel_weights | ( | float64_t * | weights, | |
int32_t | num_weights | |||
) | [virtual] |
set subkernel weights
weights | subkernel weights | |
num_weights | number of weights |
Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.
Definition at line 460 of file Kernel.cpp.
void unset_property | ( | EKernelProperty | p | ) | [protected] |
friend class CAvgDiagKernelNormalizer [friend] |
Reimplemented in CCommWordStringKernel.
friend class CDiceKernelNormalizer [friend] |
Reimplemented in CCommWordStringKernel.
friend class CFirstElementKernelNormalizer [friend] |
Reimplemented in CCommWordStringKernel.
friend class CMultitaskKernelMaskNormalizer [friend] |
friend class CMultitaskKernelMaskPairNormalizer [friend] |
friend class CMultitaskKernelMklNormalizer [friend] |
friend class CMultitaskKernelNormalizer [friend] |
friend class CRidgeKernelNormalizer [friend] |
Reimplemented in CCommWordStringKernel.
friend class CSqrtDiagKernelNormalizer [friend] |
Reimplemented in CCommWordStringKernel.
friend class CTanimotoKernelNormalizer [friend] |
Reimplemented in CCommWordStringKernel.
friend class CVarianceKernelNormalizer [friend] |
Reimplemented in CCommWordStringKernel.
int32_t cache_size [protected] |
float64_t combined_kernel_weight [protected] |
KERNELCACHE_ELEM* kernel_matrix [protected] |
this *COULD* store the whole kernel matrix usually not applicable / necessary to compute the whole matrix
Reimplemented in CSpectrumMismatchRBFKernel, and CSpectrumRBFKernel.
CKernelNormalizer* normalizer [protected] |
int32_t num_lhs [protected] |
int32_t num_rhs [protected] |
EOptimizationType opt_type [protected] |
bool optimization_initialized [protected] |
uint64_t properties [protected] |