SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
GaussianProcessClassification.cpp
浏览该文件的文档.
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 
45 
46 using namespace shogun;
47 
50 {
51 }
52 
54  CInferenceMethod* 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  {
124  data=fitc_method->get_inducing_features();
125  SG_UNREF(fitc_method);
126  }
127  else
128  data=m_method->get_features();
129  }
130  else
131  SG_REF(data);
132 
133  CBinaryLabels* result=new CBinaryLabels(get_mean_vector(data));
134  SG_UNREF(data);
135 
136  return result;
137 }
138 
140 {
141  // check whether given combination of inference method and likelihood
142  // function supports classification
143  REQUIRE(m_method, "Inference method should not be NULL\n")
145  REQUIRE(m_method->supports_binary() || m_method->supports_multiclass(), "%s with %s doesn't support "
146  "classification\n", m_method->get_name(), lik->get_name())
147  SG_UNREF(lik);
148 
149  if (data)
150  {
151  // set inducing features for FITC inference method
153  {
156  fitc_method->set_inducing_features(data);
157  SG_UNREF(fitc_method);
158  }
159  else
160  m_method->set_features(data);
161  }
162 
163  // perform inference
164  m_method->update();
165 
166  return true;
167 }
168 
170  CFeatures* data)
171 {
172  // check whether given combination of inference method and likelihood
173  // function supports classification
174  REQUIRE(m_method, "Inference method should not be NULL\n")
177  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
178 
179  SG_REF(data);
182  SG_UNREF(data);
183 
184  // evaluate mean
185  mu=lik->get_predictive_means(mu, s2);
186  SG_UNREF(lik);
187 
188  return mu;
189 }
190 
192  CFeatures* data)
193 {
194  // check whether given combination of inference method and
195  // likelihood function supports classification
196  REQUIRE(m_method, "Inference method should not be NULL\n")
199  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
200 
201  SG_REF(data);
204  SG_UNREF(data);
205 
206  // evaluate variance
207  s2=lik->get_predictive_variances(mu, s2);
208  SG_UNREF(lik);
209 
210  return s2;
211 }
212 
214  CFeatures* data)
215 {
216  // check whether given combination of inference method and likelihood
217  // function supports classification
218  REQUIRE(m_method, "Inference method should not be NULL\n")
221  "%s with %s doesn't support classification\n", m_method->get_name(), lik->get_name())
222 
223  SG_REF(data);
226  SG_UNREF(data);
227 
228  // evaluate log probabilities
230  SG_UNREF(lik);
231 
232  // evaluate probabilities
233  for (index_t idx=0; idx<p.vlen; idx++)
234  p[idx]=CMath::exp(p[idx]);
235 
236  return p;
237 }
238 
239 #endif /* HAVE_EIGEN3 */
virtual const char * get_name() const =0
virtual CFeatures * get_features()
static CSingleFITCLaplacianInferenceMethod * obtain_from_generic(CInferenceMethod *inference)
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)
virtual void set_inducing_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
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
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
The SingleFITCLaplace approximation inference method class for regression and binary Classification...
#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)
index_t vlen
Definition: SGVector.h:494
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
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
virtual bool supports_multiclass() const
virtual EInferenceType get_inference_type() 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()

SHOGUN 机器学习工具包 - 项目文档