SHOGUN  6.1.3
KLDiagonalInferenceMethod.cpp
Go to the documentation of this file.
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 
48 
49 using namespace Eigen;
50 
51 namespace shogun
52 {
53 
54 CKLDiagonalInferenceMethod::CKLDiagonalInferenceMethod() : CKLLowerTriangularInference()
55 {
56  init();
57 }
58 
60  CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod)
61  : CKLLowerTriangularInference(kern, feat, m, lab, mod)
62 {
63  init();
64 }
65 
66 void CKLDiagonalInferenceMethod::init()
67 {
68  SG_ADD(&m_InvK, "invK",
69  "The K^{-1} matrix",
71 }
72 
74  CInference* inference)
75 {
76  if (inference==NULL)
77  return NULL;
78 
79  if (inference->get_inference_type()!=INF_KL_DIAGONAL)
80  SG_SERROR("Provided inference is not of type CKLDiagonalInferenceMethod!\n")
81 
82  SG_REF(inference);
83  return (CKLDiagonalInferenceMethod*)inference;
84 }
85 
87 {
96  update();
97 
98  index_t len=m_mu.vlen;
99  SGVector<float64_t> result(len);
100 
101  Map<VectorXd> eigen_result(result.vector, len);
102  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
103 
104  eigen_result=eigen_alpha;
105  return result;
106 }
107 
109 {
110 }
111 
113 {
114  index_t len=m_mean_vec.vlen;
117  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
118 
119  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
120  //mu=K*alpha+m
121  eigen_mu=eigen_K*CMath::exp(m_log_scale*2.0)*eigen_alpha+eigen_mean;
122 
123  Map<VectorXd> eigen_log_v(m_alpha.vector+len, m_alpha.vlen-len);
124  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
125  //s2=sum(C.*C,2);
126  eigen_s2=eigen_log_v.array().exp();
127 
129  bool status=lik->set_variational_distribution(m_mu, m_s2, m_labels);
130  return status;
131 }
132 
134 {
135  REQUIRE(gradient.vlen==m_alpha.vlen,
136  "The length of gradients (%d) should the same as the length of parameters (%d)\n",
137  gradient.vlen, m_alpha.vlen);
138 
140  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
141 
142  index_t len=m_mu.vlen;
143  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
144  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
145 
147  //[a,df,dV] = a_related2(mu,s2,y,lik);
148  TParameter* s2_param=lik->m_parameters->get_parameter("sigma2");
150  Map<VectorXd> eigen_dv(dv.vector, dv.vlen);
151 
152  TParameter* mu_param=lik->m_parameters->get_parameter("mu");
154  Map<VectorXd> eigen_df(df.vector, df.vlen);
155 
156  Map<VectorXd> eigen_dnlz_alpha(gradient.vector, len);
157  //dnlZ_alpha = -K*(df-alpha);
158  eigen_dnlz_alpha=eigen_K*CMath::exp(m_log_scale*2.0)*(-eigen_df+eigen_alpha);
159 
160  Map<VectorXd> eigen_dnlz_log_v(gradient.vector+len, gradient.vlen-len);
161 
162  eigen_dnlz_log_v=(eigen_InvK.diagonal().array()-(1.0/eigen_s2.array()));
163  eigen_dnlz_log_v=(0.5*eigen_dnlz_log_v.array())-eigen_dv.array();
164  eigen_dnlz_log_v=eigen_dnlz_log_v.array()*eigen_s2.array();
165 
166 }
167 
169 {
170  Map<VectorXd> eigen_alpha(m_alpha.vector, m_mu.vlen);
171  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
173  //get mean vector and create eigen representation of it
176  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
177  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
178 
181  float64_t log_det=eigen_log_v.array().sum()-m_log_det_Kernel;
182  float64_t trace=(eigen_s2.array()*eigen_InvK.diagonal().array()).sum();
183 
184  //nlZ = -a -logdet(V*inv(K))/2 -n/2 +(alpha'*K*alpha)/2 +trace(V*inv(K))/2;
185  float64_t result=-a+0.5*(-eigen_K.rows()+eigen_alpha.dot(eigen_mu-eigen_mean)+trace-log_det);
186 
187  return result;
188 }
189 
191 {
194  Map<MatrixXd> eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols);
195  eigen_InvK=solve_inverse(MatrixXd::Identity(m_ktrtr.num_rows,m_ktrtr.num_cols));
196 
197  float64_t nlml_new=0;
198  float64_t nlml_def=0;
199 
201  index_t total_len=len*2;
202 
203  if (m_alpha.vlen == total_len)
204  {
206 
207  SGVector<float64_t> s2_tmp(m_s2.vlen);
208  Map<VectorXd> eigen_s2(s2_tmp.vector, s2_tmp.vlen);
209  eigen_s2.fill(1.0);
213  float64_t trace=eigen_InvK.diagonal().array().sum();
214  nlml_def=-a+0.5*(-eigen_K.rows()+trace+m_log_det_Kernel);
215 
216  if (nlml_new<=nlml_def)
218  }
219 
220  if (m_alpha.vlen != total_len || nlml_def<nlml_new)
221  {
222  if(m_alpha.vlen != total_len)
223  m_alpha = SGVector<float64_t>(total_len);
224  m_alpha.zero();
225 
228  }
229 
230  nlml_new=optimization();
231 }
232 
234 {
237  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
238  eigen_Sigma=eigen_s2.asDiagonal();
239 }
240 
242 {
246  eigen_InvK_Sigma=solve_inverse(eigen_Sigma);
247 }
248 
249 } /* namespace shogun */
250 
float64_t m_log_scale
Definition: Inference.h:485
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual float64_t optimization()
virtual void update()
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const =0
The KL approximation inference method class.
virtual float64_t get_negative_log_marginal_likelihood_helper()
int32_t index_t
Definition: common.h:72
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual EInferenceType get_inference_type() const
Definition: Inference.h:104
virtual CVariationalGaussianLikelihood * get_variational_likelihood() const
virtual int32_t get_num_labels() const =0
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:418
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
TParameter * get_parameter(int32_t idx)
Definition: SGMatrix.h:25
parameter struct
#define REQUIRE(x,...)
Definition: SGIO.h:181
Parameter * m_parameters
Definition: SGObject.h:609
An abstract class of the mean function.
Definition: MeanFunction.h:49
#define SG_REF(x)
Definition: SGObject.h:52
T trace(const SGMatrix< T > &A)
SGMatrix< float64_t > m_ktrtr
Definition: Inference.h:488
The KL approximation inference method class.
CLabels * m_labels
Definition: Inference.h:476
double float64_t
Definition: common.h:60
index_t num_rows
Definition: SGMatrix.h:495
SGVector< float64_t > m_mu
Definition: KLInference.h:367
virtual SGVector< float64_t > get_alpha()
virtual SGVector< float64_t > get_variational_expection()=0
index_t num_cols
Definition: SGMatrix.h:497
Eigen::MatrixXd solve_inverse(Eigen::MatrixXd A)
SGMatrix< float64_t > m_Sigma
Definition: KLInference.h:370
SGVector< float64_t > m_s2
Definition: KLInference.h:375
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
T sum(const Container< T > &a, bool no_diag=false)
The Inference Method base class.
Definition: Inference.h:81
The class Features is the base class of all feature objects.
Definition: Features.h:69
#define SG_SERROR(...)
Definition: SGIO.h:164
static float64_t exp(float64_t x)
Definition: Math.h:551
The Kernel base class.
#define SG_ADD(...)
Definition: SGObject.h:93
static CKLDiagonalInferenceMethod * obtain_from_generic(CInference *inference)
virtual void get_gradient_of_nlml_wrt_parameters(SGVector< float64_t > gradient)
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:296
The Likelihood model base class.
index_t vlen
Definition: SGVector.h:571
SGVector< float64_t > m_alpha
Definition: Inference.h:479

SHOGUN Machine Learning Toolbox - Documentation