SHOGUN
v2.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
classifier
PluginEstimate.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/features/StringFeatures.h
>
14
#include <
shogun/labels/Labels.h
>
15
#include <
shogun/labels/BinaryLabels.h
>
16
#include <
shogun/labels/RegressionLabels.h
>
17
#include <
shogun/distributions/LinearHMM.h
>
18
#include <
shogun/classifier/PluginEstimate.h
>
19
20
using namespace
shogun;
21
22
CPluginEstimate::CPluginEstimate
(
float64_t
pos_pseudo,
float64_t
neg_pseudo)
23
:
CMachine
(), m_pos_pseudo(1e-10), m_neg_pseudo(1e-10),
24
pos_model(NULL), neg_model(NULL), features(NULL)
25
{
26
m_parameters
->
add
(&
m_pos_pseudo
,
27
"pos_pseudo"
,
"pseudo count for positive class"
);
28
m_parameters
->
add
(&
m_neg_pseudo
,
29
"neg_pseudo"
,
"pseudo count for negative class"
);
30
31
m_parameters
->
add
((
CSGObject
**) &
pos_model
,
32
"pos_model"
,
"LinearHMM modelling positive class."
);
33
m_parameters
->
add
((
CSGObject
**) &
neg_model
,
34
"neg_model"
,
"LinearHMM modelling negative class."
);
35
36
m_parameters
->
add
((
CSGObject
**) &
features
,
37
"features"
,
"String Features."
);
38
}
39
40
CPluginEstimate::~CPluginEstimate
()
41
{
42
SG_UNREF
(
pos_model
);
43
SG_UNREF
(
neg_model
);
44
45
SG_UNREF
(
features
);
46
}
47
48
bool
CPluginEstimate::train_machine
(
CFeatures
* data)
49
{
50
ASSERT
(
m_labels
);
51
ASSERT
(
m_labels
->
get_label_type
() ==
LT_BINARY
);
52
if
(data)
53
{
54
if
(data->
get_feature_class
() !=
C_STRING
||
55
data->
get_feature_type
() !=
F_WORD
)
56
{
57
SG_ERROR
(
"Features not of class string type word\n"
);
58
}
59
60
set_features
((
CStringFeatures<uint16_t>
*) data);
61
}
62
ASSERT
(
features
);
63
64
SG_UNREF
(
pos_model
);
65
SG_UNREF
(
neg_model
);
66
67
pos_model
=
new
CLinearHMM
(
features
);
68
neg_model
=
new
CLinearHMM
(
features
);
69
70
SG_REF
(
pos_model
);
71
SG_REF
(
neg_model
);
72
73
int32_t* pos_indizes=
SG_MALLOC
(int32_t, ((
CStringFeatures<uint16_t>
*)
features
)->get_num_vectors());
74
int32_t* neg_indizes=
SG_MALLOC
(int32_t, ((
CStringFeatures<uint16_t>
*)
features
)->get_num_vectors());
75
76
ASSERT
(
m_labels
->
get_num_labels
()==
features
->
get_num_vectors
());
77
78
int32_t pos_idx=0;
79
int32_t neg_idx=0;
80
81
for
(int32_t i=0; i<
m_labels
->
get_num_labels
(); i++)
82
{
83
if
(((
CBinaryLabels
*)
m_labels
)->get_label(i) > 0)
84
pos_indizes[pos_idx++]=i;
85
else
86
neg_indizes[neg_idx++]=i;
87
}
88
89
SG_INFO
(
"training using pseudos %f and %f\n"
,
m_pos_pseudo
,
m_neg_pseudo
);
90
pos_model
->
train
(pos_indizes, pos_idx,
m_pos_pseudo
);
91
neg_model
->
train
(neg_indizes, neg_idx,
m_neg_pseudo
);
92
93
SG_FREE
(pos_indizes);
94
SG_FREE
(neg_indizes);
95
96
return
true
;
97
}
98
99
CBinaryLabels
*
CPluginEstimate::apply_binary
(
CFeatures
* data)
100
{
101
if
(data)
102
{
103
if
(data->
get_feature_class
() !=
C_STRING
||
104
data->
get_feature_type
() !=
F_WORD
)
105
{
106
SG_ERROR
(
"Features not of class string type word\n"
);
107
}
108
109
set_features
((
CStringFeatures<uint16_t>
*) data);
110
}
111
112
ASSERT
(
features
);
113
SGVector<float64_t>
result(
features
->
get_num_vectors
());
114
115
for
(int32_t vec=0; vec<
features
->
get_num_vectors
(); vec++)
116
result[vec] =
apply_one
(vec);
117
118
return
new
CBinaryLabels
(result);
119
}
120
121
float64_t
CPluginEstimate::apply_one
(int32_t vec_idx)
122
{
123
ASSERT
(
features
);
124
125
int32_t len;
126
bool
free_vec;
127
uint16_t* vector=
features
->
get_feature_vector
(vec_idx, len, free_vec);
128
129
if
((!
pos_model
) || (!
neg_model
))
130
SG_ERROR
(
"model(s) not assigned\n"
);
131
132
float64_t
result=
pos_model
->
get_log_likelihood_example
(vector, len) -
neg_model
->
get_log_likelihood_example
(vector, len);
133
features
->
free_feature_vector
(vector, vec_idx, free_vec);
134
return
result;
135
}
SHOGUN
Machine Learning Toolbox - Documentation