SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KLCholeskyInferenceMethod.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 
44 #ifdef HAVE_EIGEN3
48 
49 using namespace Eigen;
50 
51 namespace shogun
52 {
53 
54 CKLCholeskyInferenceMethod::CKLCholeskyInferenceMethod() : CKLLowerTriangularInferenceMethod()
55 {
56  init();
57 }
58 
60  CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod)
61  : CKLLowerTriangularInferenceMethod(kern, feat, m, lab, mod)
62 {
63  init();
64 }
65 
66 void CKLCholeskyInferenceMethod::init()
67 {
68  SG_ADD(&m_C, "C",
69  "The Cholesky represention of the variational co-variance matrix",
71  SG_ADD(&m_InvK_C, "invK_C",
72  " The K^{-1}C matrix",
74 }
75 
76 
78 {
87  update();
88 
89  index_t len=m_mu.vlen;
90  SGVector<float64_t> result(len);
91 
92  Map<VectorXd> eigen_result(result.vector, len);
93  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
94 
95  eigen_result=eigen_alpha;
96 
97  return result;
98 }
99 
101 {
102 }
103 
105 {
106  index_t len=m_mean_vec.vlen;
107  Map<VectorXd> eigen_mean(m_mean_vec.vector, m_mean_vec.vlen);
108  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
109  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
110 
111  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
112  //mu=K*alpha+m
113  eigen_mu=eigen_K*CMath::sq(m_scale)*eigen_alpha+eigen_mean;
114 
115  update_C();
116  Map<MatrixXd> eigen_C(m_C.matrix, m_C.num_rows, m_C.num_cols);
117  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
118  //s2=sum(C.*C,2);
119  eigen_s2=(eigen_C.array()*eigen_C.array()).rowwise().sum().matrix();
120 
123  Map<MatrixXd> eigen_InvK_C(m_InvK_C.matrix, m_InvK_C.num_rows, m_InvK_C.num_cols);
124 
125  eigen_InvK_C=solve_inverse(eigen_C);
126 }
127 
129 {
130  REQUIRE(gradient.vlen==m_alpha.vlen,
131  "The length of gradients (%d) should the same as the length of parameters (%d)\n",
132  gradient.vlen, m_alpha.vlen);
133 
134  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
135  Map<MatrixXd> eigen_C(m_C.matrix, m_C.num_rows, m_C.num_cols);
136 
137  index_t len=m_mu.vlen;
138  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
139  Map<VectorXd> eigen_C_seq(m_alpha.vector+len, m_alpha.vlen-len);
140 
142  //[a,df,dV] = a_related2(mu,s2,y,lik);
143  TParameter* s2_param=lik->m_parameters->get_parameter("sigma2");
145  Map<VectorXd> eigen_dv(dv.vector, dv.vlen);
146 
147  TParameter* mu_param=lik->m_parameters->get_parameter("mu");
149  Map<VectorXd> eigen_df(df.vector, df.vlen);
150 
151  Map<VectorXd> eigen_dnlz_alpha(gradient.vector, len);
152  //dnlZ_alpha = -K*(df-alpha);
153  eigen_dnlz_alpha=eigen_K*CMath::sq(m_scale)*(-eigen_df+eigen_alpha);
154 
155  Map<VectorXd> eigen_dnlz_C_seq(gradient.vector+len, gradient.vlen-len);
156 
157  SGVector<float64_t> tmp(eigen_dnlz_C_seq.rows());
158  Map<VectorXd> eigen_tmp(tmp.vector, tmp.vlen);
159 
160  //dnlZ_C=low_matrix_to_vector(invK_C)-convert_diag(1.0./diag(C))-2*(alla(n+1:end,1).*convert_dC(dv));
161  float64_t offset=0;
162  for (index_t i=0; i<len; i++)
163  {
164  eigen_tmp.block(offset, 0, len-i, 1)=VectorXd::Map(eigen_dv.data()+i, len-i);
165  offset+=(len-i);
166  }
167 
168  //-2*(alla(n+1:end,1).*convert_dC(dV))
169  eigen_dnlz_C_seq=(-2.0*(eigen_C_seq.array()*eigen_tmp.array())).matrix();
170  //low_matrix_to_vector(invK_C)
171  get_lower_triangular_vector(m_InvK_C, tmp);
172  eigen_dnlz_C_seq+=eigen_tmp;
173 
174  Map<VectorXd> eigen_tmp2(tmp.vector, eigen_C.rows());
175  //-convert_diag(1.0./diag(C))
176  eigen_tmp2=(1.0/eigen_C.diagonal().array()).matrix();
177 
178  offset=0;
179  for (index_t i=0; i<len; i++)
180  {
181  eigen_dnlz_C_seq.block(offset,0,1,1)-=VectorXd::Map(eigen_tmp2.data()+i,1);
182  offset+=(len-i);
183  }
184 }
185 
187 {
188  Map<VectorXd> eigen_alpha(m_alpha.vector, m_mu.vlen);
189  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
190  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
191  //get mean vector and create eigen representation of it
192  Map<VectorXd> eigen_mean(m_mean_vec.vector, m_mean_vec.vlen);
193 
194  Map<MatrixXd> eigen_InvK_C(m_InvK_C.matrix, m_InvK_C.num_rows, m_InvK_C.num_cols);
195  Map<MatrixXd> eigen_C(m_C.matrix, m_C.num_rows, m_C.num_cols);
196 
199 
200  //float64_t log_det=2.0*log_det(eigen_C)-m_log_det_Kernel;
201  float64_t log_det=2.0*eigen_C.diagonal().array().abs().log().sum()-m_log_det_Kernel;
202  float64_t trace=(eigen_InvK_C.array()*eigen_C.array()).sum();
203 
204  //nlZ = -a -logdet(V*inv(K))/2 -n/2 +(alpha'*K*alpha)/2 +trace(V*inv(K))/2;
205  float64_t result=-a+0.5*(-eigen_K.rows()+eigen_alpha.dot(eigen_mu-eigen_mean)+trace-log_det);
206  return result;
207 }
208 
210 {
211  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
212 
213  float64_t nlml_new=0;
214  float64_t nlml_def=0;
215 
217  index_t total_len=len*(len+3);
218 
219  if (m_alpha.vlen*2 == total_len)
220  {
222 
223  SGVector<float64_t> s2_tmp(m_s2.vlen);
224  Map<VectorXd> eigen_s2(s2_tmp.vector, s2_tmp.vlen);
225  eigen_s2.fill(1.0);
229  MatrixXd inv_K=solve_inverse(MatrixXd::Identity(m_ktrtr.num_rows, m_ktrtr.num_cols));
230  float64_t trace=inv_K.diagonal().array().sum();
231  nlml_def=-a+0.5*(-eigen_K.rows()+trace+m_log_det_Kernel);
232 
233  if (nlml_new<=nlml_def)
235  }
236 
237  if (m_alpha.vlen*2 != total_len || nlml_def<nlml_new)
238  {
239  if(m_alpha.vlen*2 != total_len)
240  m_alpha = SGVector<float64_t>(total_len/2);
241  m_alpha.zero();
242  index_t offset=0;
243  index_t count=0;
244  //init
245  for (index_t i=0; i<m_alpha.vlen; i++)
246  {
247  if (i-len==offset)
248  {
249  m_alpha[i]=1.0;
250  offset+=(len-count);
251  count++;
252  }
253  }
254  m_InvK_C=SGMatrix<float64_t>(len, len);
255  m_C=SGMatrix<float64_t>(len, len);
256  m_C.zero();
259  }
260 
261  nlml_new=lbfgs_optimization();
262 }
263 
264 void CKLCholeskyInferenceMethod::update_C()
265 {
266  ASSERT(m_C.num_rows == m_C.num_cols);
267  index_t len=m_C.num_rows;
268  ASSERT(m_alpha.vlen*2 == len*(len+3));
269 
270  Map<MatrixXd> eigen_C(m_C.matrix, m_C.num_rows, m_C.num_cols);
271  Map<VectorXd> eigen_C_seq(m_alpha.vector+len, m_alpha.vlen-len);
272 
273  index_t offset=0;
274  for (index_t i=0; i<len; i++)
275  {
276  eigen_C.block(i, i, len-i ,1)=VectorXd::Map(eigen_C_seq.data()+offset, len-i);
277  offset+=(len-i);
278  }
279 }
280 
281 void CKLCholeskyInferenceMethod::get_lower_triangular_vector(SGMatrix<float64_t> square_matrix,
282  SGVector<float64_t> target)
283 {
284  ASSERT(square_matrix.num_rows == square_matrix.num_cols);
285  index_t len=m_InvK_C.num_rows;
286  ASSERT(target.vlen*2 == len*(len+1));
287 
288  Map<MatrixXd> eigen_square_matrix(square_matrix.matrix, len, len);
289  Map<VectorXd> eigen_result(target.vector, target.vlen);
290 
291  index_t offset=0;
292  for (index_t i=0; i<len; i++)
293  {
294  eigen_result.block(offset, 0, len-i, 1)=eigen_square_matrix.block(i, i, len-i, 1);
295  offset+=(len-i);
296  }
297 }
298 
300 {
302  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
303  Map<MatrixXd> eigen_C(m_C.matrix, m_C.num_rows, m_C.num_cols);
304  eigen_Sigma=eigen_C*(eigen_C.transpose());
305 }
306 
308 {
310  Map<MatrixXd> eigen_InvK_Sigma(m_InvK_Sigma.matrix, m_InvK_Sigma.num_rows, m_InvK_Sigma.num_cols);
311  Map<MatrixXd> eigen_InvK_C(m_InvK_C.matrix, m_InvK_C.num_rows, m_InvK_C.num_cols);
312  Map<MatrixXd> eigen_C(m_C.matrix, m_C.num_rows, m_C.num_cols);
313  eigen_InvK_Sigma=eigen_InvK_C*(eigen_C.transpose());
314 }
315 
316 } /* namespace shogun */
317 
318 #endif /* HAVE_EIGEN3 */
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 float64_t get_negative_log_marginal_likelihood_helper()
virtual int32_t get_num_labels() const =0
static T sum(T *vec, int32_t len)
return sum(vec)
Definition: SGVector.h:507
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
static T sq(T x)
x^2
Definition: Math.h:324
TParameter * get_parameter(int32_t idx)
Definition: Parameter.h:286
parameter struct
Definition: Parameter.h:32
#define REQUIRE(x,...)
Definition: SGIO.h:207
Parameter * m_parameters
Definition: SGObject.h:470
An abstract class of the mean function.
Definition: MeanFunction.h:28
virtual SGVector< float64_t > get_alpha()
virtual void set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual void get_gradient_of_nlml_wrt_parameters(SGVector< float64_t > gradient)
SGMatrix< float64_t > m_Sigma
The KL approximation inference method class.
#define ASSERT(x)
Definition: SGIO.h:202
virtual float64_t lbfgs_optimization()
double float64_t
Definition: common.h:50
index_t num_rows
Definition: SGMatrix.h:298
virtual SGVector< float64_t > get_variational_expection()=0
index_t num_cols
Definition: SGMatrix.h:300
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:67
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:209
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr
index_t vlen
Definition: SGVector.h:707

SHOGUN Machine Learning Toolbox - Documentation