/
CombinedDotFeatures.cpp
347 lines (287 loc) · 8.44 KB
/
CombinedDotFeatures.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2009-2010 Soeren Sonnenburg
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
* Copyright (C) 2010 Berlin Institute of Technology
*/
#include <shogun/features/CombinedDotFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/mathematics/Math.h>
using namespace shogun;
CCombinedDotFeatures::CCombinedDotFeatures() : CDotFeatures()
{
init();
feature_array=new CDynamicObjectArray();
update_dim_feature_space_and_num_vec();
}
CCombinedDotFeatures::CCombinedDotFeatures(const CCombinedDotFeatures & orig)
: CDotFeatures(orig), num_vectors(orig.num_vectors),
num_dimensions(orig.num_dimensions)
{
init();
feature_array=new CDynamicObjectArray();
}
CFeatures* CCombinedDotFeatures::duplicate() const
{
return new CCombinedDotFeatures(*this);
}
CCombinedDotFeatures::~CCombinedDotFeatures()
{
SG_UNREF(feature_array);
}
void CCombinedDotFeatures::list_feature_objs()
{
SG_INFO("BEGIN COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions)
this->list_feature_obj();
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
f->list_feature_obj();
SG_UNREF(f);
}
SG_INFO("END COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions)
this->list_feature_obj();
}
void CCombinedDotFeatures::update_dim_feature_space_and_num_vec()
{
int32_t dim=0;
int32_t vec=-1;
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
dim+= f->get_dim_feature_space();
if (vec==-1)
vec=f->get_num_vectors();
else if (vec != f->get_num_vectors())
{
f->list_feature_obj();
SG_ERROR("Number of vectors (%d) mismatches in above feature obj (%d)\n", vec, f->get_num_vectors())
}
SG_UNREF(f);
}
num_dimensions=dim;
num_vectors=vec;
SG_DEBUG("vecs=%d, dims=%d\n", num_vectors, num_dimensions)
}
float64_t CCombinedDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
{
float64_t result=0;
ASSERT(df)
ASSERT(df->get_feature_type() == get_feature_type())
ASSERT(df->get_feature_class() == get_feature_class())
CCombinedDotFeatures* cf = (CCombinedDotFeatures*) df;
// check that both have same number of feature objects inside
ASSERT(get_num_feature_obj()==cf->get_num_feature_obj())
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f1 = get_feature_obj(f_idx);
CDotFeatures* f2 = cf->get_feature_obj(f_idx);
ASSERT(f1)
ASSERT(f2)
result += f1->dot(vec_idx1, f2,vec_idx2) *
f1->get_combined_feature_weight() *
f2->get_combined_feature_weight();
SG_UNREF(f1);
SG_UNREF(f2);
}
return result;
}
float64_t CCombinedDotFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
{
float64_t result=0;
uint32_t offs=0;
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
int32_t dim = f->get_dim_feature_space();
result += f->dense_dot(vec_idx1, vec2+offs, dim)*f->get_combined_feature_weight();
offs += dim;
SG_UNREF(f);
}
return result;
}
void CCombinedDotFeatures::dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b)
{
if (stop<=start)
return;
ASSERT(dim==num_dimensions)
uint32_t offs=0;
bool first=true;
int32_t num=stop-start;
float64_t* tmp=SG_MALLOC(float64_t, num);
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
int32_t f_dim = f->get_dim_feature_space();
if (first)
{
f->dense_dot_range(output, start, stop, alphas, vec+offs, f_dim, b);
first=false;
}
else
{
f->dense_dot_range(tmp, start, stop, alphas, vec+offs, f_dim, b);
for (int32_t i=0; i<num; i++)
output[i]+=tmp[i];
}
offs += f_dim;
SG_UNREF(f);
}
SG_FREE(tmp);
}
void CCombinedDotFeatures::dense_dot_range_subset(int32_t* sub_index, int32_t num, float64_t* output, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b)
{
if (num<=0)
return;
ASSERT(dim==num_dimensions)
uint32_t offs=0;
bool first=true;
float64_t* tmp=SG_MALLOC(float64_t, num);
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
int32_t f_dim = f->get_dim_feature_space();
if (first)
{
f->dense_dot_range_subset(sub_index, num, output, alphas, vec+offs, f_dim, b);
first=false;
}
else
{
f->dense_dot_range_subset(sub_index, num, tmp, alphas, vec+offs, f_dim, b);
for (int32_t i=0; i<num; i++)
output[i]+=tmp[i];
}
offs += f_dim;
SG_UNREF(f);
}
SG_FREE(tmp);
}
void CCombinedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
{
uint32_t offs=0;
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
int32_t dim = f->get_dim_feature_space();
f->add_to_dense_vec(alpha*f->get_combined_feature_weight(), vec_idx1, vec2+offs, dim, abs_val);
offs += dim;
SG_UNREF(f);
}
}
void* CCombinedDotFeatures::get_feature_iterator(int32_t vector_index)
{
combined_feature_iterator* it=SG_MALLOC(combined_feature_iterator, 1);
it->f=get_feature_obj(0);
iterator_idx=0;
it->iterator=it->f->get_feature_iterator(vector_index);
it->vector_index=vector_index;
return it;
}
bool CCombinedDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator)
{
ASSERT(iterator)
combined_feature_iterator* it = (combined_feature_iterator*) iterator;
while (it->f)
{
if (it->f->get_next_feature(index, value, it->iterator))
{
value*=get_combined_feature_weight();
return true;
}
it->f->free_feature_iterator(it->iterator);
it->f=get_feature_obj(++iterator_idx);
if (it->f)
it->iterator=it->f->get_feature_iterator(it->vector_index);
else
it->iterator=NULL;
}
return false;
}
void CCombinedDotFeatures::free_feature_iterator(void* iterator)
{
if (iterator)
{
combined_feature_iterator* it = (combined_feature_iterator*) iterator;
if (it->iterator && it->f)
it->f->free_feature_iterator(it->iterator);
SG_FREE(it);
}
}
CDotFeatures* CCombinedDotFeatures::get_feature_obj(int32_t idx)
{
return (CDotFeatures*) feature_array->get_element(idx);
}
bool CCombinedDotFeatures::insert_feature_obj(CDotFeatures* obj, int32_t idx)
{
ASSERT(obj)
bool result=feature_array->insert_element(obj, idx);
update_dim_feature_space_and_num_vec();
return result;
}
bool CCombinedDotFeatures::append_feature_obj(CDotFeatures* obj)
{
ASSERT(obj)
int n = get_num_feature_obj();
feature_array->push_back(obj);
update_dim_feature_space_and_num_vec();
return n+1==get_num_feature_obj();
}
bool CCombinedDotFeatures::delete_feature_obj(int32_t idx)
{
bool succesful_deletion = feature_array->delete_element(idx);
if (succesful_deletion)
update_dim_feature_space_and_num_vec();
return succesful_deletion;
}
int32_t CCombinedDotFeatures::get_num_feature_obj()
{
return feature_array->get_num_elements();
}
int32_t CCombinedDotFeatures::get_nnz_features_for_vector(int32_t num)
{
int32_t result=0;
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
result+=f->get_nnz_features_for_vector(num);
SG_UNREF(f);
}
return result;
}
SGVector<float64_t> CCombinedDotFeatures::get_subfeature_weights()
{
int32_t num_weights = get_num_feature_obj();
ASSERT(num_weights > 0)
float64_t* weights=SG_MALLOC(float64_t, num_weights);
for (index_t f_idx=0; f_idx<num_weights; f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
weights[f_idx] = f->get_combined_feature_weight();
SG_UNREF(f);
}
return SGVector<float64_t>(weights,num_weights);
}
void CCombinedDotFeatures::set_subfeature_weights(SGVector<float64_t> weights)
{
ASSERT(weights.vlen==get_num_feature_obj())
for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
{
CDotFeatures* f = get_feature_obj(f_idx);
f->set_combined_feature_weight(weights[f_idx]);
SG_UNREF(f);
}
}
void CCombinedDotFeatures::init()
{
m_parameters->add(&num_dimensions, "num_dimensions",
"Total number of dimensions.");
m_parameters->add(&num_vectors, "num_vectors",
"Total number of vectors.");
m_parameters->add((CSGObject**) &feature_array,
"feature_array", "Feature array.");
}