SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2013 Roman Votyakov 00008 */ 00009 00010 #include <shogun/lib/config.h> 00011 00012 #ifdef HAVE_EIGEN3 00013 00014 #include <shogun/classifier/GaussianProcessBinaryClassification.h> 00015 00016 using namespace shogun; 00017 00018 CGaussianProcessBinaryClassification::CGaussianProcessBinaryClassification() 00019 : CGaussianProcessMachine() 00020 { 00021 } 00022 00023 CGaussianProcessBinaryClassification::CGaussianProcessBinaryClassification( 00024 CInferenceMethod* method) : CGaussianProcessMachine(method) 00025 { 00026 // set labels 00027 m_labels=method->get_labels(); 00028 } 00029 00030 CGaussianProcessBinaryClassification::~CGaussianProcessBinaryClassification() 00031 { 00032 } 00033 00034 CBinaryLabels* CGaussianProcessBinaryClassification::apply_binary( 00035 CFeatures* data) 00036 { 00037 // check whether given combination of inference method and likelihood 00038 // function supports classification 00039 REQUIRE(m_method, "Inference method should not be NULL\n") 00040 CLikelihoodModel* lik=m_method->get_model(); 00041 REQUIRE(m_method->supports_binary(), "%s with %s doesn't support " 00042 "binary classification\n", m_method->get_name(), lik->get_name()) 00043 SG_UNREF(lik); 00044 00045 // if regression data equals to NULL, then apply classification on training 00046 // features 00047 if (!data) 00048 data=m_method->get_features(); 00049 else 00050 SG_REF(data); 00051 00052 CBinaryLabels* result=new CBinaryLabels(get_mean_vector(data)); 00053 SG_UNREF(data); 00054 00055 return result; 00056 } 00057 00058 bool CGaussianProcessBinaryClassification::train_machine(CFeatures* data) 00059 { 00060 // check whether given combination of inference method and likelihood 00061 // function supports classification 00062 REQUIRE(m_method, "Inference method should not be NULL\n") 00063 CLikelihoodModel* lik=m_method->get_model(); 00064 REQUIRE(m_method->supports_binary(), "%s with %s doesn't support " 00065 "binary classification\n", m_method->get_name(), lik->get_name()) 00066 SG_UNREF(lik); 00067 00068 if (data) 00069 m_method->set_features(data); 00070 00071 // perform inference 00072 if (m_method->update_parameter_hash()) 00073 m_method->update(); 00074 00075 return true; 00076 } 00077 00078 SGVector<float64_t> CGaussianProcessBinaryClassification::get_mean_vector( 00079 CFeatures* data) 00080 { 00081 // check whether given combination of inference method and likelihood 00082 // function supports classification 00083 REQUIRE(m_method, "Inference method should not be NULL\n") 00084 CLikelihoodModel* lik=m_method->get_model(); 00085 REQUIRE(m_method->supports_binary(), "%s with %s doesn't support " 00086 "binary classification\n", m_method->get_name(), lik->get_name()) 00087 00088 SG_REF(data); 00089 SGVector<float64_t> mu=get_posterior_means(data); 00090 SGVector<float64_t> s2=get_posterior_variances(data); 00091 SG_UNREF(data); 00092 00093 // evaluate mean 00094 mu=lik->get_predictive_means(mu, s2); 00095 SG_UNREF(lik); 00096 00097 return mu; 00098 } 00099 00100 SGVector<float64_t> CGaussianProcessBinaryClassification::get_variance_vector( 00101 CFeatures* data) 00102 { 00103 // check whether given combination of inference method and 00104 // likelihood function supports classification 00105 REQUIRE(m_method, "Inference method should not be NULL\n") 00106 CLikelihoodModel* lik=m_method->get_model(); 00107 REQUIRE(m_method->supports_binary(), "%s with %s doesn't support " 00108 "binary classification\n", m_method->get_name(), lik->get_name()) 00109 00110 SG_REF(data); 00111 SGVector<float64_t> mu=get_posterior_means(data); 00112 SGVector<float64_t> s2=get_posterior_variances(data); 00113 SG_UNREF(data); 00114 00115 // evaluate variance 00116 s2=lik->get_predictive_variances(mu, s2); 00117 SG_UNREF(lik); 00118 00119 return s2; 00120 } 00121 00122 SGVector<float64_t> CGaussianProcessBinaryClassification::get_probabilities( 00123 CFeatures* data) 00124 { 00125 // check whether given combination of inference method and likelihood 00126 // function supports classification 00127 REQUIRE(m_method, "Inference method should not be NULL\n") 00128 CLikelihoodModel* lik=m_method->get_model(); 00129 REQUIRE(m_method->supports_binary(), "%s with %s doesn't support " 00130 "binary classification\n", m_method->get_name(), lik->get_name()) 00131 00132 SG_REF(data); 00133 SGVector<float64_t> mu=get_posterior_means(data); 00134 SGVector<float64_t> s2=get_posterior_variances(data); 00135 SG_UNREF(data); 00136 00137 // evaluate log probabilities 00138 SGVector<float64_t> p=lik->get_predictive_log_probabilities(mu, s2); 00139 SG_UNREF(lik); 00140 00141 // evaluate probabilities 00142 p.exp(); 00143 00144 return p; 00145 } 00146 00147 #endif /* HAVE_EIGEN3 */