SHOGUN  6.1.3
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 
34 
35 using namespace shogun;
36 
38 {
39  initialize_sparse_kernel();
40 }
41 void CGaussianARDSparseKernel::initialize_sparse_kernel()
42 {
43 }
44 
46 {
47 }
48 
49 using namespace Eigen;
50 
52  : CGaussianARDKernel(size)
53 {
54  initialize_sparse_kernel();
55 }
56 
58  CDotFeatures* r, int32_t size)
59  : CGaussianARDKernel(l, r, size)
60 {
61  initialize_sparse_kernel();
62 }
63 
65 {
66  if (kernel->get_kernel_type()!=K_GAUSSIANARDSPARSE)
67  {
68  SG_SERROR("Provided kernel is not of type CGaussianARDSparseKernel!\n");
69  }
70 
71  /* since an additional reference is returned */
72  SG_REF(kernel);
74 }
75 
77  const TParameter* param, index_t index)
78 {
79  REQUIRE(param, "Param not set\n");
80  if (!strcmp(param->m_name, "inducing_features"))
81  return CKernel::get_parameter_gradient_diagonal(param, index);
82  else
84 }
85 
87  const TParameter* param, index_t index)
88 {
89  REQUIRE(param, "Param not set\n");
90  if (!strcmp(param->m_name, "inducing_features"))
91  {
92  REQUIRE(lhs, "Left features not set!\n");
93  REQUIRE(rhs, "Right features not set!\n");
94  REQUIRE(index>=0 && index<num_lhs,"Index (%d) is out of bound (%d)\n",
95  index, num_rhs);
96  int32_t idx_l=index;
97  //Note that CDotKernel requires lhs and rhs are CDotFeatures pointers
98  //This Kernel is a subclass of CDotKernel
100  SGMatrix<float64_t> res(left_vec.vlen, num_rhs);
101 
103 
104  for (int32_t idx_r=0; idx_r<num_rhs; idx_r++)
105  {
106  SGVector<float64_t> right_vec=get_feature_vector(idx_r, rhs);
107  Map<VectorXd> eigen_res_col_vec(res.get_column_vector(idx_r),left_vec.vlen);
108 
109  SGVector<float64_t> vec=linalg::add(left_vec, right_vec, 1.0, -1.0);
110  float64_t scalar_weight=1.0;
111  //column vector
112  SGMatrix<float64_t> right=compute_right_product(vec, scalar_weight);
113  Map<VectorXd> eigen_right_col_vec(right.matrix,right.num_rows);
114 
115  if (m_ARD_type==KT_SCALAR)
116  {
117  scalar_weight*=m_weights_raw[0];
118  eigen_res_col_vec=eigen_right_col_vec*scalar_weight;
119  }
120  else
121  {
122  if(m_ARD_type==KT_DIAG)
123  {
125  eigen_res_col_vec=eigen_right_col_vec.cwiseProduct(eigen_weights);
126  }
127  else if(m_ARD_type==KT_FULL)
128  {
130  eigen_res_col_vec=eigen_weights*eigen_right_col_vec;
131  }
132  else
133  {
134  SG_ERROR("Unsupported ARD type\n");
135  }
136 
137  }
138  for (index_t i=0; i<left_vec.vlen; i++)
139  res(i,idx_r)*=-kernel(idx_l,idx_r);
140  }
141  return res;
142  }
143  else
144  {
145  return CGaussianARDKernel::get_parameter_gradient(param, index);
146  }
147 }
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
SGVector< float64_t > m_log_weights
Gaussian Kernel with Automatic Relevance Detection with supporting Sparse inference.
int32_t index_t
Definition: common.h:72
int32_t num_rhs
number of feature vectors on right hand side
virtual SGVector< float64_t > get_parameter_gradient_diagonal(const TParameter *param, index_t index=-1)
virtual SGMatrix< float64_t > compute_right_product(SGVector< float64_t >vec, float64_t &scalar_weight)
Definition: SGMatrix.h:25
parameter struct
void add(SGVector< T > &a, SGVector< T > &b, SGVector< T > &result, T alpha=1, T beta=1)
#define SG_ERROR(...)
Definition: SGIO.h:128
#define REQUIRE(x,...)
Definition: SGIO.h:181
float64_t kernel(int32_t idx_a, int32_t idx_b)
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:52
Gaussian Kernel with Automatic Relevance Detection computed on CDotFeatures.
SGMatrix< float64_t > m_weights_raw
double float64_t
Definition: common.h:60
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
virtual SGVector< float64_t > get_parameter_gradient_diagonal(const TParameter *param, index_t index=-1)
virtual SGVector< float64_t > get_feature_vector(int32_t idx, CFeatures *hs)
index_t num_rows
Definition: SGMatrix.h:495
index_t num_cols
Definition: SGMatrix.h:497
int32_t num_lhs
number of feature vectors on left hand side
static CGaussianARDSparseKernel * obtain_from_generic(CKernel *kernel)
CFeatures * rhs
feature vectors to occur on right hand side
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
#define SG_SERROR(...)
Definition: SGIO.h:164
The Kernel base class.
virtual SGVector< float64_t > get_parameter_gradient_diagonal(const TParameter *param, index_t index=-1)
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:144
index_t vlen
Definition: SGVector.h:571

SHOGUN Machine Learning Toolbox - Documentation