SHOGUN  4.0.0
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 #include <shogun/lib/config.h>
39 
40 #ifdef HAVE_EIGEN3
41 
44 
45 using namespace shogun;
46 
49 {
50 }
51 
53  CInferenceMethod* method) : CGaussianProcessMachine(method)
54 {
55  // set labels
56  m_labels=method->get_labels();
57 }
58 
60 {
61 }
62 
64 {
65  // check whether given combination of inference method and likelihood
66  // function supports classification
67  REQUIRE(m_method, "Inference method should not be NULL\n")
69  REQUIRE(m_method->supports_multiclass(), "%s with %s doesn't support "
70  "multi classification\n", m_method->get_name(), lik->get_name())
71  SG_UNREF(lik);
72 
73  // if regression data equals to NULL, then apply classification on training
74  // features
75  if (!data)
76  data=m_method->get_features();
77  else
78  SG_REF(data);
79 
80  const index_t n=data->get_num_vectors();
82  const index_t C=mean.vlen/n;
83  SGVector<index_t> lab(n);
84  for (index_t idx=0; idx<n; idx++)
85  {
86  int32_t cate=CMath::arg_max(mean.vector+idx*C, 1, C);
87  lab[idx]=cate;
88  }
90  result->set_int_labels(lab);
91 
92  SG_UNREF(data);
93 
94  return result;
95 }
96 
98  CFeatures* data)
99 {
100  // check whether given combination of inference method and likelihood
101  // function supports classification
102  REQUIRE(m_method, "Inference method should not be NULL\n")
104  REQUIRE(m_method->supports_binary(), "%s with %s doesn't support "
105  "binary classification\n", m_method->get_name(), lik->get_name())
106  SG_UNREF(lik);
107 
108  // if regression data equals to NULL, then apply classification on training
109  // features
110  if (!data)
111  data=m_method->get_features();
112  else
113  SG_REF(data);
114 
115  CBinaryLabels* result=new CBinaryLabels(get_mean_vector(data));
116  SG_UNREF(data);
117 
118  return result;
119 }
120 
122 {
123  // check whether given combination of inference method and likelihood
124  // function supports classification
125  REQUIRE(m_method, "Inference method should not be NULL\n")
127  REQUIRE(m_method->supports_binary() || m_method->supports_multiclass(), "%s with %s doesn't support "
128  "classification\n", m_method->get_name(), lik->get_name())
129  SG_UNREF(lik);
130 
131  if (data)
132  m_method->set_features(data);
133 
134  // perform inference
135  m_method->update();
136 
137  return true;
138 }
139 
141  CFeatures* data)
142 {
143  // check whether given combination of inference method and likelihood
144  // function supports classification
145  REQUIRE(m_method, "Inference method should not be NULL\n")
148  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
149 
150  SG_REF(data);
153  SG_UNREF(data);
154 
155  // evaluate mean
156  mu=lik->get_predictive_means(mu, s2);
157  SG_UNREF(lik);
158 
159  return mu;
160 }
161 
163  CFeatures* data)
164 {
165  // check whether given combination of inference method and
166  // likelihood function supports classification
167  REQUIRE(m_method, "Inference method should not be NULL\n")
170  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
171 
172  SG_REF(data);
175  SG_UNREF(data);
176 
177  // evaluate variance
178  s2=lik->get_predictive_variances(mu, s2);
179  SG_UNREF(lik);
180 
181  return s2;
182 }
183 
185  CFeatures* data)
186 {
187  // check whether given combination of inference method and likelihood
188  // function supports classification
189  REQUIRE(m_method, "Inference method should not be NULL\n")
192  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
193 
194  SG_REF(data);
197  SG_UNREF(data);
198 
199  // evaluate log probabilities
201  SG_UNREF(lik);
202 
203  // evaluate probabilities
204  for (index_t idx=0; idx<p.vlen; idx++)
205  p[idx]=CMath::exp(p[idx]);
206 
207  return p;
208 }
209 
210 #endif /* HAVE_EIGEN3 */
virtual const char * get_name() const =0
virtual CFeatures * get_features()
SGVector< float64_t > get_variance_vector(CFeatures *data)
void set_int_labels(SGVector< int32_t > labels)
The Inference Method base class.
static int32_t arg_max(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
Definition: Math.h:262
virtual void set_features(CFeatures *feat)
int32_t index_t
Definition: common.h:62
A base class for Gaussian Processes.
SGVector< float64_t > get_posterior_variances(CFeatures *data)
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define REQUIRE(x,...)
Definition: SGIO.h:206
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
#define SG_REF(x)
Definition: SGObject.h:51
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)
virtual CBinaryLabels * apply_binary(CFeatures *data=NULL)
virtual CLabels * get_labels()
virtual bool train_machine(CFeatures *data=NULL)
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
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
virtual bool supports_multiclass() const
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 bool supports_binary() const
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
The Likelihood model base class.
CLikelihoodModel * get_model()
index_t vlen
Definition: SGVector.h:481

SHOGUN Machine Learning Toolbox - Documentation