SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
classifier
svm
WDSVMOcas.h
浏览该文件的文档.
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/labels/Labels.h
>
20
21
namespace
shogun
22
{
23
template
<
class
ST>
class
CStringFeatures
;
24
26
class
CWDSVMOcas
:
public
CMachine
27
{
28
public
:
30
MACHINE_PROBLEM_TYPE
(
PT_BINARY
);
31
33
CWDSVMOcas
();
34
39
CWDSVMOcas
(E_SVM_TYPE type);
40
49
CWDSVMOcas
(
50
float64_t
C, int32_t d, int32_t from_d,
51
CStringFeatures<uint8_t>
* traindat,
CLabels
* trainlab);
52
virtual
~CWDSVMOcas
();
53
58
virtual
EMachineType
get_classifier_type
() {
return
CT_WDSVMOCAS
; }
59
66
inline
void
set_C
(
float64_t
c_neg,
float64_t
c_pos) {
C1
=c_neg;
C2
=c_pos; }
67
72
inline
float64_t
get_C1
() {
return
C1
; }
73
78
inline
float64_t
get_C2
() {
return
C2
; }
79
84
inline
void
set_epsilon
(
float64_t
eps) {
epsilon
=eps; }
85
90
inline
float64_t
get_epsilon
() {
return
epsilon
; }
91
96
inline
void
set_features
(
CStringFeatures<uint8_t>
* feat)
97
{
98
SG_REF
(feat);
99
SG_UNREF
(
features
);
100
features
=feat;
101
}
102
107
inline
CStringFeatures<uint8_t>
*
get_features
()
108
{
109
SG_REF
(
features
);
110
return
features
;
111
}
112
117
inline
void
set_bias_enabled
(
bool
enable_bias) {
use_bias
=enable_bias; }
118
123
inline
bool
get_bias_enabled
() {
return
use_bias
; }
124
129
inline
void
set_bufsize
(int32_t sz) {
bufsize
=sz; }
130
135
inline
int32_t
get_bufsize
() {
return
bufsize
; }
136
142
inline
void
set_degree
(int32_t d, int32_t from_d)
143
{
144
degree
=d;
145
from_degree
=from_d;
146
}
147
152
inline
int32_t
get_degree
() {
return
degree
; }
153
160
virtual
CBinaryLabels
*
apply_binary
(
CFeatures
* data=NULL);
161
168
virtual
CRegressionLabels
*
apply_regression
(
CFeatures
* data=NULL);
169
175
virtual
float64_t
apply_one
(int32_t num)
176
{
177
ASSERT
(
features
)
178
if
(!
wd_weights
)
179
set_wd_weights
();
180
181
int32_t len=0;
182
float64_t
sum=0;
183
bool
free_vec;
184
uint8_t* vec=
features
->
get_feature_vector
(num, len, free_vec);
185
//SG_INFO("len %d, string_length %d\n", len, string_length)
186
ASSERT
(len==
string_length
)
187
188
for
(int32_t j=0; j<
string_length
; j++)
189
{
190
int32_t offs=
w_dim_single_char
*j;
191
int32_t val=0;
192
for
(int32_t k=0; (j+k<
string_length
) && (k<
degree
); k++)
193
{
194
val=val*
alphabet_size
+ vec[j+k];
195
sum+=
wd_weights
[k] *
w
[offs+val];
196
offs+=
w_offsets
[k];
197
}
198
}
199
features
->
free_feature_vector
(vec, num, free_vec);
200
return
sum/
normalization_const
;
201
}
202
204
inline
void
set_normalization_const
()
205
{
206
ASSERT
(
features
)
207
normalization_const
=0;
208
for
(int32_t i=0; i<
degree
; i++)
209
normalization_const
+=(
string_length
-i)*
wd_weights
[i]*
wd_weights
[i];
210
211
normalization_const
=
CMath::sqrt
(
normalization_const
);
212
SG_DEBUG
(
"normalization_const:%f\n"
,
normalization_const
)
213
}
214
219
inline
float64_t
get_normalization_const
() {
return
normalization_const
; }
220
221
222
protected
:
223
228
SGVector<float64_t>
apply_get_outputs
(
CFeatures
* data);
229
234
int32_t
set_wd_weights
();
235
244
static
void
compute_W
(
245
float64_t
*sq_norm_W,
float64_t
*dp_WoldW,
float64_t
*alpha,
246
uint32_t nSel,
void
* ptr );
247
254
static
float64_t
update_W
(
float64_t
t,
void
* ptr );
255
261
static
void
*
add_new_cut_helper
(
void
* ptr);
262
271
static
int
add_new_cut
(
272
float64_t
*new_col_H, uint32_t *new_cut, uint32_t cut_length,
273
uint32_t nSel,
void
* ptr );
274
280
static
void
*
compute_output_helper
(
void
* ptr);
281
287
static
int
compute_output
(
float64_t
*output,
void
* ptr );
288
295
static
int
sort
(
float64_t
* vals,
float64_t
* data, uint32_t size);
296
298
static
inline
void
print
(ocas_return_value_T value)
299
{
300
return
;
301
}
302
303
305
virtual
const
char
*
get_name
()
const
{
return
"WDSVMOcas"
; }
306
307
protected
:
316
virtual
bool
train_machine
(
CFeatures
* data=NULL);
317
318
protected
:
320
CStringFeatures<uint8_t>
*
features
;
322
bool
use_bias
;
324
int32_t
bufsize
;
326
float64_t
C1
;
328
float64_t
C2
;
330
float64_t
epsilon
;
332
E_SVM_TYPE
method
;
333
335
int32_t
degree
;
337
int32_t
from_degree
;
339
float32_t
*
wd_weights
;
341
int32_t
num_vec
;
343
int32_t
string_length
;
345
int32_t
alphabet_size
;
346
348
float64_t
normalization_const
;
349
351
float64_t
bias
;
353
float64_t
old_bias
;
355
int32_t*
w_offsets
;
357
int32_t
w_dim
;
359
int32_t
w_dim_single_char
;
361
float32_t
*
w
;
363
float32_t
*
old_w
;
365
float64_t
*
lab
;
366
368
float32_t
**
cuts
;
370
float64_t
*
cp_bias
;
371
};
372
}
373
#endif
SHOGUN
机器学习工具包 - 项目文档