SHOGUN  6.1.3
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)
21 {
22 }
23 
26 : CStringKernel<char>(0)
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(m_normal.vector, 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  m_normal.vector[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  SGVector<char> a_wrap(avec, alen, false);
75  SGVector<char> b_wrap(bvec, blen, false);
76  float64_t result = linalg::dot(a_wrap, b_wrap);
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  int32_t num_suppvec, int32_t *sv_idx, float64_t *alphas)
84 {
85  int32_t num_feat = ((CStringFeatures<char>*) lhs)->get_max_vector_length();
86  ASSERT(num_feat)
87 
88  m_normal = SGVector<float64_t>(num_feat);
90  clear_normal();
91 
92  for (int32_t i = 0; i<num_suppvec; i++)
93  {
94  int32_t alen;
95  bool free_avec;
96  char *avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(sv_idx[i], alen, free_avec);
97  ASSERT(avec)
98 
99  for (int32_t j = 0; j<num_feat; j++)
100  {
101  m_normal.vector[j] += alphas[i]*
102  normalizer->normalize_lhs(((float64_t) avec[j]), sv_idx[i]);
103  }
104  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, sv_idx[i], free_avec);
105  }
106  set_is_initialized(true);
107  return true;
108 }
109 
111 {
113  set_is_initialized(false);
114  return true;
115 }
116 
118 {
119  int32_t blen;
120  bool free_bvec;
121  char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
122  float64_t dot = 0.0;
123  for (auto i = 0; m_normal.vlen; ++i)
124  dot += m_normal[i]*(float64_t)bvec[i];
125  float64_t result=normalizer->normalize_rhs(dot, idx_b);
126  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
127  return result;
128 }
virtual void cleanup()
Definition: Kernel.cpp:172
SGVector< float64_t > m_normal
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)
T dot(const SGVector< T > &a, const SGVector< T > &b)
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:176
virtual bool init(CFeatures *l, CFeatures *r)
double float64_t
Definition: common.h:60
virtual bool init_normalizer()
Definition: Kernel.cpp:167
CFeatures * rhs
feature vectors to occur on right hand side
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
The class Features is the base class of all feature objects.
Definition: Features.h:69
virtual float64_t compute_optimized(int32_t idx)
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
CKernelNormalizer * normalizer
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
index_t vlen
Definition: SGVector.h:571

SHOGUN Machine Learning Toolbox - Documentation