SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RandomFourierGaussPreproc.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) 2010-2011 Alexander Binder
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  * Copyright (C) 2010-2011 Berlin Institute of Technology
10  */
11 
13 #include <cmath>
14 #include <vector>
15 #include <algorithm>
16 
17 using namespace shogun;
18 
20 
23 
26 
29 
31  {
32  if(feats.randomcoeff_additive==NULL)
33  {
34  throw ShogunException(
35  "void CRandomFourierGaussPreproc::copy(...): feats.randomcoeff_additive==NULL && cur_dim_feature_space>0 \n");
36  }
37 
40  }
41  else
42  {
43  randomcoeff_additive = NULL;
44  }
45 
47  {
48  if(feats.randomcoeff_multiplicative==NULL)
49  {
50  throw ShogunException(
51  "void CRandomFourierGaussPreproc::copy(...): feats.randomcoeff_multiplicative==NULL && cur_dim_feature_space>0 &&(cur_dim_input_space>0) \n");
52  }
53 
56  }
57  else
58  {
60  }
61 
62 }
63 
66  dim_feature_space = 1000;
67  dim_input_space = 0;
70 
73 
74  kernelwidth=1;
76 
77  //m_parameter is inherited from CSGObject,
78  //serialization initialization
79  if(m_parameters)
80  {
81  SG_ADD(&dim_input_space, "dim_input_space",
82  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
83  SG_ADD(&cur_dim_input_space, "cur_dim_input_space",
84  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
85  SG_ADD(&dim_feature_space, "dim_feature_space",
86  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
87  SG_ADD(&cur_dim_feature_space, "cur_dim_feature_space",
88  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
89 
90  SG_ADD(&kernelwidth, "kernelwidth", "Kernel width.", MS_AVAILABLE);
91  SG_ADD(&cur_kernelwidth, "cur_kernelwidth", "Kernel width.", MS_AVAILABLE);
92 
95  }
96 
97 }
98 
100  const CRandomFourierGaussPreproc & feats) :
102 
105 
106  //m_parameter is inherited from CSGObject,
107  //serialization initialization
108  if(m_parameters)
109  {
110  SG_ADD(&dim_input_space, "dim_input_space",
111  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
112  SG_ADD(&cur_dim_input_space, "cur_dim_input_space",
113  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
114  SG_ADD(&dim_feature_space, "dim_feature_space",
115  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
116  SG_ADD(&cur_dim_feature_space, "cur_dim_feature_space",
117  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
118 
119  SG_ADD(&kernelwidth, "kernelwidth", "Kernel width.", MS_AVAILABLE);
120  SG_ADD(&cur_kernelwidth, "cur_kernelwidth", "Kernel width.", MS_AVAILABLE);
121 
124  }
125 
126  copy(feats);
127 }
128 
130 
132  SG_FREE(randomcoeff_additive);
133 
134 }
135 
137  return C_DENSE;
138 }
139 
141  return F_DREAL;
142 }
143 
145  return ((int32_t) dim_feature_space);
146 }
147 
149  if (dim <= 0) {
150  throw ShogunException(
151  "void CRandomFourierGaussPreproc::set_dim_feature_space(const int32 dim): dim<=0 is not allowed");
152  }
153 
154  dim_feature_space = dim;
155 
156 }
157 
159  return ((int32_t) dim_input_space);
160 }
161 
163  if (kernelwidth2 <= 0) {
164  throw ShogunException(
165  "void CRandomFourierGaussPreproc::set_kernelwidth(const float64_t kernelwidth2 ): kernelwidth2 <= 0 is not allowed");
166  }
167  kernelwidth=kernelwidth2;
168 }
169 
171  return (kernelwidth);
172 }
173 
175  if (dim <= 0) {
176  throw ShogunException(
177  "void CRandomFourierGaussPreproc::set_dim_input_space(const int32 dim): dim<=0 is not allowed");
178  }
179 
180  dim_input_space = dim;
181 
182 }
183 
185 
187  && (dim_input_space > 0) && (dim_feature_space > 0)) {
189 
190  // already inited
191  return true;
192  } else {
193  return false;
194  }
195  }
196 
197  return false;
198 }
199 
201  if (dim_feature_space <= 0) {
202  throw ShogunException(
203  "bool CRandomFourierGaussPreproc::init_randomcoefficients(): dim_feature_space<=0 is not allowed\n");
204  }
205  if (dim_input_space <= 0) {
206  throw ShogunException(
207  "bool CRandomFourierGaussPreproc::init_randomcoefficients(): dim_input_space<=0 is not allowed\n");
208  }
209 
210  if (test_rfinited()) {
211  return false;
212  }
213 
214 
215  SG_INFO("initializing randomcoefficients \n")
216 
217  float64_t pi = 3.14159265;
218 
219 
222  SG_FREE(randomcoeff_additive);
224 
225 
230 
232 
233  for (int32_t i = 0; i < cur_dim_feature_space; ++i) {
234  randomcoeff_additive[i] = CMath::random((float64_t) 0.0, 2 * pi);
235  }
236 
237  for (int32_t i = 0; i < cur_dim_feature_space; ++i) {
238  for (int32_t k = 0; k < cur_dim_input_space; ++k) {
239  float64_t x1,x2;
240  float64_t s = 2;
241  while ((s >= 1) ) {
242  // Marsaglia polar for gaussian
243  x1 = CMath::random((float64_t) -1.0, (float64_t) 1.0);
244  x2 = CMath::random((float64_t) -1.0, (float64_t) 1.0);
245  s=x1*x1+x2*x2;
246  }
247 
248  // = x1/CMath::sqrt(val)* CMath::sqrt(-2*CMath::log(val));
249  randomcoeff_multiplicative[i*cur_dim_input_space+k] = x1*CMath::sqrt(-2*CMath::log(s)/s )/kernelwidth;
250  }
251  }
252 
253  SG_INFO("finished: initializing randomcoefficients \n")
254 
255  return true;
256 }
257 
259  float64_t ** randomcoeff_additive2,
260  float64_t ** randomcoeff_multiplicative2, int32_t *dim_feature_space2,
261  int32_t *dim_input_space2, float64_t* kernelwidth2) const {
262 
263  ASSERT(randomcoeff_additive2)
264  ASSERT(randomcoeff_multiplicative2)
265 
266  if (!test_rfinited()) {
267  *dim_feature_space2 = 0;
268  *dim_input_space2 = 0;
269  *kernelwidth2=1;
270  *randomcoeff_additive2 = NULL;
271  *randomcoeff_multiplicative2 = NULL;
272  return;
273  }
274 
275  *dim_feature_space2 = cur_dim_feature_space;
276  *dim_input_space2 = cur_dim_input_space;
277  *kernelwidth2=cur_kernelwidth;
278 
279  *randomcoeff_additive2 = SG_MALLOC(float64_t, cur_dim_feature_space);
280  *randomcoeff_multiplicative2 = SG_MALLOC(float64_t, cur_dim_feature_space*cur_dim_input_space);
281 
283  *randomcoeff_additive2);
285  *randomcoeff_multiplicative2);
286 
287 
288 }
289 
291  float64_t *randomcoeff_additive2,
292  float64_t * randomcoeff_multiplicative2,
293  const int32_t dim_feature_space2, const int32_t dim_input_space2, const float64_t kernelwidth2) {
294  dim_feature_space = dim_feature_space2;
295  dim_input_space = dim_input_space2;
296  kernelwidth=kernelwidth2;
297 
300  SG_FREE(randomcoeff_additive);
302 
306 
307  if( (dim_feature_space>0) && (dim_input_space>0) )
308  {
311 
312  std::copy(randomcoeff_additive2, randomcoeff_additive2
314  std::copy(randomcoeff_multiplicative2, randomcoeff_multiplicative2
315  + cur_dim_feature_space*cur_dim_input_space, randomcoeff_multiplicative);
316  }
317 
318 }
319 
320 bool CRandomFourierGaussPreproc::init(CFeatures *f) {
321  if (f->get_feature_class() != get_feature_class()) {
322  throw ShogunException(
323  "CRandomFourierGaussPreproc::init (CFeatures *f) requires CDenseFeatures<float64_t> as features\n");
324  }
325  if (f->get_feature_type() != get_feature_type()) {
326  throw ShogunException(
327  "CRandomFourierGaussPreproc::init (CFeatures *f) requires CDenseFeatures<float64_t> as features\n");
328  }
329  if (dim_feature_space <= 0) {
330  throw ShogunException(
331  "CRandomFourierGaussPreproc::init (CFeatures *f): dim_feature_space<=0 is not allowed, use void set_dim_feature_space(const int32 dim) before!\n");
332  }
333 
334  SG_INFO("calling CRandomFourierGaussPreproc::init(...)\n")
335  int32_t num_features =
336  ((CDenseFeatures<float64_t>*) f)->get_num_features();
337 
338  if (!test_rfinited()) {
339  dim_input_space = num_features;
342  return true;
343  } else {
344  dim_input_space = num_features;
345  // does not reinit if dimension is the same to avoid overriding a previous call of set_randomcoefficients(...)
346  bool inited = init_randomcoefficients();
347  return inited;
348  }
349 
350 }
351 
353 {
354  if (!test_rfinited()) {
355  throw ShogunException(
356  "float64_t * CRandomFourierGaussPreproc::apply_to_feature_vector(...): test_rfinited()==false: you need to call before CRandomFourierGaussPreproc::init (CFeatures *f) OR 1. set_dim_feature_space(const int32 dim), 2. set_dim_input_space(const int32 dim), 3. init_randomcoefficients() or set_randomcoefficients(...) \n");
357  }
358 
360  float64_t *res = SG_MALLOC(float64_t, cur_dim_feature_space);
361 
362  for (int32_t od = 0; od < cur_dim_feature_space; ++od) {
363  res[od] = val * cos(randomcoeff_additive[od] + SGVector<float64_t>::dot(vector.vector,
364  randomcoeff_multiplicative+od*cur_dim_input_space, cur_dim_input_space));
365  }
366 
368 }
369 
371 {
372  init(features);
373 
374  // version for case dim_feature_space < dim_input space with direct transformation on feature matrix ??
375 
376  int32_t num_vectors = 0;
377  int32_t num_features = 0;
378  float64_t* m = ((CDenseFeatures<float64_t>*) features)->get_feature_matrix(
379  num_features, num_vectors);
380  SG_INFO("get Feature matrix: %ix%i\n", num_vectors, num_features)
381 
382  if (num_features!=cur_dim_input_space)
383  {
384  throw ShogunException(
385  "float64_t * CRandomFourierGaussPreproc::apply_to_feature_matrix(CFeatures *f): num_features!=cur_dim_input_space is not allowed\n");
386  }
387 
388  if (m)
389  {
391 
393 
394  for (int32_t vec = 0; vec < num_vectors; vec++)
395  {
396  for (int32_t od = 0; od < cur_dim_feature_space; ++od)
397  {
398  res.matrix[od + vec * cur_dim_feature_space] = val * cos(
400  + SGVector<float64_t>::dot(m+vec * num_features,
402  cur_dim_input_space));
403  }
404  }
405  ((CDenseFeatures<float64_t>*) features)->set_feature_matrix(res);
406 
407  return res;
408  }
409  else
410  return SGMatrix<float64_t>();
411 }
412 
414 {
415 
416 }

SHOGUN Machine Learning Toolbox - Documentation