SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
KLApproxDiagonalInferenceMethod.cpp
浏览该文件的文档.
1  /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Wu Lin
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  *
30  * Code adapted from
31  * http://hannes.nickisch.org/code/approxXX.tar.gz
32  * and Gaussian Process Machine Learning Toolbox
33  * http://www.gaussianprocess.org/gpml/code/matlab/doc/
34  * and the reference paper is
35  * Challis, Edward, and David Barber.
36  * "Concave Gaussian variational approximations for inference in large-scale Bayesian linear models."
37  * International conference on Artificial Intelligence and Statistics. 2011.
38  *
39  * This code specifically adapted from function in approxKL.m and infKL.m
40  */
41 
43 
44 #ifdef HAVE_EIGEN3
49 
50 using namespace Eigen;
51 
52 namespace shogun
53 {
54 
55 CKLApproxDiagonalInferenceMethod::CKLApproxDiagonalInferenceMethod() : CKLLowerTriangularInferenceMethod()
56 {
57  init();
58 }
59 
61  CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod)
62  : CKLLowerTriangularInferenceMethod(kern, feat, m, lab, mod)
63 {
64  init();
65 }
66 
67 void CKLApproxDiagonalInferenceMethod::init()
68 {
69  SG_ADD(&m_InvK, "invK",
70  "The K^{-1} matrix",
72 }
73 
75  CInferenceMethod* inference)
76 {
77  if (inference==NULL)
78  return NULL;
79 
80  if (inference->get_inference_type()!=INF_KL_DIAGONAL)
81  SG_SERROR("Provided inference is not of type CKLApproxDiagonalInferenceMethod!\n")
82 
83  SG_REF(inference);
84  return (CKLApproxDiagonalInferenceMethod*)inference;
85 }
86 
88 {
97  update();
98 
99  index_t len=m_mu.vlen;
100  SGVector<float64_t> result(len);
101 
102  Map<VectorXd> eigen_result(result.vector, len);
103  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
104 
105  eigen_result=eigen_alpha;
106  return result;
107 }
108 
110 {
111 }
112 
114 {
115  index_t len=m_mean_vec.vlen;
118  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
119 
120  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
121  //mu=K*alpha+m
122  eigen_mu=eigen_K*CMath::exp(m_log_scale*2.0)*eigen_alpha+eigen_mean;
123 
124  Map<VectorXd> eigen_log_v(m_alpha.vector+len, m_alpha.vlen-len);
125  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
126  //s2=sum(C.*C,2);
127  eigen_s2=eigen_log_v.array().exp();
128 
130  bool status=lik->set_variational_distribution(m_mu, m_s2, m_labels);
131  return status;
132 }
133 
135 {
136  REQUIRE(gradient.vlen==m_alpha.vlen,
137  "The length of gradients (%d) should the same as the length of parameters (%d)\n",
138  gradient.vlen, m_alpha.vlen);
139 
141  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
142 
143  index_t len=m_mu.vlen;
144  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
145  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
146 
148  //[a,df,dV] = a_related2(mu,s2,y,lik);
149  TParameter* s2_param=lik->m_parameters->get_parameter("sigma2");
151  Map<VectorXd> eigen_dv(dv.vector, dv.vlen);
152 
153  TParameter* mu_param=lik->m_parameters->get_parameter("mu");
155  Map<VectorXd> eigen_df(df.vector, df.vlen);
156 
157  Map<VectorXd> eigen_dnlz_alpha(gradient.vector, len);
158  //dnlZ_alpha = -K*(df-alpha);
159  eigen_dnlz_alpha=eigen_K*CMath::exp(m_log_scale*2.0)*(-eigen_df+eigen_alpha);
160 
161  Map<VectorXd> eigen_dnlz_log_v(gradient.vector+len, gradient.vlen-len);
162 
163  eigen_dnlz_log_v=(eigen_InvK.diagonal().array()-(1.0/eigen_s2.array()));
164  eigen_dnlz_log_v=(0.5*eigen_dnlz_log_v.array())-eigen_dv.array();
165  eigen_dnlz_log_v=eigen_dnlz_log_v.array()*eigen_s2.array();
166 
167 }
168 
170 {
171  Map<VectorXd> eigen_alpha(m_alpha.vector, m_mu.vlen);
172  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
174  //get mean vector and create eigen representation of it
177  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
178  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
179 
182  float64_t log_det=eigen_log_v.array().sum()-m_log_det_Kernel;
183  float64_t trace=(eigen_s2.array()*eigen_InvK.diagonal().array()).sum();
184 
185  //nlZ = -a -logdet(V*inv(K))/2 -n/2 +(alpha'*K*alpha)/2 +trace(V*inv(K))/2;
186  float64_t result=-a+0.5*(-eigen_K.rows()+eigen_alpha.dot(eigen_mu-eigen_mean)+trace-log_det);
187 
188  return result;
189 }
190 
192 {
195  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
196  eigen_InvK=solve_inverse(MatrixXd::Identity(m_ktrtr.num_rows,m_ktrtr.num_cols));
197 
198  float64_t nlml_new=0;
199  float64_t nlml_def=0;
200 
202  index_t total_len=len*2;
203 
204  if (m_alpha.vlen == total_len)
205  {
207 
208  SGVector<float64_t> s2_tmp(m_s2.vlen);
209  Map<VectorXd> eigen_s2(s2_tmp.vector, s2_tmp.vlen);
210  eigen_s2.fill(1.0);
214  float64_t trace=eigen_InvK.diagonal().array().sum();
215  nlml_def=-a+0.5*(-eigen_K.rows()+trace+m_log_det_Kernel);
216 
217  if (nlml_new<=nlml_def)
219  }
220 
221  if (m_alpha.vlen != total_len || nlml_def<nlml_new)
222  {
223  if(m_alpha.vlen != total_len)
224  m_alpha = SGVector<float64_t>(total_len);
225  m_alpha.zero();
226 
229  }
230 
231  nlml_new=lbfgs_optimization();
232 }
233 
235 {
238  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
239  eigen_Sigma=eigen_s2.asDiagonal();
240 }
241 
243 {
247  eigen_InvK_Sigma=solve_inverse(eigen_Sigma);
248 }
249 
250 } /* namespace shogun */
251 
252 #endif /* HAVE_EIGEN3 */
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
SGVector< float64_t > m_alpha
The Inference Method base class.
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const =0
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
TParameter * get_parameter(int32_t idx)
Definition: SGMatrix.h:20
parameter struct
#define REQUIRE(x,...)
Definition: SGIO.h:206
Parameter * m_parameters
Definition: SGObject.h:378
index_t num_cols
Definition: SGMatrix.h:378
An abstract class of the mean function.
Definition: MeanFunction.h:49
#define SG_REF(x)
Definition: SGObject.h:51
index_t num_rows
Definition: SGMatrix.h:376
static CKLApproxDiagonalInferenceMethod * obtain_from_generic(CInferenceMethod *inference)
virtual void get_gradient_of_nlml_wrt_parameters(SGVector< float64_t > gradient)
SGMatrix< float64_t > m_Sigma
The KL approximation inference method class.
index_t vlen
Definition: SGVector.h:494
virtual float64_t lbfgs_optimization()
double float64_t
Definition: common.h:50
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:354
virtual SGVector< float64_t > get_variational_expection()=0
The KL approximation inference method class.
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
#define SG_SERROR(...)
Definition: SGIO.h:179
static float64_t exp(float64_t x)
Definition: Math.h:621
SGVector< float64_t > m_mu
SGVector< float64_t > m_s2
virtual EInferenceType get_inference_type() const
The Kernel base class.
Definition: Kernel.h:158
virtual CVariationalGaussianLikelihood * get_variational_likelihood() const
#define SG_ADD(...)
Definition: SGObject.h:81
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:262
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr

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