SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
PeriodicKernel.cpp
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Esben Soerig
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
32 
33 using namespace shogun;
34 
36 {
37  init();
38 }
39 
41 {
42  init();
43  set_length_scale(ls);
44  set_period(p);
45 }
46 
48  float64_t p, int32_t s) : CDotKernel(s)
49 {
50  init();
51  set_length_scale(ls);
52  set_period(p);
53  init(l,r);
54 }
55 
56 void CPeriodicKernel::precompute_squared_helper(SGVector<float64_t>& buf,
57  CDotFeatures* df)
58 {
59  int32_t num_vec=df->get_num_vectors();
60  buf=SGVector<float64_t>(num_vec);
61 
62  for (int32_t i=0; i<num_vec; i++)
63  buf[i]=df->dot(i,df, i);
64 }
65 
66 bool CPeriodicKernel::init(CFeatures* l, CFeatures* r)
67 {
68  CDotKernel::init(l, r);
69  precompute_squared();
70  return init_normalizer();
71 }
72 
73 float64_t CPeriodicKernel::compute(int32_t idx_a, int32_t idx_b)
74 {
75  /* Periodic kernel defined as by David Duvenaud in
76  http://mlg.eng.cam.ac.uk/duvenaud/cookbook/index.html
77 
78  k({\bf x},{\bf x'})= exp(-\frac{2sin^2(|{\bf x}-{\bf x'}|/p)}{l^2})
79  */
80  float64_t sin_term=CMath::sin(M_PI*distance(idx_a,idx_b)/m_period);
81  return CMath::exp(-2*CMath::pow(sin_term, 2)/CMath::pow(m_length_scale,2));
82 }
83 
84 void CPeriodicKernel::precompute_squared()
85 {
86  if (!lhs || !rhs)
87  return;
88 
89  CDotFeatures* dotlhs=dynamic_cast<CDotFeatures*>(lhs);
90  REQUIRE(dotlhs!=NULL, "Left-hand-side features must be of type CDotFeatures\n")
91 
92  precompute_squared_helper(m_sq_lhs, dotlhs);
93 
94  if (lhs==rhs)
95  m_sq_rhs=m_sq_lhs;
96  else
97  {
98  CDotFeatures *dotrhs=dynamic_cast<CDotFeatures*>(rhs);
99  REQUIRE(dotrhs!=NULL, "Left-hand-side features must be of type CDotFeatures\n")
100 
101  precompute_squared_helper(m_sq_rhs, dotrhs);
102  }
103 }
104 
106  const TParameter* param, index_t index)
107 {
108  REQUIRE(lhs, "Left-hand-side features not set!\n")
109  REQUIRE(rhs, "Right-hand-side features not set!\n")
110 
111  if (!strcmp(param->m_name, "length_scale"))
112  {
114 
115  for (int j=0; j<num_lhs; j++)
116  {
117  for (int k=0; k<num_rhs; k++)
118  {
119  float64_t dist=distance(j,k);
120  float64_t trig_arg=M_PI*dist/m_period;
121  float64_t original=compute(j, k);
122 
123  derivative(j,k)=original*4.0*pow(sin(trig_arg),2)/pow(m_length_scale,3);
124  }
125  }
126 
127  return derivative;
128  }
129  else if (!strcmp(param->m_name, "period"))
130  {
132 
133  for (int j=0; j<num_lhs; j++)
134  {
135  for (int k=0; k<num_rhs; k++)
136  {
137  float64_t dist=distance(j,k);
138  float64_t trig_arg=M_PI*dist/m_period;
139  float64_t original=compute(j, k);
140 
141  derivative(j,k)=original*4.0*M_PI*dist*cos(trig_arg)*sin(trig_arg)/pow(m_period*m_length_scale,2);
142  }
143  }
144  return derivative;
145  }
146  else
147  {
148  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
149  return SGMatrix<float64_t>();
150  }
151 }
152 
153 void CPeriodicKernel::init()
154 {
155  set_length_scale(1.0);
156  set_period(1.0);
157 
158  SG_ADD(&m_length_scale, "length_scale",
159  "Kernel length scale", MS_AVAILABLE, GRADIENT_AVAILABLE);
160  SG_ADD(&m_period, "period",
161  "Kernel period", MS_AVAILABLE, GRADIENT_AVAILABLE);
162  SG_ADD(&m_sq_lhs, "sq_lhs",
163  "Vector of dot products of each left-hand-side vector with itself.", MS_NOT_AVAILABLE);
164  SG_ADD(&m_sq_rhs, "sq_rhs",
165  "Vector of dot products of each right-hand-side vector with itself.", MS_NOT_AVAILABLE);
166 }
167 
168 float64_t CPeriodicKernel::distance(int32_t idx_a, int32_t idx_b)
169 {
170  return sqrt(m_sq_lhs[idx_a]+m_sq_rhs[idx_b]-2*CDotKernel::compute(idx_a,idx_b));
171 }
static float64_t sin(float64_t x)
tanh(x), x being a complex128_t
Definition: Math.h:820
SGVector< float64_t > m_sq_lhs
virtual bool init(CFeatures *l, CFeatures *r)
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1069
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)=0
parameter struct
virtual int32_t get_num_vectors() const =0
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
The periodic kernel as described in The Kernel Cookbook by David Duvenaud: http://people.seas.harvard.edu/~dduvenaud/cookbook/.
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Definition: DotKernel.h:134
Features that support dot products among other operations.
Definition: DotFeatures.h:44
shogun matrix
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:31
double float64_t
Definition: common.h:50
#define M_PI
workaround for log2 being a define on cygwin
Definition: Math.h:59
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:1067
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
virtual bool init_normalizer()
Definition: Kernel.cpp:168
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void set_period(float64_t period)
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
The class Features is the base class of all feature objects.
Definition: Features.h:68
static float64_t exp(float64_t x)
Definition: Math.h:621
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
SGVector< float64_t > m_sq_rhs
virtual void set_length_scale(float64_t length_scale)
#define SG_ADD(...)
Definition: SGObject.h:81
static int32_t pow(bool x, int32_t n)
Definition: Math.h:535

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