SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
GaussianMatchStringKernel.cpp
浏览该文件的文档.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
12 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
21 : CStringKernel<char>(0), width(0.0)
22 {
25 }
26 
28 : CStringKernel<char>(size), width(w)
29 {
32 }
33 
36 : CStringKernel<char>(10), width(w)
37 {
39  init(l, r);
41 }
42 
44 {
45  cleanup();
46 }
47 
48 bool CGaussianMatchStringKernel::init(CFeatures* l, CFeatures* r)
49 {
51  return init_normalizer();
52 }
53 
55 {
57 }
58 
59 float64_t CGaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b)
60 {
61  int32_t i, alen, blen ;
62  bool free_avec, free_bvec;
63 
64  char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
65  char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
66 
67  float64_t result=0;
68 
69  ASSERT(alen==blen)
70 
71  for (i = 0; i<alen; i++)
72  result+=(avec[i]==bvec[i]) ? 0:4;
73 
74  result=exp(-result/width);
75 
76 
77  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
78  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
79  return result;
80 }
81 
83 {
84  SG_ADD(&width, "width", "kernel width", MS_AVAILABLE);
85 }
virtual void cleanup()
Definition: Kernel.cpp:173
virtual bool set_normalizer(CKernelNormalizer *normalizer)
Definition: Kernel.cpp:150
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual bool init_normalizer()
Definition: Kernel.cpp:168
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual bool init(CFeatures *l, CFeatures *r)
#define SG_ADD(...)
Definition: SGObject.h:81
friend class CSqrtDiagKernelNormalizer
Definition: Kernel.h:161
Template class StringKernel, is the base class of all String Kernels.
Definition: StringKernel.h:26

SHOGUN 机器学习工具包 - 项目文档