SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
kernel
string
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
>
13
#include <
shogun/mathematics/Math.h
>
14
#include <
shogun/kernel/string/LinearStringKernel.h
>
15
#include <
shogun/features/StringFeatures.h
>
16
17
using namespace
shogun;
18
19
CLinearStringKernel::CLinearStringKernel
()
20
:
CStringKernel
<char>(0), normal(NULL)
21
{
22
}
23
24
CLinearStringKernel::CLinearStringKernel
(
25
CStringFeatures<char>
* l,
CStringFeatures<char>
* r)
26
:
CStringKernel
<char>(0), normal(NULL)
27
{
28
init
(l, r);
29
}
30
31
CLinearStringKernel::~CLinearStringKernel
()
32
{
33
cleanup
();
34
}
35
36
bool
CLinearStringKernel::init(
CFeatures
*l,
CFeatures
*r)
37
{
38
CStringKernel<char>::init
(l, r);
39
return
init_normalizer
();
40
}
41
42
void
CLinearStringKernel::cleanup
()
43
{
44
delete_optimization
();
45
46
CKernel::cleanup
();
47
}
48
49
void
CLinearStringKernel::clear_normal
()
50
{
51
memset(
normal
, 0,
lhs
->
get_num_vectors
()*
sizeof
(
float64_t
));
52
}
53
54
void
CLinearStringKernel::add_to_normal
(int32_t idx,
float64_t
weight)
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=
SGVector<float64_t>::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
80
bool
CLinearStringKernel::init_optimization
(
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
108
bool
CLinearStringKernel::delete_optimization
()
109
{
110
SG_FREE(
normal
);
111
normal
= NULL;
112
set_is_initialized
(
false
);
113
return
true
;
114
}
115
116
float64_t
CLinearStringKernel::compute_optimized
(int32_t idx_b)
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
(
SGVector<float64_t>::dot
(
normal
, bvec, blen), idx_b);
122
((
CStringFeatures<char>
*)
rhs
)->free_feature_vector(bvec, idx_b, free_bvec);
123
return
result;
124
}
SHOGUN
Machine Learning Toolbox - Documentation