SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CombinedDotFeatures.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) 2009-2010 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  * Copyright (C) 2010 Berlin Institute of Technology
10  */
11 
13 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
19 {
20  init();
21 
24 }
25 
27 : CDotFeatures(orig), num_vectors(orig.num_vectors),
28  num_dimensions(orig.num_dimensions)
29 {
30  init();
31 
33 }
34 
36 {
37  return new CCombinedDotFeatures(*this);
38 }
39 
41 {
43 }
44 
46 {
47  SG_INFO("BEGIN COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions)
48  this->list_feature_obj();
49 
50  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
51  {
52  CDotFeatures* f = get_feature_obj(f_idx);
53  f->list_feature_obj();
54  SG_UNREF(f);
55  }
56 
57  SG_INFO("END COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions)
58  this->list_feature_obj();
59 }
60 
62 {
63  int32_t dim=0;
64  int32_t vec=-1;
65 
66  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
67  {
68  CDotFeatures* f = get_feature_obj(f_idx);
69  dim+= f->get_dim_feature_space();
70  if (vec==-1)
71  vec=f->get_num_vectors();
72  else if (vec != f->get_num_vectors())
73  {
74  f->list_feature_obj();
75  SG_ERROR("Number of vectors (%d) mismatches in above feature obj (%d)\n", vec, f->get_num_vectors())
76  }
77 
78  SG_UNREF(f);
79  }
80 
81  num_dimensions=dim;
82  num_vectors=vec;
83  SG_DEBUG("vecs=%d, dims=%d\n", num_vectors, num_dimensions)
84 }
85 
86 float64_t CCombinedDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
87 {
88  float64_t result=0;
89 
90  ASSERT(df)
94 
95  // check that both have same number of feature objects inside
96  ASSERT(get_num_feature_obj()==cf->get_num_feature_obj())
97 
98  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
99  {
100  CDotFeatures* f1 = get_feature_obj(f_idx);
101  CDotFeatures* f2 = cf->get_feature_obj(f_idx);
102 
103  ASSERT(f1)
104  ASSERT(f2)
105 
106  result += f1->dot(vec_idx1, f2,vec_idx2) *
109 
110  SG_UNREF(f1);
111  SG_UNREF(f2);
112  }
113 
114  return result;
115 }
116 
117 float64_t CCombinedDotFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
118 {
119  float64_t result=0;
120 
121  uint32_t offs=0;
122 
123  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
124  {
125  CDotFeatures* f = get_feature_obj(f_idx);
126  int32_t dim = f->get_dim_feature_space();
127  result += f->dense_dot(vec_idx1, vec2+offs, dim)*f->get_combined_feature_weight();
128  offs += dim;
129 
130  SG_UNREF(f);
131  }
132 
133  return result;
134 }
135 
136 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)
137 {
138  if (stop<=start)
139  return;
140  ASSERT(dim==num_dimensions)
141 
142  uint32_t offs=0;
143  bool first=true;
144  int32_t num=stop-start;
145  float64_t* tmp=SG_MALLOC(float64_t, num);
146 
147  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
148  {
149  CDotFeatures* f = get_feature_obj(f_idx);
150  int32_t f_dim = f->get_dim_feature_space();
151  if (first)
152  {
153  f->dense_dot_range(output, start, stop, alphas, vec+offs, f_dim, b);
154  first=false;
155  }
156  else
157  {
158  f->dense_dot_range(tmp, start, stop, alphas, vec+offs, f_dim, b);
159  for (int32_t i=0; i<num; i++)
160  output[i]+=tmp[i];
161  }
162  offs += f_dim;
163 
164  SG_UNREF(f);
165  }
166  SG_FREE(tmp);
167 }
168 
169 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)
170 {
171  if (num<=0)
172  return;
173  ASSERT(dim==num_dimensions)
174 
175  uint32_t offs=0;
176  bool first=true;
177  float64_t* tmp=SG_MALLOC(float64_t, num);
178 
179  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
180  {
181  CDotFeatures* f = get_feature_obj(f_idx);
182  int32_t f_dim = f->get_dim_feature_space();
183  if (first)
184  {
185  f->dense_dot_range_subset(sub_index, num, output, alphas, vec+offs, f_dim, b);
186  first=false;
187  }
188  else
189  {
190  f->dense_dot_range_subset(sub_index, num, tmp, alphas, vec+offs, f_dim, b);
191  for (int32_t i=0; i<num; i++)
192  output[i]+=tmp[i];
193  }
194  offs += f_dim;
195 
196  SG_UNREF(f);
197  }
198  SG_FREE(tmp);
199 }
200 
201 void CCombinedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
202 {
203  uint32_t offs=0;
204 
205  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
206  {
207  CDotFeatures* f = get_feature_obj(f_idx);
208  int32_t dim = f->get_dim_feature_space();
209  f->add_to_dense_vec(alpha*f->get_combined_feature_weight(), vec_idx1, vec2+offs, dim, abs_val);
210  offs += dim;
211 
212  SG_UNREF(f);
213  }
214 }
215 
217 {
218  combined_feature_iterator* it=SG_MALLOC(combined_feature_iterator, 1);
219 
220  it->f=get_feature_obj(0);
221  iterator_idx=0;
222  it->iterator=it->f->get_feature_iterator(vector_index);
223  it->vector_index=vector_index;
224  return it;
225 }
226 
227 bool CCombinedDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator)
228 {
229  ASSERT(iterator)
230  combined_feature_iterator* it = (combined_feature_iterator*) iterator;
231 
232  while (it->f)
233  {
234  if (it->f->get_next_feature(index, value, it->iterator))
235  {
237  return true;
238  }
239 
241  {
242  index = -1;
243  break;
244  }
245 
246  it->f->free_feature_iterator(it->iterator);
247  SG_UNREF(it->f);
249  if (it->f)
250  it->iterator=it->f->get_feature_iterator(it->vector_index);
251  else
252  it->iterator=NULL;
253  }
254  return false;
255 }
256 
258 {
259  if (iterator)
260  {
261  combined_feature_iterator* it = (combined_feature_iterator*) iterator;
262  if (it->iterator && it->f)
263  it->f->free_feature_iterator(it->iterator);
264  SG_UNREF(it->f);
265  SG_FREE(it);
266  }
267 }
268 
270 {
271  return (CDotFeatures*) feature_array->get_element(idx);
272 }
273 
275 {
276  ASSERT(obj)
277  bool result=feature_array->insert_element(obj, idx);
279  return result;
280 }
281 
283 {
284  ASSERT(obj)
285  int n = get_num_feature_obj();
286  feature_array->push_back(obj);
288  return n+1==get_num_feature_obj();
289 }
290 
292 {
293  bool succesful_deletion = feature_array->delete_element(idx);
294  if (succesful_deletion)
296  return succesful_deletion;
297 }
298 
300 {
302 }
303 
305 {
306  int32_t result=0;
307 
308  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
309  {
310  CDotFeatures* f = get_feature_obj(f_idx);
311  result+=f->get_nnz_features_for_vector(num);
312  SG_UNREF(f);
313  }
314 
315  return result;
316 }
317 
319 {
320  int32_t num_weights = get_num_feature_obj();
321  ASSERT(num_weights > 0)
322 
323  float64_t* weights=SG_MALLOC(float64_t, num_weights);
324 
325  for (index_t f_idx=0; f_idx<num_weights; f_idx++)
326  {
327  CDotFeatures* f = get_feature_obj(f_idx);
328  weights[f_idx] = f->get_combined_feature_weight();
329  SG_UNREF(f);
330  }
331  return SGVector<float64_t>(weights,num_weights);
332 }
333 
335 {
336  ASSERT(weights.vlen==get_num_feature_obj())
337 
338  for (index_t f_idx=0; f_idx<get_num_feature_obj(); f_idx++)
339  {
340  CDotFeatures* f = get_feature_obj(f_idx);
341  f->set_combined_feature_weight(weights[f_idx]);
342  SG_UNREF(f);
343  }
344 }
345 
346 void CCombinedDotFeatures::init()
347 {
348  m_parameters->add(&num_dimensions, "num_dimensions",
349  "Total number of dimensions.");
350  m_parameters->add(&num_vectors, "num_vectors",
351  "Total number of vectors.");
353  "feature_array", "Feature array.");
354 }
355 
virtual void dense_dot_range(float64_t *output, int32_t start, int32_t stop, float64_t *alphas, float64_t *vec, int32_t dim, float64_t b)
Definition: DotFeatures.cpp:67
#define SG_INFO(...)
Definition: SGIO.h:118
int32_t iterator_idx
idx for iterator
virtual int32_t get_nnz_features_for_vector(int32_t num)=0
CDynamicObjectArray * feature_array
int32_t num_vectors
total number of vectors
virtual void dense_dot_range(float64_t *output, int32_t start, int32_t stop, float64_t *alphas, float64_t *vec, int32_t dim, float64_t b)
int32_t index_t
Definition: common.h:62
virtual bool get_next_feature(int32_t &index, float64_t &value, void *iterator)
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
bool insert_feature_obj(CDotFeatures *obj, int32_t idx)
virtual void set_subfeature_weights(SGVector< float64_t > weights)
bool insert_element(CSGObject *e, int32_t index)
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)=0
virtual int32_t get_num_vectors() const =0
#define SG_ERROR(...)
Definition: SGIO.h:129
virtual void 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)
Parameter * m_parameters
Definition: SGObject.h:378
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)=0
virtual CFeatures * duplicate() const
Features that support dot products among other operations.
Definition: DotFeatures.h:44
bool append_feature_obj(CDotFeatures *obj)
virtual int32_t get_dim_feature_space() const =0
virtual EFeatureType get_feature_type() const
virtual SGVector< float64_t > get_subfeature_weights()
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
index_t vlen
Definition: SGVector.h:494
virtual EFeatureClass get_feature_class() const
void list_feature_obj() const
Definition: Features.cpp:171
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
virtual void 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)
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)
double float64_t
Definition: common.h:50
CDotFeatures * get_feature_obj(int32_t idx)
virtual EFeatureClass get_feature_class() const =0
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
virtual int32_t get_nnz_features_for_vector(int32_t num)
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
Features that allow stacking of a number of DotFeatures.
virtual void free_feature_iterator(void *iterator)
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)
CSGObject * get_element(int32_t index) const
void set_combined_feature_weight(float64_t nw)
Definition: DotFeatures.h:160
virtual void * get_feature_iterator(int32_t vector_index)
int32_t num_dimensions
total number of dimensions
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)
float64_t get_combined_feature_weight()
Definition: DotFeatures.h:154
virtual EFeatureType get_feature_type() const =0

SHOGUN Machine Learning Toolbox - Documentation