SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
features
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
>
12
#include <
shogun/mathematics/Math.h
>
13
#include <
shogun/features/RandomFourierDotFeatures.h
>
14
15
namespace
shogun {
16
17
enum
KernelName
;
18
19
CRandomFourierDotFeatures::CRandomFourierDotFeatures
()
20
{
21
init(
NOT_SPECIFIED
,
SGVector<float64_t>
());
22
}
23
24
CRandomFourierDotFeatures::CRandomFourierDotFeatures
(
CDotFeatures
* features,
25
int32_t D,
KernelName
kernel_name,
SGVector<float64_t>
params)
26
:
CRandomKitchenSinksDotFeatures
(features, D)
27
{
28
init(kernel_name, params);
29
random_coeff
=
generate_random_coefficients
();
30
}
31
32
CRandomFourierDotFeatures::CRandomFourierDotFeatures
(
CDotFeatures
* features,
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
40
CRandomFourierDotFeatures::CRandomFourierDotFeatures
(
CFile
* loader)
41
{
42
SG_NOTIMPLEMENTED
;
43
}
44
45
CRandomFourierDotFeatures::CRandomFourierDotFeatures
(
const
CRandomFourierDotFeatures
& orig)
46
:
CRandomKitchenSinksDotFeatures
(orig)
47
{
48
init(orig.kernel, orig.kernel_params);
49
}
50
51
CRandomFourierDotFeatures::~CRandomFourierDotFeatures
()
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"
,
66
MS_NOT_AVAILABLE
);
67
}
68
69
CFeatures
*
CRandomFourierDotFeatures::duplicate
()
const
70
{
71
return
new
CRandomFourierDotFeatures
(*
this
);
72
}
73
74
const
char
*
CRandomFourierDotFeatures::get_name
()
const
75
{
76
return
"RandomFourierDotFeatures"
;
77
}
78
79
float64_t
CRandomFourierDotFeatures::post_dot
(
float64_t
dot_result,
index_t
par_idx)
80
{
81
dot_result +=
random_coeff
(
random_coeff
.
num_rows
-1, par_idx);
82
return
CMath::cos
(dot_result) * constant;
83
}
84
85
SGVector<float64_t>
CRandomFourierDotFeatures::generate_random_parameter_vector
()
86
{
87
SGVector<float64_t>
vec(
feats
->
get_dim_feature_space
()+1);
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