-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FKFeatures.cpp
260 lines (213 loc) · 6.3 KB
/
FKFeatures.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Evan Shelhamer, Björn Esser, Sergey Lisitsyn
*/
#include <shogun/features/FKFeatures.h>
#include <shogun/features/StringFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/base/Parameter.h>
using namespace shogun;
CFKFeatures::CFKFeatures() : CDenseFeatures<float64_t>()
{
init();
}
CFKFeatures::CFKFeatures(int32_t size, CHMM* p, CHMM* n)
: CDenseFeatures<float64_t>(size)
{
init();
weight_a=-1;
set_models(p,n);
}
CFKFeatures::CFKFeatures(const CFKFeatures &orig)
: CDenseFeatures<float64_t>(orig), pos(orig.pos), neg(orig.neg), weight_a(orig.weight_a)
{
}
CFKFeatures::~CFKFeatures()
{
SG_UNREF(pos);
SG_UNREF(neg);
}
float64_t CFKFeatures::deriv_a(float64_t a, int32_t dimension)
{
CStringFeatures<uint16_t> *Obs=pos->get_observations() ;
float64_t deriv=0.0 ;
int32_t i=dimension ;
if (dimension==-1)
{
for (i=0; i<Obs->get_num_vectors(); i++)
{
//float64_t pp=pos->model_probability(i) ;
//float64_t pn=neg->model_probability(i) ;
float64_t pp=(pos_prob) ? pos_prob[i] : pos->model_probability(i);
float64_t pn=(neg_prob) ? neg_prob[i] : neg->model_probability(i);
float64_t sub=pp ;
if (pn>pp) sub=pn ;
pp-=sub ;
pn-=sub ;
pp=exp(pp) ;
pn=exp(pn) ;
float64_t p=a*pp+(1-a)*pn ;
deriv+=(pp-pn)/p ;
/*float64_t d1=(pp-pn)/p ;
pp=exp(pos->model_probability(i)) ;
pn=exp(neg->model_probability(i)) ;
p=a*pp+(1-a)*pn ;
float64_t d2=(pp-pn)/p ;
fprintf(stderr, "d1=%e d2=%e, d1-d2=%e\n",d1,d2) ;*/
} ;
} else
{
float64_t pp=pos->model_probability(i) ;
float64_t pn=neg->model_probability(i) ;
float64_t sub=pp ;
if (pn>pp) sub=pn ;
pp-=sub ;
pn-=sub ;
pp=exp(pp) ;
pn=exp(pn) ;
float64_t p=a*pp+(1-a)*pn ;
deriv+=(pp-pn)/p ;
} ;
return deriv ;
}
float64_t CFKFeatures::set_opt_a(float64_t a)
{
if (a==-1)
{
SG_INFO("estimating a.\n")
pos_prob=SG_MALLOC(float64_t, pos->get_observations()->get_num_vectors());
neg_prob=SG_MALLOC(float64_t, pos->get_observations()->get_num_vectors());
for (int32_t i=0; i<pos->get_observations()->get_num_vectors(); i++)
{
pos_prob[i]=pos->model_probability(i) ;
neg_prob[i]=neg->model_probability(i) ;
}
float64_t la=0;
float64_t ua=1;
a=(la+ua)/2;
while (CMath::abs(ua-la)>1e-6)
{
float64_t da=deriv_a(a);
if (da>0)
la=a;
if (da<=0)
ua=a;
a=(la+ua)/2;
SG_INFO("opt_a: a=%1.3e deriv=%1.3e la=%1.3e ua=%1.3e\n", a, da, la ,ua)
}
SG_FREE(pos_prob);
SG_FREE(neg_prob);
pos_prob=NULL;
neg_prob=NULL;
}
weight_a=a;
SG_INFO("setting opt_a: %g\n", a)
return a;
}
void CFKFeatures::set_models(CHMM* p, CHMM* n)
{
ASSERT(p && n)
SG_REF(p);
SG_REF(n);
pos=p;
neg=n;
set_num_vectors(0);
free_feature_matrix();
SG_INFO("pos_feat=[%i,%i,%i,%i],neg_feat=[%i,%i,%i,%i]\n", pos->get_N(), pos->get_N(), pos->get_N()*pos->get_N(), pos->get_N()*pos->get_M(), neg->get_N(), neg->get_N(), neg->get_N()*neg->get_N(), neg->get_N()*neg->get_M())
if (pos && pos->get_observations())
set_num_vectors(pos->get_observations()->get_num_vectors());
if (pos && neg)
num_features=1+pos->get_N()*(1+pos->get_N()+1+pos->get_M()) + neg->get_N()*(1+neg->get_N()+1+neg->get_M()) ;
}
float64_t* CFKFeatures::compute_feature_vector(
int32_t num, int32_t &len, float64_t* target)
{
float64_t* featurevector=target;
if (!featurevector)
featurevector=SG_MALLOC(float64_t,
1+
pos->get_N()*(1+pos->get_N()+1+pos->get_M())+
neg->get_N()*(1+neg->get_N()+1+neg->get_M())
);
if (!featurevector)
return NULL;
compute_feature_vector(featurevector, num, len);
return featurevector;
}
void CFKFeatures::compute_feature_vector(
float64_t* featurevector, int32_t num, int32_t& len)
{
int32_t i,j,p=0,x=num;
float64_t posx=pos->model_probability(x);
float64_t negx=neg->model_probability(x);
len=1+pos->get_N()*(1+pos->get_N()+1+pos->get_M()) + neg->get_N()*(1+neg->get_N()+1+neg->get_M());
featurevector[p++] = deriv_a(weight_a, x);
float64_t px=CMath::logarithmic_sum(
posx+log(weight_a),negx+log(1-weight_a));
//first do positive model
for (i=0; i<pos->get_N(); i++)
{
featurevector[p++]=weight_a*exp(pos->model_derivative_p(i, x)-px);
featurevector[p++]=weight_a*exp(pos->model_derivative_q(i, x)-px);
for (j=0; j<pos->get_N(); j++) {
featurevector[p++]=weight_a*exp(pos->model_derivative_a(i, j, x)-px);
}
for (j=0; j<pos->get_M(); j++) {
featurevector[p++]=weight_a*exp(pos->model_derivative_b(i, j, x)-px);
}
}
//then do negative
for (i=0; i<neg->get_N(); i++)
{
featurevector[p++]= (1-weight_a)*exp(neg->model_derivative_p(i, x)-px);
featurevector[p++]= (1-weight_a)* exp(neg->model_derivative_q(i, x)-px);
for (j=0; j<neg->get_N(); j++) {
featurevector[p++]= (1-weight_a)*exp(neg->model_derivative_a(i, j, x)-px);
}
for (j=0; j<neg->get_M(); j++) {
featurevector[p++]= (1-weight_a)*exp(neg->model_derivative_b(i, j, x)-px);
}
}
}
float64_t* CFKFeatures::set_feature_matrix()
{
ASSERT(pos)
ASSERT(pos->get_observations())
ASSERT(neg)
ASSERT(neg->get_observations())
int32_t len=0;
num_features=1+ pos->get_N()*(1+pos->get_N()+1+pos->get_M()) + neg->get_N()*(1+neg->get_N()+1+neg->get_M());
num_vectors=pos->get_observations()->get_num_vectors();
ASSERT(num_vectors)
SG_INFO("allocating FK feature cache of size %.2fM\n", sizeof(float64_t)*num_features*num_vectors/1024.0/1024.0)
free_feature_matrix();
feature_matrix=SGMatrix<float64_t>(num_features,num_vectors);
SG_INFO("calculating FK feature matrix\n")
for (int32_t x=0; x<num_vectors; x++)
{
if (!(x % (num_vectors/10+1)))
SG_DEBUG("%02d%%.", (int) (100.0*x/num_vectors))
else if (!(x % (num_vectors/200+1)))
SG_DEBUG(".")
compute_feature_vector(&feature_matrix.matrix[x*num_features], x, len);
}
SG_DONE()
num_vectors=get_num_vectors();
num_features=get_num_features();
return feature_matrix.matrix;
}
void CFKFeatures::init()
{
pos = NULL;
neg = NULL;
pos_prob = NULL;
neg_prob = NULL;
weight_a = 0.0;
unset_generic();
//TODO serialize HMMs
//m_parameters->add((CSGObject**) &pos, "pos", "HMM for positive class.");
//m_parameters->add((CSGObject**) &neg, "neg", "HMM for negative class.");
m_parameters->add(&weight_a, "weight_a", "Class prior.");
watch_param("weight_a", &weight_a, AnyParameterProperties("Class prior."));
}