SHOGUN  6.1.3
GaussianProcessClassification.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 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  * Code adapted from
32  * Gaussian Process Machine Learning Toolbox
33  * http://www.gaussianprocess.org/gpml/code/matlab/doc/
34  * and
35  * https://gist.github.com/yorkerlin/8a36e8f9b298aa0246a4
36  */
37 
38 
39 #include <shogun/lib/config.h>
42 #ifdef USE_GPL_SHOGUN
44 #endif //USE_GPL_SHOGUN
45 
46 using namespace shogun;
47 
50 {
51 }
52 
54  CInference* method) : CGaussianProcessMachine(method)
55 {
56  // set labels
57  m_labels=method->get_labels();
58 }
59 
61 {
62 }
63 
65 {
66  // check whether given combination of inference method and likelihood
67  // function supports classification
68  REQUIRE(m_method, "Inference method should not be NULL\n")
70  REQUIRE(m_method->supports_multiclass(), "%s with %s doesn't support "
71  "multi classification\n", m_method->get_name(), lik->get_name())
72  SG_UNREF(lik);
73 
74  // if regression data equals to NULL, then apply classification on training
75  // features
76  if (!data)
77  {
79  {
81  }
82  else
83  data=m_method->get_features();
84  }
85  else
86  SG_REF(data);
87 
88  const index_t n=data->get_num_vectors();
90  const index_t C=mean.vlen/n;
91  SGVector<index_t> lab(n);
92  for (index_t idx=0; idx<n; idx++)
93  {
94  int32_t cate=CMath::arg_max(mean.vector+idx*C, 1, C);
95  lab[idx]=cate;
96  }
98  result->set_int_labels(lab);
99 
100  SG_UNREF(data);
101 
102  return result;
103 }
104 
106  CFeatures* data)
107 {
108  // check whether given combination of inference method and likelihood
109  // function supports classification
110  REQUIRE(m_method, "Inference method should not be NULL\n")
112  REQUIRE(m_method->supports_binary(), "%s with %s doesn't support "
113  "binary classification\n", m_method->get_name(), lik->get_name())
114  SG_UNREF(lik);
115 
116  // if regression data equals to NULL, then apply classification on training
117  // features
118  if (!data)
119  {
121  {
122 #ifdef USE_GPL_SHOGUN
125  data=fitc_method->get_inducing_features();
126  SG_UNREF(fitc_method);
127 #else
129 #endif //USE_GPL_SHOGUN
130  }
131  else
132  data=m_method->get_features();
133  }
134  else
135  SG_REF(data);
136 
137  CBinaryLabels* result=new CBinaryLabels(get_mean_vector(data));
138  SG_UNREF(data);
139 
140  return result;
141 }
142 
144 {
145  // check whether given combination of inference method and likelihood
146  // function supports classification
147  REQUIRE(m_method, "Inference method should not be NULL\n")
149  REQUIRE(m_method->supports_binary() || m_method->supports_multiclass(), "%s with %s doesn't support "
150  "classification\n", m_method->get_name(), lik->get_name())
151  SG_UNREF(lik);
152 
153  if (data)
154  {
155  // set inducing features for FITC inference method
157  {
158 #ifdef USE_GPL_SHOGUN
161  fitc_method->set_inducing_features(data);
162  SG_UNREF(fitc_method);
163 #else
164  SG_ERROR("Single FITC Laplace inference only supported under GPL.\n")
165 #endif //USE_GPL_SHOGUN
166  }
167  else
168  m_method->set_features(data);
169  }
170 
171  // perform inference
172  m_method->update();
173 
174  return true;
175 }
176 
178  CFeatures* data)
179 {
180  // check whether given combination of inference method and likelihood
181  // function supports classification
182  REQUIRE(m_method, "Inference method should not be NULL\n")
185  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
186 
187  SG_REF(data);
190  SG_UNREF(data);
191 
192  // evaluate mean
193  mu=lik->get_predictive_means(mu, s2);
194  SG_UNREF(lik);
195 
196  return mu;
197 }
198 
200  CFeatures* data)
201 {
202  // check whether given combination of inference method and
203  // likelihood function supports classification
204  REQUIRE(m_method, "Inference method should not be NULL\n")
207  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
208 
209  SG_REF(data);
212  SG_UNREF(data);
213 
214  // evaluate variance
215  s2=lik->get_predictive_variances(mu, s2);
216  SG_UNREF(lik);
217 
218  return s2;
219 }
220 
222  CFeatures* data)
223 {
224  // check whether given combination of inference method and likelihood
225  // function supports classification
226  REQUIRE(m_method, "Inference method should not be NULL\n")
229  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
230 
231  SG_REF(data);
234  SG_UNREF(data);
235 
236  // evaluate log probabilities
238  SG_UNREF(lik);
239 
240  // evaluate probabilities
241  for (index_t idx=0; idx<p.vlen; idx++)
242  p[idx]=CMath::exp(p[idx]);
243 
244  return p;
245 }
virtual const char * get_name() const =0
virtual void update()
Definition: Inference.cpp:243
virtual void set_inducing_features(CFeatures *feat)
SGVector< float64_t > get_variance_vector(CFeatures *data)
void set_int_labels(SGVector< int32_t > labels)
int32_t index_t
Definition: common.h:72
A base class for Gaussian Processes.
virtual EInferenceType get_inference_type() const
Definition: Inference.h:104
SGVector< float64_t > get_posterior_variances(CFeatures *data)
virtual bool supports_binary() const
Definition: Inference.h:371
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:436
#define SG_ERROR(...)
Definition: SGIO.h:128
#define REQUIRE(x,...)
Definition: SGIO.h:181
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:138
SGVector< float64_t > get_posterior_means(CFeatures *data)
virtual SGVector< float64_t > get_predictive_variances(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab=NULL) const =0
virtual CLabels * get_labels()
Definition: Inference.h:317
virtual CFeatures * get_features()
Definition: Inference.h:266
static CSingleFITCLaplaceInferenceMethod * obtain_from_generic(CInference *inference)
std::enable_if<!std::is_same< T, complex128_t >::value, float64_t >::type mean(const Container< T > &a)
#define SG_REF(x)
Definition: SGObject.h:52
SGVector< float64_t > get_mean_vector(CFeatures *data)
Multiclass Labels for multi-class classification.
SGVector< float64_t > get_probabilities(CFeatures *data)
virtual SGVector< float64_t > get_predictive_log_probabilities(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab=NULL)
#define SG_GPL_ONLY
Definition: SGIO.h:139
virtual CBinaryLabels * apply_binary(CFeatures *data=NULL)
virtual CFeatures * get_inducing_features()
CLikelihoodModel * get_model()
Definition: Inference.h:334
virtual bool train_machine(CFeatures *data=NULL)
virtual void set_features(CFeatures *feat)
Definition: Inference.h:272
#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
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
virtual SGVector< float64_t > get_predictive_means(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab=NULL) const =0
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
The FITC approximation inference method class for regression and binary Classification. Note that the number of inducing points (m) is usually far less than the number of input points (n). (the time complexity is computed based on the assumption m < n)
virtual bool supports_multiclass() const
Definition: Inference.h:378
The Likelihood model base class.
index_t vlen
Definition: SGVector.h:571
static int32_t arg_max(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
Definition: Math.h:235

SHOGUN Machine Learning Toolbox - Documentation