SHOGUN  6.1.3
SparseInference.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (W) 2015 Wu Lin
4  * Written (W) 2013 Roman Votyakov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  *
31  */
33 
34 
39 
40 using namespace shogun;
41 using namespace Eigen;
42 
44 {
45  init();
46 }
47 
49 {
50  REQUIRE(m_features, "Input features not set\n")
51 }
52 
54 {
55  CDotFeatures *feat_type=dynamic_cast<CDotFeatures *>(m_features);
56 
59  CDotFeatures *lat_type=new CDenseFeatures<float64_t>(lat_m);
60 
61  REQUIRE(feat_type, "Input features (%s) must be DotFeatures"
62  " or one of its subclasses\n", m_features->get_name())
63  REQUIRE(feat_type->get_dim_feature_space()==lat_type->get_dim_feature_space(),
64  "The dim of feature spaces between"
65  " input features (%d) and inducing features (%d) must be same\n",
66  feat_type->get_dim_feature_space(),
67  lat_type->get_dim_feature_space())
68  if((m_features->get_feature_class()!=lat_type->get_feature_class())||
70  {
72  {
73  SG_WARNING("Input features (%s) and inducing features (%s) are"
74  " difference classes\n", m_features->get_name(),
75  lat_type->get_name());
76  }
77  if(m_features->get_feature_type()!=lat_type->get_feature_type())
78  {
79  SG_WARNING("Input features and inducing features are difference types\n");
80  }
81  SG_WARNING("Input features may be deleted\n");
86  }
87  SG_UNREF(lat_type);
88 }
89 
91  CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat)
92  : CInference(kern, feat, m, lab, mod)
93 {
94  init();
96 }
97 
98 void CSparseInference::init()
99 {
100  SG_ADD(&m_inducing_features, "inducing_features", "inducing features",
102  SG_ADD(&m_log_ind_noise, "log_inducing_noise", "noise about inducing potins in log domain",
104  SG_ADD(&m_mu, "mu", "mean vector of the approximation to the posterior", MS_NOT_AVAILABLE);
105  SG_ADD(&m_Sigma, "Sigma", "covariance matrix of the approximation to the posterior", MS_NOT_AVAILABLE);
106  SG_ADD(&m_ktrtr_diag, "ktrtr_diag", "diagonal elements of kernel matrix m_ktrtr", MS_NOT_AVAILABLE);
107 
110 }
111 
113 {
114  REQUIRE(noise>0, "Noise (%f) for inducing points must be postive",noise);
116 }
117 
119 {
120  return CMath::exp(m_log_ind_noise);
121 }
122 
124 {
125 }
126 
128 {
130 
131  REQUIRE(m_inducing_features.num_rows, "Inducing features should not be empty\n")
132  REQUIRE(m_inducing_features.num_cols, "Inducing features should not be empty\n")
133 }
134 
136 {
138  update();
139 
141  return result;
142 }
143 
145 {
147  update();
148 
149  SGMatrix<float64_t> result(m_L);
150  return result;
151 }
152 
154 {
155  //time complexity can be O(m*n) if the TO DO is done
156  check_features();
158 
161 
162  CFeatures* inducing_features=get_inducing_features();
163 
164  // create kernel matrix for inducing features
165  m_kernel->init(inducing_features, inducing_features);
167 
168  // create kernel matrix for inducing and training features
169  m_kernel->init(inducing_features, m_features);
171 
172  SG_UNREF(inducing_features);
173 }
174 
virtual const char * get_name() const =0
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:97
virtual void set_inducing_features(CFeatures *feat)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
CKernel * m_kernel
Definition: Inference.h:464
SGVector< float64_t > m_mu
Definition: SGMatrix.h:25
virtual void set_inducing_noise(float64_t noise)
#define REQUIRE(x,...)
Definition: SGIO.h:181
An abstract class of the mean function.
Definition: MeanFunction.h:49
Features that support dot products among other operations.
Definition: DotFeatures.h:44
SGMatrix< float64_t > get_kernel_matrix()
#define SG_REF(x)
Definition: SGObject.h:52
virtual void check_members() const
CFeatures * m_features
Definition: Inference.h:473
virtual int32_t get_dim_feature_space() const =0
virtual void update_train_kernel()
virtual SGMatrix< float64_t > get_cholesky()
double float64_t
Definition: common.h:60
SGMatrix< float64_t > m_Sigma
virtual CFeatures * get_inducing_features()
virtual void convert_features()
index_t num_rows
Definition: SGMatrix.h:495
SGMatrix< float64_t > m_kuu
SGMatrix< float64_t > m_L
Definition: Inference.h:482
virtual EFeatureClass get_feature_class() const =0
index_t num_cols
Definition: SGMatrix.h:497
virtual SGVector< float64_t > get_alpha()
SGVector< float64_t > m_ktrtr_diag
#define SG_UNREF(x)
Definition: SGObject.h:53
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The Inference Method base class.
Definition: Inference.h:81
SGMatrix< float64_t > m_inducing_features
The class Features is the base class of all feature objects.
Definition: Features.h:69
static float64_t exp(float64_t x)
Definition: Math.h:551
SGVector< float64_t > get_kernel_diagonal(SGVector< float64_t > preallocated=SGVector< float64_t >())
static float64_t log(float64_t v)
Definition: Math.h:714
virtual float64_t get_inducing_noise()
The Kernel base class.
SGMatrix< float64_t > m_ktru
virtual void update()=0
#define SG_WARNING(...)
Definition: SGIO.h:127
#define SG_ADD(...)
Definition: SGObject.h:93
SGMatrix< float64_t > get_computed_dot_feature_matrix()
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:296
The Likelihood model base class.
virtual EFeatureType get_feature_type() const =0
SGVector< float64_t > m_alpha
Definition: Inference.h:479
virtual void check_members() const
Definition: Inference.cpp:249

SHOGUN Machine Learning Toolbox - Documentation