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 
15 using namespace shogun;
16 
18 
21 
24 
27 
29  {
30  if(feats.randomcoeff_additive==NULL)
31  {
32  throw ShogunException(
33  "void CRandomFourierGaussPreproc::copy(...): feats.randomcoeff_additive==NULL && cur_dim_feature_space>0 \n");
34  }
35 
38  }
39  else
40  {
41  randomcoeff_additive = NULL;
42  }
43 
45  {
46  if(feats.randomcoeff_multiplicative==NULL)
47  {
48  throw ShogunException(
49  "void CRandomFourierGaussPreproc::copy(...): feats.randomcoeff_multiplicative==NULL && cur_dim_feature_space>0 &&(cur_dim_input_space>0) \n");
50  }
51 
54  }
55  else
56  {
58  }
59 
60 }
61 
64  dim_feature_space = 1000;
65  dim_input_space = 0;
68 
71 
72  kernelwidth=1;
74 
75  //m_parameter is inherited from CSGObject,
76  //serialization initialization
77  if(m_parameters)
78  {
79  SG_ADD(&dim_input_space, "dim_input_space",
80  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
81  SG_ADD(&cur_dim_input_space, "cur_dim_input_space",
82  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
83  SG_ADD(&dim_feature_space, "dim_feature_space",
84  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
85  SG_ADD(&cur_dim_feature_space, "cur_dim_feature_space",
86  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
87 
88  SG_ADD(&kernelwidth, "kernelwidth", "Kernel width.", MS_AVAILABLE);
89  SG_ADD(&cur_kernelwidth, "cur_kernelwidth", "Kernel width.", MS_AVAILABLE);
90 
93  }
94 
95 }
96 
98  const CRandomFourierGaussPreproc & feats) :
100 
103 
104  //m_parameter is inherited from CSGObject,
105  //serialization initialization
106  if(m_parameters)
107  {
108  SG_ADD(&dim_input_space, "dim_input_space",
109  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
110  SG_ADD(&cur_dim_input_space, "cur_dim_input_space",
111  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
112  SG_ADD(&dim_feature_space, "dim_feature_space",
113  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
114  SG_ADD(&cur_dim_feature_space, "cur_dim_feature_space",
115  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
116 
117  SG_ADD(&kernelwidth, "kernelwidth", "Kernel width.", MS_AVAILABLE);
118  SG_ADD(&cur_kernelwidth, "cur_kernelwidth", "Kernel width.", MS_AVAILABLE);
119 
122  }
123 
124  copy(feats);
125 }
126 
128 
130  SG_FREE(randomcoeff_additive);
131 
132 }
133 
135  return C_DENSE;
136 }
137 
139  return F_DREAL;
140 }
141 
143  return ((int32_t) dim_feature_space);
144 }
145 
147  if (dim <= 0) {
148  throw ShogunException(
149  "void CRandomFourierGaussPreproc::set_dim_feature_space(const int32 dim): dim<=0 is not allowed");
150  }
151 
152  dim_feature_space = dim;
153 
154 }
155 
157  return ((int32_t) dim_input_space);
158 }
159 
161  if (kernelwidth2 <= 0) {
162  throw ShogunException(
163  "void CRandomFourierGaussPreproc::set_kernelwidth(const float64_t kernelwidth2 ): kernelwidth2 <= 0 is not allowed");
164  }
165  kernelwidth=kernelwidth2;
166 }
167 
169  return (kernelwidth);
170 }
171 
173  if (dim <= 0) {
174  throw ShogunException(
175  "void CRandomFourierGaussPreproc::set_dim_input_space(const int32 dim): dim<=0 is not allowed");
176  }
177 
178  dim_input_space = dim;
179 
180 }
181 
183 
185  && (dim_input_space > 0) && (dim_feature_space > 0)) {
187 
188  // already inited
189  return true;
190  } else {
191  return false;
192  }
193  }
194 
195  return false;
196 }
197 
199  if (dim_feature_space <= 0) {
200  throw ShogunException(
201  "bool CRandomFourierGaussPreproc::init_randomcoefficients(): dim_feature_space<=0 is not allowed\n");
202  }
203  if (dim_input_space <= 0) {
204  throw ShogunException(
205  "bool CRandomFourierGaussPreproc::init_randomcoefficients(): dim_input_space<=0 is not allowed\n");
206  }
207 
208  if (test_rfinited()) {
209  return false;
210  }
211 
212 
213  SG_INFO("initializing randomcoefficients \n")
214 
215  float64_t pi = 3.14159265;
216 
217 
220  SG_FREE(randomcoeff_additive);
222 
223 
228 
230 
231  for (int32_t i = 0; i < cur_dim_feature_space; ++i) {
232  randomcoeff_additive[i] = CMath::random((float64_t) 0.0, 2 * pi);
233  }
234 
235  for (int32_t i = 0; i < cur_dim_feature_space; ++i) {
236  for (int32_t k = 0; k < cur_dim_input_space; ++k) {
237  float64_t x1,x2;
238  float64_t s = 2;
239  while ((s >= 1) ) {
240  // Marsaglia polar for gaussian
241  x1 = CMath::random((float64_t) -1.0, (float64_t) 1.0);
242  x2 = CMath::random((float64_t) -1.0, (float64_t) 1.0);
243  s=x1*x1+x2*x2;
244  }
245 
246  // = x1/CMath::sqrt(val)* CMath::sqrt(-2*CMath::log(val));
247  randomcoeff_multiplicative[i*cur_dim_input_space+k] = x1*CMath::sqrt(-2*CMath::log(s)/s )/kernelwidth;
248  }
249  }
250 
251  SG_INFO("finished: initializing randomcoefficients \n")
252 
253  return true;
254 }
255 
257  float64_t ** randomcoeff_additive2,
258  float64_t ** randomcoeff_multiplicative2, int32_t *dim_feature_space2,
259  int32_t *dim_input_space2, float64_t* kernelwidth2) const {
260 
261  ASSERT(randomcoeff_additive2)
262  ASSERT(randomcoeff_multiplicative2)
263 
264  if (!test_rfinited()) {
265  *dim_feature_space2 = 0;
266  *dim_input_space2 = 0;
267  *kernelwidth2=1;
268  *randomcoeff_additive2 = NULL;
269  *randomcoeff_multiplicative2 = NULL;
270  return;
271  }
272 
273  *dim_feature_space2 = cur_dim_feature_space;
274  *dim_input_space2 = cur_dim_input_space;
275  *kernelwidth2=cur_kernelwidth;
276 
277  *randomcoeff_additive2 = SG_MALLOC(float64_t, cur_dim_feature_space);
278  *randomcoeff_multiplicative2 = SG_MALLOC(float64_t, cur_dim_feature_space*cur_dim_input_space);
279 
281  *randomcoeff_additive2);
283  *randomcoeff_multiplicative2);
284 
285 
286 }
287 
289  float64_t *randomcoeff_additive2,
290  float64_t * randomcoeff_multiplicative2,
291  const int32_t dim_feature_space2, const int32_t dim_input_space2, const float64_t kernelwidth2) {
292  dim_feature_space = dim_feature_space2;
293  dim_input_space = dim_input_space2;
294  kernelwidth=kernelwidth2;
295 
298  SG_FREE(randomcoeff_additive);
300 
304 
305  if( (dim_feature_space>0) && (dim_input_space>0) )
306  {
309 
310  std::copy(randomcoeff_additive2, randomcoeff_additive2
312  std::copy(randomcoeff_multiplicative2, randomcoeff_multiplicative2
313  + cur_dim_feature_space*cur_dim_input_space, randomcoeff_multiplicative);
314  }
315 
316 }
317 
318 bool CRandomFourierGaussPreproc::init(CFeatures *f) {
319  if (f->get_feature_class() != get_feature_class()) {
320  throw ShogunException(
321  "CRandomFourierGaussPreproc::init (CFeatures *f) requires CDenseFeatures<float64_t> as features\n");
322  }
323  if (f->get_feature_type() != get_feature_type()) {
324  throw ShogunException(
325  "CRandomFourierGaussPreproc::init (CFeatures *f) requires CDenseFeatures<float64_t> as features\n");
326  }
327  if (dim_feature_space <= 0) {
328  throw ShogunException(
329  "CRandomFourierGaussPreproc::init (CFeatures *f): dim_feature_space<=0 is not allowed, use void set_dim_feature_space(const int32 dim) before!\n");
330  }
331 
332  SG_INFO("calling CRandomFourierGaussPreproc::init(...)\n")
333  int32_t num_features =
334  ((CDenseFeatures<float64_t>*) f)->get_num_features();
335 
336  if (!test_rfinited()) {
337  dim_input_space = num_features;
340  return true;
341  } else {
342  dim_input_space = num_features;
343  // does not reinit if dimension is the same to avoid overriding a previous call of set_randomcoefficients(...)
344  bool inited = init_randomcoefficients();
345  return inited;
346  }
347 
348 }
349 
351 {
352  if (!test_rfinited()) {
353  throw ShogunException(
354  "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");
355  }
356 
358  float64_t *res = SG_MALLOC(float64_t, cur_dim_feature_space);
359 
360  for (int32_t od = 0; od < cur_dim_feature_space; ++od) {
361  res[od] = val * cos(randomcoeff_additive[od] + SGVector<float64_t>::dot(vector.vector,
362  randomcoeff_multiplicative+od*cur_dim_input_space, cur_dim_input_space));
363  }
364 
366 }
367 
369 {
370  init(features);
371 
372  // version for case dim_feature_space < dim_input space with direct transformation on feature matrix ??
373 
374  int32_t num_vectors = 0;
375  int32_t num_features = 0;
376  float64_t* m = ((CDenseFeatures<float64_t>*) features)->get_feature_matrix(
377  num_features, num_vectors);
378  SG_INFO("get Feature matrix: %ix%i\n", num_vectors, num_features)
379 
380  if (num_features!=cur_dim_input_space)
381  {
382  throw ShogunException(
383  "float64_t * CRandomFourierGaussPreproc::apply_to_feature_matrix(CFeatures *f): num_features!=cur_dim_input_space is not allowed\n");
384  }
385 
386  if (m)
387  {
389 
391 
392  for (int32_t vec = 0; vec < num_vectors; vec++)
393  {
394  for (int32_t od = 0; od < cur_dim_feature_space; ++od)
395  {
396  res.matrix[od + vec * cur_dim_feature_space] = val * cos(
398  + SGVector<float64_t>::dot(m+vec * num_features,
400  cur_dim_input_space));
401  }
402  }
403  ((CDenseFeatures<float64_t>*) features)->set_feature_matrix(res);
404 
405  return res;
406  }
407  else
408  return SGMatrix<float64_t>();
409 }
410 
412 {
413 
414 }

SHOGUN Machine Learning Toolbox - Documentation