SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RandomFourierDotFeatures.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) 2013 Evangelos Anagnostopoulos
8  * Copyright (C) 2013 Evangelos Anagnostopoulos
9  */
10 
11 #include <shogun/base/Parameter.h>
14 
15 namespace shogun {
16 
17 enum KernelName;
18 
20 {
22 }
23 
25  int32_t D, KernelName kernel_name, SGVector<float64_t> params)
26 : CRandomKitchenSinksDotFeatures(features, D)
27 {
28  init(kernel_name, params);
30 }
31 
33  int32_t D, KernelName kernel_name, SGVector<float64_t> params,
34  SGMatrix<float64_t> coeff)
35 : CRandomKitchenSinksDotFeatures(features, D, coeff)
36 {
37  init(kernel_name, params);
38 }
39 
41 {
43 }
44 
47 {
48  init(orig.kernel, orig.kernel_params);
49 }
50 
52 {
53 }
54 
55 void CRandomFourierDotFeatures::init(KernelName kernel_name, SGVector<float64_t> params)
56 {
57  kernel = kernel_name;
58  kernel_params = params;
59 
60  constant = num_samples>0 ? CMath::sqrt(2.0 / num_samples) : 1;
61  m_parameters->add(&kernel_params, "kernel_params",
62  "The parameters of the kernel to approximate");
63  SG_ADD((machine_int_t* ) &kernel, "kernel",
64  "The kernel to approximate", MS_NOT_AVAILABLE);
65  SG_ADD(&constant, "constant", "A constant needed",
67 }
68 
70 {
71  return new CRandomFourierDotFeatures(*this);
72 }
73 
75 {
76  return "RandomFourierDotFeatures";
77 }
78 
80 {
81  dot_result += random_coeff(random_coeff.num_rows-1, par_idx);
82  return CMath::cos(dot_result) * constant;
83 }
84 
86 {
88  switch (kernel)
89  {
90  case GAUSSIAN:
91  for (index_t i=0; i<vec.vlen-1; i++)
92  {
93  vec[i] = CMath::sqrt((float64_t) 1/kernel_params[0]) *
94  CMath::sqrt(2.0) * CMath::normal_random(0.0, 1);
95  }
96 
97  vec[vec.vlen-1] = CMath::random(0.0, 2 * CMath::PI);
98  break;
99 
100  default:
101  SG_SERROR("Unknown kernel\n");
102  }
103  return vec;
104 }
105 
106 }
virtual SGVector< float64_t > generate_random_parameter_vector()
class that implements the Random Kitchen Sinks (RKS) for the DotFeatures as mentioned in http://books...
virtual const char * get_name() const
int32_t index_t
Definition: common.h:62
static float32_t normal_random(float32_t mean, float32_t std_dev)
Definition: Math.h:1095
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
Parameter * m_parameters
Definition: SGObject.h:378
virtual CFeatures * duplicate() const
Features that support dot products among other operations.
Definition: DotFeatures.h:44
index_t num_rows
Definition: SGMatrix.h:376
static uint64_t random()
Definition: Math.h:1019
virtual int32_t get_dim_feature_space() const =0
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
double float64_t
Definition: common.h:50
A File access base class.
Definition: File.h:34
static float64_t cos(float64_t x)
sinh(x), x being a complex128_t
Definition: Math.h:856
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
int machine_int_t
Definition: common.h:59
The class Features is the base class of all feature objects.
Definition: Features.h:68
#define SG_SERROR(...)
Definition: SGIO.h:179
This class implements the random fourier features for the DotFeatures framework. Basically upon the o...
#define SG_ADD(...)
Definition: SGObject.h:81
static float32_t sqrt(float32_t x)
Definition: Math.h:459
virtual float64_t post_dot(float64_t dot_result, index_t par_idx)
static const float64_t PI
Definition: Math.h:2055

SHOGUN Machine Learning Toolbox - Documentation