SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GaussianARDSparseKernel.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 #ifdef HAVE_LINALG_LIB
36 #endif
37 
38 using namespace shogun;
39 
41 {
42  initialize_sparse_kernel();
43 }
44 void CGaussianARDSparseKernel::initialize_sparse_kernel()
45 {
46 }
47 
49 {
50 }
51 
52 #if defined(HAVE_EIGEN3) && defined(HAVE_LINALG_LIB)
53 using namespace Eigen;
54 
56  : CGaussianARDKernel(size)
57 {
58  initialize_sparse_kernel();
59 }
60 
62  CDotFeatures* r, int32_t size)
63  : CGaussianARDKernel(l, r, size)
64 {
65  initialize_sparse_kernel();
66 }
67 
69 {
70  if (kernel->get_kernel_type()!=K_GAUSSIANARDSPARSE)
71  {
72  SG_SERROR("Provided kernel is not of type CGaussianARDSparseKernel!\n");
73  }
74 
75  /* since an additional reference is returned */
76  SG_REF(kernel);
78 }
79 
81  const TParameter* param, index_t index)
82 {
83  REQUIRE(param, "Param not set\n");
84  if (!strcmp(param->m_name, "inducing_features"))
85  return CKernel::get_parameter_gradient_diagonal(param, index);
86  else
88 }
89 
91  const TParameter* param, index_t index)
92 {
93  REQUIRE(param, "Param not set\n");
94  if (!strcmp(param->m_name, "inducing_features"))
95  {
96  REQUIRE(lhs, "Left features not set!\n");
97  REQUIRE(rhs, "Right features not set!\n");
98  REQUIRE(index>=0 && index<num_lhs,"Index (%d) is out of bound (%d)\n",
99  index, num_rhs);
100  int32_t idx_l=index;
101  //Note that CDotKernel requires lhs and rhs are CDotFeatures pointers
102  //This Kernel is a subclass of CDotKernel
103  SGVector<float64_t> left_vec=get_feature_vector(idx_l, lhs);
104  SGMatrix<float64_t> res(left_vec.vlen, num_rhs);
105 
106  lazy_update_weights();
107 
108  for (int32_t idx_r=0; idx_r<num_rhs; idx_r++)
109  {
110  SGVector<float64_t> right_vec=get_feature_vector(idx_r, rhs);
111  Map<VectorXd> eigen_res_col_vec(res.get_column_vector(idx_r),left_vec.vlen);
112 
113  SGVector<float64_t> vec=linalg::add(left_vec, right_vec, 1.0, -1.0);
114  float64_t scalar_weight=1.0;
115  //column vector
116  SGMatrix<float64_t> right=compute_right_product(vec, scalar_weight);
117  Map<VectorXd> eigen_right_col_vec(right.matrix,right.num_rows);
118 
119  if (m_ARD_type==KT_SCALAR)
120  {
121  scalar_weight*=m_weights_raw[0];
122  eigen_res_col_vec=eigen_right_col_vec*scalar_weight;
123  }
124  else
125  {
126  if(m_ARD_type==KT_DIAG)
127  {
129  eigen_res_col_vec=eigen_right_col_vec.cwiseProduct(eigen_weights);
130  }
131  else if(m_ARD_type==KT_FULL)
132  {
134  eigen_res_col_vec=eigen_weights*eigen_right_col_vec;
135  }
136  else
137  {
138  SG_ERROR("Unsupported ARD type\n");
139  }
140 
141  }
142  for (index_t i=0; i<left_vec.vlen; i++)
143  res(i,idx_r)*=-kernel(idx_l,idx_r);
144  }
145  return res;
146  }
147  else
148  {
149  return CGaussianARDKernel::get_parameter_gradient(param, index);
150  }
151 }
152 #endif /* HAVE_LINALG_LIB */
153 
SGVector< float64_t > m_log_weights
Gaussian Kernel with Automatic Relevance Detection with supporting Sparse inference.
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1069
Definition: SGMatrix.h:20
parameter struct
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:206
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:51
index_t num_rows
Definition: SGMatrix.h:376
Gaussian Kernel with Automatic Relevance Detection computed on CDotFeatures.
SGMatrix< float64_t > m_weights_raw
index_t vlen
Definition: SGVector.h:494
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
Definition: Core.h:65
double float64_t
Definition: common.h:50
virtual SGVector< float64_t > get_feature_vector(int32_t idx, CFeatures *hs)
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:1067
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
static CKernel * obtain_from_generic(CSGObject *kernel)
Definition: Kernel.cpp:896
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual EKernelType get_kernel_type()=0
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
#define SG_SERROR(...)
Definition: SGIO.h:179
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
Definition: Kernel.h:850
The Kernel base class.
Definition: Kernel.h:158
virtual SGVector< float64_t > get_parameter_gradient_diagonal(const TParameter *param, index_t index=-1)
Definition: Kernel.h:864

SHOGUN Machine Learning Toolbox - Documentation