SHOGUN
v1.1.0
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
src
shogun
classifier
svm
WDSVMOcas.h
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) 2007-2008 Vojtech Franc
8
* Written (W) 2007-2009 Soeren Sonnenburg
9
* Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10
*/
11
12
#ifndef _WDSVMOCAS_H___
13
#define _WDSVMOCAS_H___
14
15
#include <
shogun/lib/common.h
>
16
#include <
shogun/machine/Machine.h
>
17
#include <
shogun/classifier/svm/SVMOcas.h
>
18
#include <
shogun/features/StringFeatures.h
>
19
#include <
shogun/features/Labels.h
>
20
21
namespace
shogun
22
{
23
template
<
class
ST>
class
CStringFeatures;
24
26
class
CWDSVMOcas
:
public
CMachine
27
{
28
public
:
30
CWDSVMOcas
();
31
36
CWDSVMOcas
(E_SVM_TYPE type);
37
46
CWDSVMOcas
(
47
float64_t
C, int32_t d, int32_t from_d,
48
CStringFeatures<uint8_t>
* traindat,
CLabels
* trainlab);
49
virtual
~CWDSVMOcas
();
50
55
virtual
inline
EClassifierType
get_classifier_type
() {
return
CT_WDSVMOCAS
; }
56
63
inline
void
set_C
(
float64_t
c_neg,
float64_t
c_pos) {
C1
=c_neg;
C2
=c_pos; }
64
69
inline
float64_t
get_C1
() {
return
C1
; }
70
75
inline
float64_t
get_C2
() {
return
C2
; }
76
81
inline
void
set_epsilon
(
float64_t
eps) {
epsilon
=eps; }
82
87
inline
float64_t
get_epsilon
() {
return
epsilon
; }
88
93
inline
void
set_features
(
CStringFeatures<uint8_t>
* feat)
94
{
95
SG_UNREF
(
features
);
96
SG_REF
(feat);
97
features
=feat;
98
}
99
104
inline
CStringFeatures<uint8_t>
*
get_features
()
105
{
106
SG_REF
(
features
);
107
return
features
;
108
}
109
114
inline
void
set_bias_enabled
(
bool
enable_bias) {
use_bias
=enable_bias; }
115
120
inline
bool
get_bias_enabled
() {
return
use_bias
; }
121
126
inline
void
set_bufsize
(int32_t sz) {
bufsize
=sz; }
127
132
inline
int32_t
get_bufsize
() {
return
bufsize
; }
133
139
inline
void
set_degree
(int32_t d, int32_t from_d)
140
{
141
degree
=d;
142
from_degree
=from_d;
143
}
144
149
inline
int32_t
get_degree
() {
return
degree
; }
150
155
CLabels
*
apply
();
156
162
virtual
CLabels
*
apply
(
CFeatures
* data);
163
169
inline
virtual
float64_t
apply
(int32_t num)
170
{
171
ASSERT
(
features
);
172
if
(!
wd_weights
)
173
set_wd_weights
();
174
175
int32_t len=0;
176
float64_t
sum=0;
177
bool
free_vec;
178
uint8_t* vec=
features
->
get_feature_vector
(num, len, free_vec);
179
//SG_INFO("len %d, string_length %d\n", len, string_length);
180
ASSERT
(len==
string_length
);
181
182
for
(int32_t j=0; j<
string_length
; j++)
183
{
184
int32_t offs=
w_dim_single_char
*j;
185
int32_t val=0;
186
for
(int32_t k=0; (j+k<
string_length
) && (k<
degree
); k++)
187
{
188
val=val*
alphabet_size
+ vec[j+k];
189
sum+=
wd_weights
[k] *
w
[offs+val];
190
offs+=
w_offsets
[k];
191
}
192
}
193
features
->
free_feature_vector
(vec, num, free_vec);
194
return
sum/
normalization_const
;
195
}
196
198
inline
void
set_normalization_const
()
199
{
200
ASSERT
(
features
);
201
normalization_const
=0;
202
for
(int32_t i=0; i<
degree
; i++)
203
normalization_const
+=(
string_length
-i)*
wd_weights
[i]*
wd_weights
[i];
204
205
normalization_const
=
CMath::sqrt
(
normalization_const
);
206
SG_DEBUG
(
"normalization_const:%f\n"
,
normalization_const
);
207
}
208
213
inline
float64_t
get_normalization_const
() {
return
normalization_const
; }
214
215
216
protected
:
221
int32_t
set_wd_weights
();
222
231
static
void
compute_W
(
232
float64_t
*sq_norm_W,
float64_t
*dp_WoldW,
float64_t
*alpha,
233
uint32_t nSel,
void
* ptr );
234
241
static
float64_t
update_W
(
float64_t
t,
void
* ptr );
242
248
static
void
*
add_new_cut_helper
(
void
* ptr);
249
258
static
int
add_new_cut
(
259
float64_t
*new_col_H, uint32_t *new_cut, uint32_t cut_length,
260
uint32_t nSel,
void
* ptr );
261
267
static
void
*
compute_output_helper
(
void
* ptr);
268
274
static
int
compute_output
(
float64_t
*output,
void
* ptr );
275
282
static
int
sort
(
float64_t
* vals,
float64_t
* data, uint32_t size);
283
285
static
inline
void
print
(ocas_return_value_T value)
286
{
287
return
;
288
}
289
290
292
inline
virtual
const
char
*
get_name
()
const
{
return
"WDSVMOcas"
; }
293
294
protected
:
303
virtual
bool
train_machine
(
CFeatures
* data=NULL);
304
305
protected
:
307
CStringFeatures<uint8_t>
*
features
;
309
bool
use_bias
;
311
int32_t
bufsize
;
313
float64_t
C1
;
315
float64_t
C2
;
317
float64_t
epsilon
;
319
E_SVM_TYPE
method
;
320
322
int32_t
degree
;
324
int32_t
from_degree
;
326
float32_t
*
wd_weights
;
328
int32_t
num_vec
;
330
int32_t
string_length
;
332
int32_t
alphabet_size
;
333
335
float64_t
normalization_const
;
336
338
float64_t
bias
;
340
float64_t
old_bias
;
342
int32_t*
w_offsets
;
344
int32_t
w_dim
;
346
int32_t
w_dim_single_char
;
348
float32_t
*
w
;
350
float32_t
*
old_w
;
352
float64_t
*
lab
;
353
355
float32_t
**
cuts
;
357
float64_t
*
cp_bias
;
358
};
359
}
360
#endif
SHOGUN
Machine Learning Toolbox - Documentation