SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
RandomFourierGaussPreproc.cpp
浏览该文件的文档.
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 
14 #include <cmath>
15 #include <vector>
16 #include <algorithm>
17 
18 using namespace shogun;
19 
21 
24 
27 
30 
32  {
33  if(feats.randomcoeff_additive==NULL)
34  {
35  throw ShogunException(
36  "void CRandomFourierGaussPreproc::copy(...): feats.randomcoeff_additive==NULL && cur_dim_feature_space>0 \n");
37  }
38 
41  }
42  else
43  {
44  randomcoeff_additive = NULL;
45  }
46 
48  {
49  if(feats.randomcoeff_multiplicative==NULL)
50  {
51  throw ShogunException(
52  "void CRandomFourierGaussPreproc::copy(...): feats.randomcoeff_multiplicative==NULL && cur_dim_feature_space>0 &&(cur_dim_input_space>0) \n");
53  }
54 
57  }
58  else
59  {
61  }
62 
63 }
64 
67  dim_feature_space = 1000;
68  dim_input_space = 0;
71 
74 
75  kernelwidth=1;
77 
78  //m_parameter is inherited from CSGObject,
79  //serialization initialization
80  if(m_parameters)
81  {
82  SG_ADD(&dim_input_space, "dim_input_space",
83  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
84  SG_ADD(&cur_dim_input_space, "cur_dim_input_space",
85  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
86  SG_ADD(&dim_feature_space, "dim_feature_space",
87  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
88  SG_ADD(&cur_dim_feature_space, "cur_dim_feature_space",
89  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
90 
91  SG_ADD(&kernelwidth, "kernelwidth", "Kernel width.", MS_AVAILABLE);
92  SG_ADD(&cur_kernelwidth, "cur_kernelwidth", "Kernel width.", MS_AVAILABLE);
93 
96  }
97 
98 }
99 
101  const CRandomFourierGaussPreproc & feats) :
103 
106 
107  //m_parameter is inherited from CSGObject,
108  //serialization initialization
109  if(m_parameters)
110  {
111  SG_ADD(&dim_input_space, "dim_input_space",
112  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
113  SG_ADD(&cur_dim_input_space, "cur_dim_input_space",
114  "Dimensionality of the input space.", MS_NOT_AVAILABLE);
115  SG_ADD(&dim_feature_space, "dim_feature_space",
116  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
117  SG_ADD(&cur_dim_feature_space, "cur_dim_feature_space",
118  "Dimensionality of the feature space.", MS_NOT_AVAILABLE);
119 
120  SG_ADD(&kernelwidth, "kernelwidth", "Kernel width.", MS_AVAILABLE);
121  SG_ADD(&cur_kernelwidth, "cur_kernelwidth", "Kernel width.", MS_AVAILABLE);
122 
125  }
126 
127  copy(feats);
128 }
129 
131 
133  SG_FREE(randomcoeff_additive);
134 
135 }
136 
138  return C_DENSE;
139 }
140 
142  return F_DREAL;
143 }
144 
146  return ((int32_t) dim_feature_space);
147 }
148 
150  if (dim <= 0) {
151  throw ShogunException(
152  "void CRandomFourierGaussPreproc::set_dim_feature_space(const int32 dim): dim<=0 is not allowed");
153  }
154 
155  dim_feature_space = dim;
156 
157 }
158 
160  return ((int32_t) dim_input_space);
161 }
162 
164  if (kernelwidth2 <= 0) {
165  throw ShogunException(
166  "void CRandomFourierGaussPreproc::set_kernelwidth(const float64_t kernelwidth2 ): kernelwidth2 <= 0 is not allowed");
167  }
168  kernelwidth=kernelwidth2;
169 }
170 
172  return (kernelwidth);
173 }
174 
176  if (dim <= 0) {
177  throw ShogunException(
178  "void CRandomFourierGaussPreproc::set_dim_input_space(const int32 dim): dim<=0 is not allowed");
179  }
180 
181  dim_input_space = dim;
182 
183 }
184 
186 
188  && (dim_input_space > 0) && (dim_feature_space > 0)) {
190 
191  // already inited
192  return true;
193  } else {
194  return false;
195  }
196  }
197 
198  return false;
199 }
200 
202  if (dim_feature_space <= 0) {
203  throw ShogunException(
204  "bool CRandomFourierGaussPreproc::init_randomcoefficients(): dim_feature_space<=0 is not allowed\n");
205  }
206  if (dim_input_space <= 0) {
207  throw ShogunException(
208  "bool CRandomFourierGaussPreproc::init_randomcoefficients(): dim_input_space<=0 is not allowed\n");
209  }
210 
211  if (test_rfinited()) {
212  return false;
213  }
214 
215 
216  SG_INFO("initializing randomcoefficients \n")
217 
218  float64_t pi = 3.14159265;
219 
220 
223  SG_FREE(randomcoeff_additive);
225 
226 
231 
233 
234  for (int32_t i = 0; i < cur_dim_feature_space; ++i) {
235  randomcoeff_additive[i] = CMath::random((float64_t) 0.0, 2 * pi);
236  }
237 
238  for (int32_t i = 0; i < cur_dim_feature_space; ++i) {
239  for (int32_t k = 0; k < cur_dim_input_space; ++k) {
240  float64_t x1,x2;
241  float64_t s = 2;
242  while ((s >= 1) ) {
243  // Marsaglia polar for gaussian
244  x1 = CMath::random((float64_t) -1.0, (float64_t) 1.0);
245  x2 = CMath::random((float64_t) -1.0, (float64_t) 1.0);
246  s=x1*x1+x2*x2;
247  }
248 
249  // = x1/CMath::sqrt(val)* CMath::sqrt(-2*CMath::log(val));
250  randomcoeff_multiplicative[i*cur_dim_input_space+k] = x1*CMath::sqrt(-2*CMath::log(s)/s )/kernelwidth;
251  }
252  }
253 
254  SG_INFO("finished: initializing randomcoefficients \n")
255 
256  return true;
257 }
258 
260  float64_t ** randomcoeff_additive2,
261  float64_t ** randomcoeff_multiplicative2, int32_t *dim_feature_space2,
262  int32_t *dim_input_space2, float64_t* kernelwidth2) const {
263 
264  ASSERT(randomcoeff_additive2)
265  ASSERT(randomcoeff_multiplicative2)
266 
267  if (!test_rfinited()) {
268  *dim_feature_space2 = 0;
269  *dim_input_space2 = 0;
270  *kernelwidth2=1;
271  *randomcoeff_additive2 = NULL;
272  *randomcoeff_multiplicative2 = NULL;
273  return;
274  }
275 
276  *dim_feature_space2 = cur_dim_feature_space;
277  *dim_input_space2 = cur_dim_input_space;
278  *kernelwidth2=cur_kernelwidth;
279 
280  *randomcoeff_additive2 = SG_MALLOC(float64_t, cur_dim_feature_space);
281  *randomcoeff_multiplicative2 = SG_MALLOC(float64_t, cur_dim_feature_space*cur_dim_input_space);
282 
284  *randomcoeff_additive2);
286  *randomcoeff_multiplicative2);
287 
288 
289 }
290 
292  float64_t *randomcoeff_additive2,
293  float64_t * randomcoeff_multiplicative2,
294  const int32_t dim_feature_space2, const int32_t dim_input_space2, const float64_t kernelwidth2) {
295  dim_feature_space = dim_feature_space2;
296  dim_input_space = dim_input_space2;
297  kernelwidth=kernelwidth2;
298 
301  SG_FREE(randomcoeff_additive);
303 
307 
308  if( (dim_feature_space>0) && (dim_input_space>0) )
309  {
312 
313  std::copy(randomcoeff_additive2, randomcoeff_additive2
315  std::copy(randomcoeff_multiplicative2, randomcoeff_multiplicative2
316  + cur_dim_feature_space*cur_dim_input_space, randomcoeff_multiplicative);
317  }
318 
319 }
320 
321 bool CRandomFourierGaussPreproc::init(CFeatures *f) {
322  if (f->get_feature_class() != get_feature_class()) {
323  throw ShogunException(
324  "CRandomFourierGaussPreproc::init (CFeatures *f) requires CDenseFeatures<float64_t> as features\n");
325  }
326  if (f->get_feature_type() != get_feature_type()) {
327  throw ShogunException(
328  "CRandomFourierGaussPreproc::init (CFeatures *f) requires CDenseFeatures<float64_t> as features\n");
329  }
330  if (dim_feature_space <= 0) {
331  throw ShogunException(
332  "CRandomFourierGaussPreproc::init (CFeatures *f): dim_feature_space<=0 is not allowed, use void set_dim_feature_space(const int32 dim) before!\n");
333  }
334 
335  SG_INFO("calling CRandomFourierGaussPreproc::init(...)\n")
336  int32_t num_features =
337  ((CDenseFeatures<float64_t>*) f)->get_num_features();
338 
339  if (!test_rfinited()) {
340  dim_input_space = num_features;
343  return true;
344  } else {
345  dim_input_space = num_features;
346  // does not reinit if dimension is the same to avoid overriding a previous call of set_randomcoefficients(...)
347  bool inited = init_randomcoefficients();
348  return inited;
349  }
350 
351 }
352 
354 {
355  if (!test_rfinited()) {
356  throw ShogunException(
357  "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");
358  }
359 
361  float64_t *res = SG_MALLOC(float64_t, cur_dim_feature_space);
362 
363  for (int32_t od = 0; od < cur_dim_feature_space; ++od) {
364  res[od] = val * cos(randomcoeff_additive[od] + CMath::dot(vector.vector,
365  randomcoeff_multiplicative+od*cur_dim_input_space, cur_dim_input_space));
366  }
367 
369 }
370 
372 {
373  init(features);
374 
375  // version for case dim_feature_space < dim_input space with direct transformation on feature matrix ??
376 
377  int32_t num_vectors = 0;
378  int32_t num_features = 0;
379  float64_t* m = ((CDenseFeatures<float64_t>*) features)->get_feature_matrix(
380  num_features, num_vectors);
381  SG_INFO("get Feature matrix: %ix%i\n", num_vectors, num_features)
382 
383  if (num_features!=cur_dim_input_space)
384  {
385  throw ShogunException(
386  "float64_t * CRandomFourierGaussPreproc::apply_to_feature_matrix(CFeatures *f): num_features!=cur_dim_input_space is not allowed\n");
387  }
388 
389  if (m)
390  {
392 
394 
395  for (int32_t vec = 0; vec < num_vectors; vec++)
396  {
397  for (int32_t od = 0; od < cur_dim_feature_space; ++od)
398  {
399  res.matrix[od + vec * cur_dim_feature_space] = val * cos(
401  + CMath::dot(m+vec * num_features,
403  cur_dim_input_space));
404  }
405  }
406  ((CDenseFeatures<float64_t>*) features)->set_feature_matrix(res);
407 
408  return res;
409  }
410  else
411  return SGMatrix<float64_t>();
412 }
413 
415 {
416 
417 }
#define SG_INFO(...)
Definition: SGIO.h:118
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
Parameter * m_parameters
Definition: SGObject.h:378
virtual SGVector< float64_t > apply_to_feature_vector(SGVector< float64_t > vector)
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
static uint64_t random()
Definition: Math.h:1019
#define ASSERT(x)
Definition: SGIO.h:201
void set_randomcoefficients(float64_t *randomcoeff_additive2, float64_t *randomcoeff_multiplicative2, const int32_t dim_feature_space2, const int32_t dim_input_space2, const float64_t kernelwidth2)
Template class DensePreprocessor, base class for preprocessors (cf. CPreprocessor) that apply to CDen...
double float64_t
Definition: common.h:50
virtual EFeatureClass get_feature_class() const =0
void copy(const CRandomFourierGaussPreproc &feats)
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
Definition: Math.h:627
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
Definition: Parameter.cpp:334
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
static float64_t log(float64_t v)
Definition: Math.h:922
Preprocessor CRandomFourierGaussPreproc implements Random Fourier Features for the Gauss kernel a la ...
#define SG_ADD(...)
Definition: SGObject.h:81
static float32_t sqrt(float32_t x)
Definition: Math.h:459
void add_matrix(bool **param, index_t *length_y, index_t *length_x, const char *name, const char *description="")
Definition: Parameter.cpp:943
void get_randomcoefficients(float64_t **randomcoeff_additive2, float64_t **randomcoeff_multiplicative2, int32_t *dim_feature_space2, int32_t *dim_input_space2, float64_t *kernelwidth2) const
virtual SGMatrix< float64_t > apply_to_feature_matrix(CFeatures *features)
virtual EFeatureType get_feature_type() const =0
static T abs(T a)
Definition: Math.h:179

SHOGUN 机器学习工具包 - 项目文档