SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 }

SHOGUN Machine Learning Toolbox - Documentation