CKernel Class Reference

Inheritance diagram for CKernel:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 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)
virtual float64_tget_kernel_matrix_real (int32_t &m, int32_t &n, float64_t *target)
virtual float32_tget_kernel_matrix_shortreal (int32_t &m, int32_t &n, float32_t *target)
virtual bool init (CFeatures *lhs, CFeatures *rhs)
virtual bool set_normalizer (CKernelNormalizer *normalizer)
virtual CKernelNormalizerget_normalizer ()
virtual bool init_normalizer ()
virtual void cleanup ()
bool load (char *fname)
bool save (char *fname)
virtual bool load_init (FILE *src)=0
virtual bool save_init (FILE *dest)=0
CFeaturesget_lhs ()
CFeaturesget_rhs ()
int32_t get_num_vec_lhs ()
int32_t get_num_vec_rhs ()
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
virtual const char * get_name ()=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_tget_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

Protected Attributes

int32_t cache_size
 cache_size in MB
KERNELCACHE_ELEMkernel_matrix
CFeatureslhs
 feature vectors to occur on left hand side
CFeaturesrhs
 feature vectors to occur on right hand side
float64_t combined_kernel_weight
bool optimization_initialized
EOptimizationType opt_type
uint64_t properties
CKernelNormalizernormalizer

Friends

class CSqrtDiagKernelNormalizer
class CAvgDiagKernelNormalizer
class CFirstElementKernelNormalizer


Detailed Description

