SHOGUN  4.0.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 {
84  update();
85 
86  index_t len=m_mu.vlen;
87  SGVector<float64_t> result(len);
88 
89  Map<VectorXd> eigen_result(result.vector, len);
90  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
91 
92  eigen_result=eigen_alpha;
93  return result;
94 }
95 
97 {
98 }
99 
101 {
102  index_t len=m_mean_vec.vlen;
105  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
106 
107  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
108  //mu=K*alpha+m
109  eigen_mu=eigen_K*CMath::sq(m_scale)*eigen_alpha+eigen_mean;
110 
111  Map<VectorXd> eigen_log_v(m_alpha.vector+len, m_alpha.vlen-len);
112  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
113  //s2=sum(C.*C,2);
114  eigen_s2=eigen_log_v.array().exp();
115 
117  bool status=lik->set_variational_distribution(m_mu, m_s2, m_labels);
118  return status;
119 }
120 
122 {
123  REQUIRE(gradient.vlen==m_alpha.vlen,
124  "The length of gradients (%d) should the same as the length of parameters (%d)\n",
125  gradient.vlen, m_alpha.vlen);
126 
128  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
129 
130  index_t len=m_mu.vlen;
131  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
132  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
133 
135  //[a,df,dV] = a_related2(mu,s2,y,lik);
136  TParameter* s2_param=lik->m_parameters->get_parameter("sigma2");
138  Map<VectorXd> eigen_dv(dv.vector, dv.vlen);
139 
140  TParameter* mu_param=lik->m_parameters->get_parameter("mu");
142  Map<VectorXd> eigen_df(df.vector, df.vlen);
143 
144  Map<VectorXd> eigen_dnlz_alpha(gradient.vector, len);
145  //dnlZ_alpha = -K*(df-alpha);
146  eigen_dnlz_alpha=eigen_K*CMath::sq(m_scale)*(-eigen_df+eigen_alpha);
147 
148  Map<VectorXd> eigen_dnlz_log_v(gradient.vector+len, gradient.vlen-len);
149 
150  eigen_dnlz_log_v=(eigen_InvK.diagonal().array()-(1.0/eigen_s2.array()));
151  eigen_dnlz_log_v=(0.5*eigen_dnlz_log_v.array())-eigen_dv.array();
152  eigen_dnlz_log_v=eigen_dnlz_log_v.array()*eigen_s2.array();
153 
154 }
155 
157 {
158  Map<VectorXd> eigen_alpha(m_alpha.vector, m_mu.vlen);
159  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
161  //get mean vector and create eigen representation of it
164  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
165  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
166 
169  float64_t log_det=eigen_log_v.array().sum()-m_log_det_Kernel;
170  float64_t trace=(eigen_s2.array()*eigen_InvK.diagonal().array()).sum();
171 
172  //nlZ = -a -logdet(V*inv(K))/2 -n/2 +(alpha'*K*alpha)/2 +trace(V*inv(K))/2;
173  float64_t result=-a+0.5*(-eigen_K.rows()+eigen_alpha.dot(eigen_mu-eigen_mean)+trace-log_det);
174 
175  return result;
176 }
177 
179 {
182  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
183  eigen_InvK=solve_inverse(MatrixXd::Identity(m_ktrtr.num_rows,m_ktrtr.num_cols));
184 
185  float64_t nlml_new=0;
186  float64_t nlml_def=0;
187 
189  index_t total_len=len*2;
190 
191  if (m_alpha.vlen == total_len)
192  {
194 
195  SGVector<float64_t> s2_tmp(m_s2.vlen);
196  Map<VectorXd> eigen_s2(s2_tmp.vector, s2_tmp.vlen);
197  eigen_s2.fill(1.0);
201  float64_t trace=eigen_InvK.diagonal().array().sum();
202  nlml_def=-a+0.5*(-eigen_K.rows()+trace+m_log_det_Kernel);
203 
204  if (nlml_new<=nlml_def)
206  }
207 
208  if (m_alpha.vlen != total_len || nlml_def<nlml_new)
209  {
210  if(m_alpha.vlen != total_len)
211  m_alpha = SGVector<float64_t>(total_len);
212  m_alpha.zero();
213 
216  }
217 
218  nlml_new=lbfgs_optimization();
219 }
220 
222 {
225  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
226  eigen_Sigma=eigen_s2.asDiagonal();
227 }
228 
230 {
234  eigen_InvK_Sigma=solve_inverse(eigen_Sigma);
235 }
236 
237 } /* namespace shogun */
238 
239 #endif /* HAVE_EIGEN3 */
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
SGVector< float64_t > m_alpha
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
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:341
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
static T sq(T x)
Definition: Math.h:450
TParameter * get_parameter(int32_t idx)
Definition: Parameter.h:286
Definition: SGMatrix.h:20
parameter struct
Definition: Parameter.h:32
#define REQUIRE(x,...)
Definition: SGIO.h:206
Parameter * m_parameters
Definition: SGObject.h:505
An abstract class of the mean function.
Definition: MeanFunction.h:28
virtual void get_gradient_of_nlml_wrt_parameters(SGVector< float64_t > gradient)
SGMatrix< float64_t > m_Sigma
The KL approximation inference method class.
virtual float64_t lbfgs_optimization()
double float64_t
Definition: common.h:50
Matrix::Scalar sum(Matrix m, bool no_diag=false)
Definition: Redux.h:70
index_t num_rows
Definition: SGMatrix.h:329
virtual SGVector< float64_t > get_variational_expection()=0
index_t num_cols
Definition: SGMatrix.h:331
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
SGVector< float64_t > m_mu
SGVector< float64_t > m_s2
The Kernel base class.
Definition: Kernel.h:153
virtual CVariationalGaussianLikelihood * get_variational_likelihood() const
#define SG_ADD(...)
Definition: SGObject.h:81
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:263
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr
index_t vlen
Definition: SGVector.h:481

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