SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LinearStringKernel.cpp
Go to the documentation of this file.
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>
16 
17 using namespace shogun;
18 
20 : CStringKernel<char>(0), normal(NULL)
21 {
22 }
23 
26 : CStringKernel<char>(0), normal(NULL)
27 {
28  init(l, r);
29 }
30 
32 {
33  cleanup();
34 }
35 
36 bool CLinearStringKernel::init(CFeatures *l, CFeatures *r)
37 {
39  return init_normalizer();
40 }
41 
43 {
45 
47 }
48 
50 {
51  memset(normal, 0, lhs->get_num_vectors()*sizeof(float64_t));
52 }
53 
55 {
56  int32_t vlen;
57  bool vfree;
58  char* vec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx, vlen, vfree);
59 
60  for (int32_t i=0; i<vlen; i++)
61  normal[i] += weight*normalizer->normalize_lhs(vec[i], idx);
62 
63  ((CStringFeatures<char>*) lhs)->free_feature_vector(vec, idx, vfree);
64 }
65 
66 float64_t CLinearStringKernel::compute(int32_t idx_a, int32_t idx_b)
67 {
68  int32_t alen, blen;
69  bool free_avec, free_bvec;
70 
71  char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
72  char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
73  ASSERT(alen==blen)
74  float64_t result=CMath::dot(avec, bvec, alen);
75  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
76  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
77  return result;
78 }
79 
81  int32_t num_suppvec, int32_t *sv_idx, float64_t *alphas)
82 {
83  int32_t num_feat = ((CStringFeatures<char>*) lhs)->get_max_vector_length();
84  ASSERT(num_feat)
85 
86  normal = SG_MALLOC(float64_t, num_feat);
87  ASSERT(normal)
88  clear_normal();
89 
90  for (int32_t i = 0; i<num_suppvec; i++)
91  {
92  int32_t alen;
93  bool free_avec;
94  char *avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(sv_idx[i], alen, free_avec);
95  ASSERT(avec)
96 
97  for (int32_t j = 0; j<num_feat; j++)
98  {
99  normal[j] += alphas[i]*
100  normalizer->normalize_lhs(((float64_t) avec[j]), sv_idx[i]);
101  }
102  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, sv_idx[i], free_avec);
103  }
104  set_is_initialized(true);
105  return true;
106 }
107 
109 {
110  SG_FREE(normal);
111  normal = NULL;
112  set_is_initialized(false);
113  return true;
114 }
115 
117 {
118  int32_t blen;
119  bool free_bvec;
120  char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
121  float64_t result=normalizer->normalize_rhs(CMath::dot(normal, bvec, blen), idx_b);
122  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
123  return result;
124 }
virtual void cleanup()
Definition: Kernel.cpp:162
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)=0
virtual int32_t get_num_vectors() const =0
void set_is_initialized(bool p_init)
Definition: Kernel.h:899
virtual bool init_optimization(int32_t num_suppvec, int32_t *sv_idx, float64_t *alphas)
virtual void add_to_normal(int32_t idx, float64_t weight)
#define ASSERT(x)
Definition: SGIO.h:201
virtual bool init(CFeatures *l, CFeatures *r)
double float64_t
Definition: common.h:50
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
Definition: Math.h:627
virtual bool init_normalizer()
Definition: Kernel.cpp:157
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
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 float64_t compute_optimized(int32_t idx)
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
CKernelNormalizer * normalizer
Definition: Kernel.h:1086
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)=0
Template class StringKernel, is the base class of all String Kernels.
Definition: StringKernel.h:26

SHOGUN Machine Learning Toolbox - Documentation