The Kernel base class. Non-mathematically spoken, a kernel is a function that given two input objects ${\bf x}$ and ${\bf x'}$ 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

\[ k({\bf x},{\bf x'})= \Phi_k({\bf x})\cdot \Phi_k({\bf x'}) \]

where $\Phi$ 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 97 of file Kernel.h.


Constructor & Destructor Documentation

CKernel::CKernel ( int32_t  size  ) 

constructor

Parameters:
size cache size

Definition at line 34 of file Kernel.cpp.

CKernel::CKernel ( CFeatures l,
CFeatures r,
int32_t  size 
)

constructor

Parameters:
l features for left-hand side
r features for right-hand side
size cache size

Definition at line 52 of file Kernel.cpp.

CKernel::~CKernel (  )  [virtual]

Definition at line 69 of file Kernel.cpp.


Member Function Documentation

void CKernel::add_to_normal ( int32_t  vector_idx,
float64_t  weight 
) [virtual]

void CKernel::cleanup (  )  [virtual]

void CKernel::clear_normal (  )  [virtual]

for optimizable kernels, i.e. kernels where the weight vector can be computed explicitely (if it fits into memory)

Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.

Definition at line 602 of file Kernel.cpp.

virtual float64_t CKernel::compute ( int32_t  x,
int32_t  y 
) [protected, pure virtual]

void CKernel::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 590 of file Kernel.cpp.

void CKernel::compute_by_subkernel ( int32_t  vector_idx,
float64_t subkernel_contrib 
) [virtual]

compute by subkernel

Parameters:
vector_idx index
subkernel_contrib subkernel contribution

Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.

Definition at line 612 of file Kernel.cpp.

float64_t CKernel::compute_optimized ( int32_t  vector_idx  )  [virtual]

compute optimized

Parameters:
vector_idx index to compute
Returns:
optimized value at given index

Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedCommWordStringKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.

Definition at line 584 of file Kernel.cpp.

bool CKernel::delete_optimization (  )  [virtual]

int32_t CKernel::get_cache_size (  ) 

return the size of the kernel cache

Returns:
size of kernel cache

Definition at line 354 of file Kernel.h.

float64_t CKernel::get_combined_kernel_weight (  ) 

get combined kernel weight

Returns:
combined kernel weight

Definition at line 445 of file Kernel.h.

virtual EFeatureClass CKernel::get_feature_class (  )  [pure virtual]

virtual EFeatureType CKernel::get_feature_type (  )  [pure virtual]

bool CKernel::get_is_initialized (  ) 

check if optimization is initialized

Returns:
if optimization is initialized

Definition at line 396 of file Kernel.h.

void CKernel::get_kernel_matrix ( float64_t **  dst,
int32_t *  m,
int32_t *  n 
)

get kernel matrix

Parameters:
dst destination where matrix will be stored
m dimension m of matrix
n dimension n of matrix

Definition at line 79 of file Kernel.cpp.

float64_t * CKernel::get_kernel_matrix_real ( int32_t &  m,
int32_t &  n,
float64_t target 
) [virtual]

get kernel matrix real

Parameters:
m dimension m of matrix
n dimension n of matrix
target the kernel matrix
Returns:
the kernel matrix

Definition at line 216 of file Kernel.cpp.

float32_t * CKernel::get_kernel_matrix_shortreal ( int32_t &  m,
int32_t &  n,
float32_t target 
) [virtual]

get kernel matrix shortreal

Parameters:
m dimension m of matrix
n dimension n of matrix
target target for kernel matrix
Returns:
the kernel matrix

Reimplemented in CCustomKernel.

Definition at line 146 of file Kernel.cpp.

virtual EKernelType CKernel::get_kernel_type (  )  [pure virtual]

CFeatures* CKernel::get_lhs (  ) 

get left-hand side of features used in kernel

Returns:
features of left-hand side

Definition at line 250 of file Kernel.h.

virtual const char* CKernel::get_name (  )  [pure virtual]

CKernelNormalizer * CKernel::get_normalizer (  )  [virtual]

obtain the current kernel normalizer

Returns:
the kernel normalizer

Definition at line 325 of file Kernel.cpp.

int32_t CKernel::get_num_subkernels (  )  [virtual]

get number of subkernels

Returns:
number of subkernels

Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.

Definition at line 607 of file Kernel.cpp.

int32_t CKernel::get_num_vec_lhs (  ) 

get number of vectors of lhs features

Returns:
number of vectors of left-hand side

Definition at line 262 of file Kernel.h.

int32_t CKernel::get_num_vec_rhs (  ) 

get number of vectors of rhs features

Returns:
number of vectors of right-hand side

Definition at line 274 of file Kernel.h.

EOptimizationType CKernel::get_optimization_type (  ) 

get optimization type

Returns:
optimization type

Definition at line 384 of file Kernel.h.

CFeatures* CKernel::get_rhs (  ) 

get right-hand side of features used in kernel

Returns:
features of right-hand side

Definition at line 256 of file Kernel.h.

const float64_t * CKernel::get_subkernel_weights ( int32_t &  num_weights  )  [virtual]

get subkernel weights

Parameters:
num_weights number of weights will be stored here
Returns:
subkernel weights

Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.

Definition at line 618 of file Kernel.cpp.

bool CKernel::has_features (  ) 

test whether features have been assigned to lhs and rhs

Returns:
true if features are assigned

Definition at line 286 of file Kernel.h.

bool CKernel::has_property ( EKernelProperty  p  ) 

check if kernel has given property

Parameters:
p kernel property
Returns:
if kernel has given property

Definition at line 366 of file Kernel.h.

bool CKernel::init ( CFeatures lhs,
CFeatures rhs 
) [virtual]

bool CKernel::init_normalizer (  )  [virtual]

initialize the current kernel normalizer

Returns:
if init was successful

Definition at line 331 of file Kernel.cpp.

bool CKernel::init_optimization ( int32_t  count,
int32_t *  IDX,
float64_t weights 
) [virtual]

initialize optimization

Parameters:
count count
IDX index
weights weights
Returns:
if initializing was successful

Reimplemented in CCombinedKernel, CCommUlongStringKernel, CCommWordStringKernel, CLinearByteKernel, CLinearKernel, CLinearStringKernel, CLinearWordKernel, CSparseLinearKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.

Definition at line 571 of file Kernel.cpp.

bool CKernel::init_optimization_svm ( CSVM svm  ) 

initialize optimization

Parameters:
svm svm model
Returns:
if initializing was successful

Definition at line 631 of file Kernel.cpp.

float64_t CKernel::kernel ( int32_t  idx_a,
int32_t  idx_b 
)

get kernel function for lhs feature vector a and rhs feature vector b

Parameters:
idx_a index of feature vector a
idx_b index of feature vector b
Returns:
computed kernel function

Definition at line 127 of file Kernel.h.

bool CKernel::lhs_equals_rhs (  ) 

test whether features on lhs and rhs are the same

Returns:
true if features are the same

Definition at line 295 of file Kernel.h.

void CKernel::list_kernel (  ) 

list kernel

Definition at line 410 of file Kernel.cpp.

bool CKernel::load ( char *  fname  ) 

load the kernel matrix

Parameters:
fname filename to load from
Returns:
if loading was succesful

Definition at line 343 of file Kernel.cpp.

virtual bool CKernel::load_init ( FILE *  src  )  [pure virtual]

void CKernel::remove_lhs (  )  [virtual]

void CKernel::remove_lhs_and_rhs (  )  [virtual]

remove lhs and rhs from kernel

Definition at line 379 of file Kernel.cpp.

void CKernel::remove_rhs (  )  [virtual]

takes all necessary steps if the rhs is removed from kernel

remove rhs from kernel

Reimplemented in CCombinedKernel, CCommUlongStringKernel, and CCommWordStringKernel.

Definition at line 400 of file Kernel.cpp.

bool CKernel::save ( char *  fname  ) 

save kernel matrix

Parameters:
fname filename to save to
Returns:
if saving was successful

Definition at line 348 of file Kernel.cpp.

virtual bool CKernel::save_init ( FILE *  dest  )  [pure virtual]

void CKernel::set_cache_size ( int32_t  size  ) 

set the size of the kernel cache

Parameters:
size of kernel cache

Definition at line 344 of file Kernel.h.

void CKernel::set_combined_kernel_weight ( float64_t  nw  ) 

set combined kernel weight

Parameters:
nw new combined kernel weight

Definition at line 451 of file Kernel.h.

void CKernel::set_is_initialized ( bool  p_init  )  [protected]

set is initialized

Parameters:
p_init if optimization shall be set to initialized

Definition at line 505 of file Kernel.h.

bool CKernel::set_normalizer ( CKernelNormalizer normalizer  )  [virtual]

set the current kernel normalizer

Returns:
if succesful

Definition at line 313 of file Kernel.cpp.

virtual void CKernel::set_optimization_type ( EOptimizationType  t  )  [virtual]

set optimization type

Parameters:
t optimization type to set

Reimplemented in CCombinedKernel.

Definition at line 390 of file Kernel.h.

void CKernel::set_property ( EKernelProperty  p  )  [protected]

set property

Parameters:
p kernel property to set

Definition at line 487 of file Kernel.h.

void CKernel::set_subkernel_weights ( float64_t weights,
int32_t  num_weights 
) [virtual]

set subkernel weights

Parameters:
weights subkernel weights
num_weights number of weights

Reimplemented in CCombinedKernel, CWeightedDegreePositionStringKernel, and CWeightedDegreeStringKernel.

Definition at line 624 of file Kernel.cpp.

void CKernel::unset_property ( EKernelProperty  p  )  [protected]

unset property

Parameters:
p kernel property to unset

Definition at line 496 of file Kernel.h.


Friends And Related Function Documentation

friend class CAvgDiagKernelNormalizer [friend]

Definition at line 100 of file Kernel.h.

friend class CFirstElementKernelNormalizer [friend]

Definition at line 101 of file Kernel.h.

friend class CSqrtDiagKernelNormalizer [friend]

Reimplemented in CCommWordStringKernel.

Definition at line 99 of file Kernel.h.


Member Data Documentation

int32_t CKernel::cache_size [protected]

cache_size in MB

Definition at line 525 of file Kernel.h.

combined kernel weight

Definition at line 539 of file Kernel.h.

this *COULD* store the whole kernel matrix usually not applicable / necessary to compute the whole matrix

Definition at line 531 of file Kernel.h.

CFeatures* CKernel::lhs [protected]

feature vectors to occur on left hand side

Definition at line 534 of file Kernel.h.

normalize the kernel(i,j) function based on this normalization function

Definition at line 553 of file Kernel.h.

optimization type (currently FASTBUTMEMHUNGRY and SLOWBUTMEMEFFICIENT)

Definition at line 546 of file Kernel.h.

if optimization is initialized

Definition at line 542 of file Kernel.h.

uint64_t CKernel::properties [protected]

kernel properties

Definition at line 549 of file Kernel.h.

CFeatures* CKernel::rhs [protected]

feature vectors to occur on right hand side

Definition at line 536 of file Kernel.h.


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

SHOGUN Machine Learning Toolbox - Documentation