SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SparseInferenceBase.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 #ifdef HAVE_EIGEN3
35 
40 
41 using namespace shogun;
42 using namespace Eigen;
43 
45 {
46  init();
47 }
48 
50 {
51  REQUIRE(m_features, "Input features not set\n")
52 }
53 
55 {
56  CDotFeatures *feat_type=dynamic_cast<CDotFeatures *>(m_features);
57 
60  CDotFeatures *lat_type=new CDenseFeatures<float64_t>(lat_m);
61 
62  REQUIRE(feat_type, "Input features (%s) must be DotFeatures"
63  " or one of its subclasses\n", m_features->get_name())
64  REQUIRE(feat_type->get_dim_feature_space()==lat_type->get_dim_feature_space(),
65  "The dim of feature spaces between"
66  " input features (%d) and inducing features (%d) must be same\n",
67  feat_type->get_dim_feature_space(),
68  lat_type->get_dim_feature_space())
69  if((m_features->get_feature_class()!=lat_type->get_feature_class())||
71  {
73  {
74  SG_WARNING("Input features (%s) and inducing features (%s) are"
75  " difference classes\n", m_features->get_name(),
76  lat_type->get_name());
77  }
78  if(m_features->get_feature_type()!=lat_type->get_feature_type())
79  {
80  SG_WARNING("Input features and inducing features are difference types\n");
81  }
82  SG_WARNING("Input features may be deleted\n");
87  }
88  SG_UNREF(lat_type);
89 }
90 
92  CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat)
93  : CInferenceMethod(kern, feat, m, lab, mod)
94 {
95  init();
97 }
98 
99 void CSparseInferenceBase::init()
100 {
101  SG_ADD(&m_inducing_features, "inducing_features", "inducing features",
103  SG_ADD(&m_log_ind_noise, "log_inducing_noise", "noise about inducing potins in log domain",
105  SG_ADD(&m_mu, "mu", "mean vector of the approximation to the posterior", MS_NOT_AVAILABLE);
106  SG_ADD(&m_Sigma, "Sigma", "covariance matrix of the approximation to the posterior", MS_NOT_AVAILABLE);
107  SG_ADD(&m_ktrtr_diag, "ktrtr_diag", "diagonal elements of kernel matrix m_ktrtr", MS_NOT_AVAILABLE);
108 
111 }
112 
114 {
115  REQUIRE(noise>0, "Noise (%f) for inducing points must be postive",noise);
117 }
118 
120 {
121  return CMath::exp(m_log_ind_noise);
122 }
123 
125 {
126 }
127 
129 {
131 
132  REQUIRE(m_inducing_features.num_rows, "Inducing features should not be empty\n")
133  REQUIRE(m_inducing_features.num_cols, "Inducing features should not be empty\n")
134 }
135 
137 {
139  update();
140 
142  return result;
143 }
144 
146 {
148  update();
149 
150  SGMatrix<float64_t> result(m_L);
151  return result;
152 }
153 
155 {
156  //time complexity can be O(m*n) if the TO DO is done
157  check_features();
159 
162 
163  CFeatures* inducing_features=get_inducing_features();
164 
165  // create kernel matrix for inducing features
166  m_kernel->init(inducing_features, inducing_features);
168 
169  // create kernel matrix for inducing and training features
170  m_kernel->init(inducing_features, m_features);
172 
173  SG_UNREF(inducing_features);
174 }
175 
176 #endif /* HAVE_EIGEN3 */
virtual const char * get_name() const =0
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
virtual void check_members() const
SGVector< float64_t > m_ktrtr_diag
SGVector< float64_t > m_alpha
The Inference Method base class.
virtual void set_inducing_features(CFeatures *feat)
virtual float64_t get_inducing_noise()
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
Definition: SGMatrix.h:20
virtual SGMatrix< float64_t > get_cholesky()
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
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 > m_inducing_features
SGMatrix< float64_t > get_kernel_matrix()
Definition: Kernel.h:219
#define SG_REF(x)
Definition: SGObject.h:51
index_t num_rows
Definition: SGMatrix.h:376
virtual int32_t get_dim_feature_space() const =0
SGMatrix< float64_t > m_L
double float64_t
Definition: common.h:50
virtual EFeatureClass get_feature_class() const =0
virtual void set_inducing_noise(float64_t noise)
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual CFeatures * get_inducing_features()
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
SGVector< float64_t > get_kernel_diagonal(SGVector< float64_t > preallocated=SGVector< float64_t >())
Definition: Kernel.h:230
static float64_t log(float64_t v)
Definition: Math.h:922
virtual void check_members() const
The Kernel base class.
Definition: Kernel.h:158
#define SG_WARNING(...)
Definition: SGIO.h:128
#define SG_ADD(...)
Definition: SGObject.h:81
SGMatrix< float64_t > get_computed_dot_feature_matrix()
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:262
The Likelihood model base class.
virtual EFeatureType get_feature_type() const =0
virtual SGVector< float64_t > get_alpha()

SHOGUN Machine Learning Toolbox - Documentation