SHOGUN  4.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianARDFITCKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Wu Lin
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  */
31 
33 
34 using namespace shogun;
35 
37 {
38  init();
39 }
40 void CGaussianARDFITCKernel::init()
41 {
42 }
43 
45 {
46 }
47 
48 #ifdef HAVE_CXX11
51  : CGaussianARDKernel(size,width)
52 {
53  init();
54 }
55 
57  CDotFeatures* r, int32_t size, float64_t width)
58  : CGaussianARDKernel(l, r, size, width)
59 {
60  init();
61 }
62 
64 {
65  if (kernel->get_kernel_type()!=K_GAUSSIANARDFITC)
66  {
67  SG_SERROR("Provided kernel is not of type CGaussianARDFITCKernel!\n");
68  }
69 
70  /* since an additional reference is returned */
71  SG_REF(kernel);
73 }
74 
75 #include <typeinfo>
77 {
78  // TODO: remove this after all the classes get shallow_copy properly implemented
79  // this assert is to avoid any subclass of CGaussianARDFITCKernel accidentally called
80  // with the implement here
81  ASSERT(typeid(*this) == typeid(CGaussianARDFITCKernel))
83  if (lhs)
84  {
85  ker->CGaussianARDKernel::init(lhs, rhs);
86  }
87  return ker;
88 }
89 
91  const TParameter* param, index_t index)
92 {
93  if (!strcmp(param->m_name, "latent_features"))
94  {
95  REQUIRE(lhs && rhs, "Features not set!\n")
96  REQUIRE(index>=0 && index<num_lhs,"")
97  int32_t idx_l=index;
98  SGVector<float64_t> left_vec=((CDotFeatures *)lhs)->get_computed_dot_feature_vector(idx_l);
99  SGMatrix<float64_t> res(left_vec.vlen, num_rhs);
100 
101  for (int32_t idx_r=0; idx_r<num_rhs; idx_r++)
102  {
103  SGVector<float64_t> right_vec=((CDotFeatures *)rhs)->get_computed_dot_feature_vector(idx_r);
104  SGMatrix<float64_t> res_transpose(res.get_column_vector(idx_r),1,left_vec.vlen,false);
105  linalg::add(left_vec, right_vec, right_vec, 1.0, -1.0);
106  float64_t scalar_weight=1.0;
107  //column vector
108  SGMatrix<float64_t> right=compute_right_product(right_vec, scalar_weight);
109  //row vector
110  SGMatrix<float64_t> right_transpose(right.matrix,1,right.num_rows,false);
111  if (m_ARD_type==KT_SCALAR)
112  {
113  scalar_weight*=m_weights[0];
114  SGMatrix<float64_t> weights(1,right.num_rows);
115  weights.set_const(scalar_weight);
116  linalg::matrix_product(right_transpose, weights, res_transpose);
117  }
118  else
119  {
120  if(m_ARD_type==KT_DIAG)
121  {
123  linalg::elementwise_product(right_transpose, weights, res_transpose);
124  }
125  else if(m_ARD_type==KT_FULL)
126  {
127  linalg::matrix_product(right_transpose, m_weights, res_transpose);
128  }
129  else
130  {
131  SG_ERROR("Unsupported ARD kernel\n");
132  }
133 
134  }
135 
136  for (index_t i=0; i<left_vec.vlen; i++)
137  res(i,idx_r)*=kernel(idx_l,idx_r)*-2.0/m_width;
138  }
139  return res;
140  }
141  else
142  {
143  return CGaussianARDKernel::get_parameter_gradient(param, index);
144  }
145 }
146 #endif /* HAVE_CXX11 */
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1057
virtual CSGObject * shallow_copy() const
Definition: SGObject.cpp:194
parameter struct
Definition: Parameter.h:32
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
SGMatrix< float64_t > m_weights
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:205
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:51
int32_t cache_size
cache_size in MB
Definition: Kernel.h:1035
Gaussian Kernel with Automatic Relevance Detection computed on CDotFeatures.
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
Definition: Core.h:58
double float64_t
Definition: common.h:50
index_t num_rows
Definition: SGMatrix.h:329
shogun vector
Definition: Parameter.h:28
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:1055
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1049
static CKernel * obtain_from_generic(CSGObject *kernel)
Definition: Kernel.cpp:883
virtual EKernelType get_kernel_type()=0
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1047
#define SG_SERROR(...)
Definition: SGIO.h:179
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
Definition: Kernel.h:851
Gaussian Kernel with Automatic Relevance Detection with supporting FITC inference.
The Kernel base class.
Definition: Kernel.h:157

SHOGUN Machine Learning Toolbox - Documentation