SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EPInferenceMethod.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Roman Votyakov
8  *
9  * Based on ideas from GAUSSIAN PROCESS REGRESSION AND CLASSIFICATION Toolbox
10  * Copyright (C) 2005-2013 by Carl Edward Rasmussen & Hannes Nickisch under the
11  * FreeBSD License
12  * http://www.gaussianprocess.org/gpml/code/matlab/doc/
13  */
14 
16 
17 #ifdef HAVE_EIGEN3
18 
24 
26 
27 using namespace shogun;
28 using namespace Eigen;
29 
30 // try to use previously allocated memory for SGVector
31 #define CREATE_SGVECTOR(vec, len, sg_type) \
32  { \
33  if (!vec.vector || vec.vlen!=len) \
34  vec=SGVector<sg_type>(len); \
35  }
36 
37 // try to use previously allocated memory for SGMatrix
38 #define CREATE_SGMATRIX(mat, rows, cols, sg_type) \
39  { \
40  if (!mat.matrix || mat.num_rows!=rows || mat.num_cols!=cols) \
41  mat=SGMatrix<sg_type>(rows, cols); \
42  }
43 
45 {
46  init();
47 }
48 
50  CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model)
51  : CInferenceMethod(kernel, features, mean, labels, model)
52 {
53  init();
54 }
55 
57 {
58 }
59 
60 void CEPInferenceMethod::init()
61 {
62  m_max_sweep=15;
63  m_min_sweep=2;
64  m_tol=1e-4;
65 }
66 
68 {
70  update();
71 
72  return m_nlZ;
73 }
74 
76 {
78  update();
79 
81 }
82 
84 {
86  update();
87 
88  return SGMatrix<float64_t>(m_L);
89 }
90 
92 {
94  update();
95 
96  return SGVector<float64_t>(m_sttau);
97 }
98 
100 {
102  update();
103 
104  return SGVector<float64_t>(m_mu);
105 }
106 
108 {
110  update();
111 
112  return SGMatrix<float64_t>(m_Sigma);
113 }
114 
116 {
117  SG_DEBUG("entering\n");
118 
119  // update kernel and feature matrix
121 
122  // get number of labels (trainig examples)
124 
125  // try to use tilde values from previous call
126  if (m_ttau.vlen==n)
127  {
128  update_chol();
132  }
133 
134  // get mean vector
136 
137  // get and scale diagonal of the kernel matrix
139  ktrtr_diag.scale(CMath::sq(m_scale));
140 
141  // marginal likelihood for ttau = tnu = 0
143  mean, ktrtr_diag, m_labels));
144 
145  // use zero values if we have no better guess or it's better
146  if (m_ttau.vlen!=n || m_nlZ>nlZ0)
147  {
148  CREATE_SGVECTOR(m_ttau, n, float64_t);
149  m_ttau.zero();
150 
151  CREATE_SGVECTOR(m_sttau, n, float64_t);
152  m_sttau.zero();
153 
154  CREATE_SGVECTOR(m_tnu, n, float64_t);
155  m_tnu.zero();
156 
158 
159  // copy data manually, since we don't have appropriate method
160  for (index_t i=0; i<m_ktrtr.num_rows; i++)
161  for (index_t j=0; j<m_ktrtr.num_cols; j++)
162  m_Sigma(i,j)=m_ktrtr(i,j)*CMath::sq(m_scale);
163 
164  CREATE_SGVECTOR(m_mu, n, float64_t);
165  m_mu.zero();
166 
167  // set marginal likelihood
168  m_nlZ=nlZ0;
169  }
170 
171  // create vector of the random permutation
173 
174  // cavity tau and nu vectors
175  SGVector<float64_t> tau_n(n);
176  SGVector<float64_t> nu_n(n);
177 
178  // cavity mu and s2 vectors
179  SGVector<float64_t> mu_n(n);
180  SGVector<float64_t> s2_n(n);
181 
182  float64_t nlZ_old=CMath::INFTY;
183  uint32_t sweep=0;
184 
185  while ((CMath::abs(m_nlZ-nlZ_old)>m_tol && sweep<m_max_sweep) ||
186  sweep<m_min_sweep)
187  {
188  nlZ_old=m_nlZ;
189  sweep++;
190 
191  // shuffle random permutation
192  randperm.permute();
193 
194  for (index_t j=0; j<n; j++)
195  {
196  index_t i=randperm[j];
197 
198  // find cavity paramters
199  tau_n[i]=1.0/m_Sigma(i,i)-m_ttau[i];
200  nu_n[i]=m_mu[i]/m_Sigma(i,i)+mean[i]*tau_n[i]-m_tnu[i];
201 
202  // compute cavity mean and variance
203  mu_n[i]=nu_n[i]/tau_n[i];
204  s2_n[i]=1.0/tau_n[i];
205 
206  // get moments
207  float64_t mu=m_model->get_first_moment(mu_n, s2_n, m_labels, i);
208  float64_t s2=m_model->get_second_moment(mu_n, s2_n, m_labels, i);
209 
210  // save old value of ttau
211  float64_t ttau_old=m_ttau[i];
212 
213  // compute ttau and sqrt(ttau)
214  m_ttau[i]=CMath::max(1.0/s2-tau_n[i], 0.0);
215  m_sttau[i]=CMath::sqrt(m_ttau[i]);
216 
217  // compute tnu
218  m_tnu[i]=mu/s2-nu_n[i];
219 
220  // compute difference ds2=ttau_new-ttau_old
221  float64_t ds2=m_ttau[i]-ttau_old;
222 
223  // create eigen representation of Sigma, tnu and mu
224  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows,
225  m_Sigma.num_cols);
226  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
227  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
228 
229  VectorXd eigen_si=eigen_Sigma.col(i);
230 
231  // rank-1 update Sigma
232  eigen_Sigma=eigen_Sigma-ds2/(1.0+ds2*eigen_si(i))*eigen_si*
233  eigen_si.adjoint();
234 
235  // update mu
236  eigen_mu=eigen_Sigma*eigen_tnu;
237  }
238 
239  // update upper triangular factor (L^T) of Cholesky decomposition of
240  // matrix B, approximate posterior covariance and mean, negative
241  // marginal likelihood
242  update_chol();
246  }
247 
248  if (sweep==m_max_sweep && CMath::abs(m_nlZ-nlZ_old)>m_tol)
249  {
250  SG_ERROR("Maximum number (%d) of sweeps reached, but tolerance (%f) was "
251  "not yet reached. You can manually set maximum number of sweeps "
252  "or tolerance to fix this problem.\n", m_max_sweep, m_tol);
253  }
254 
255  // update vector alpha
256  update_alpha();
257 
258  // update matrices to compute derivatives
259  update_deriv();
260 
261  // update hash of the parameters
263 
264  SG_DEBUG("leaving\n");
265 }
266 
268 {
269  // create eigen representations kernel matrix, L^T, sqrt(ttau) and tnu
270  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
271  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
272  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
273  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
274 
275  // create shogun and eigen representation of the alpha vector
277  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
278 
279  // solve LL^T * v = tS^(1/2) * K * tnu
280  VectorXd eigen_v=eigen_L.triangularView<Upper>().adjoint().solve(
281  eigen_sttau.cwiseProduct(eigen_K*CMath::sq(m_scale)*eigen_tnu));
282  eigen_v=eigen_L.triangularView<Upper>().solve(eigen_v);
283 
284  // compute alpha = (I - tS^(1/2) * B^(-1) * tS(1/2) * K) * tnu =
285  // tnu - tS(1/2) * (L^T)^(-1) * L^(-1) * tS^(1/2) * K * tnu =
286  // tnu - tS(1/2) * v
287  eigen_alpha=eigen_tnu-eigen_sttau.cwiseProduct(eigen_v);
288 }
289 
291 {
292  // create eigen representations of kernel matrix and sqrt(ttau)
293  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
294  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
295 
296  // create shogun and eigen representation of the upper triangular factor
297  // (L^T) of the Cholesky decomposition of the matrix B
299  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
300 
301  // compute upper triangular factor L^T of the Cholesky decomposion of the
302  // matrix: B = tS^(1/2) * K * tS^(1/2) + I
303  LLT<MatrixXd> eigen_chol((eigen_sttau*eigen_sttau.adjoint()).cwiseProduct(
304  eigen_K*CMath::sq(m_scale))+
305  MatrixXd::Identity(m_L.num_rows, m_L.num_cols));
306 
307  eigen_L=eigen_chol.matrixU();
308 }
309 
311 {
312  // create eigen representations of kernel matrix, L^T matrix and sqrt(ttau)
313  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
314  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
315  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
316 
317  // create shogun and eigen representation of the approximate covariance
318  // matrix
320  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
321 
322  // compute V = L^(-1) * tS^(1/2) * K, using upper triangular factor L^T
323  MatrixXd eigen_V=eigen_L.triangularView<Upper>().adjoint().solve(
324  eigen_sttau.asDiagonal()*eigen_K*CMath::sq(m_scale));
325 
326  // compute covariance matrix of the posterior:
327  // Sigma = K - K * tS^(1/2) * (L * L^T)^(-1) * tS^(1/2) * K =
328  // K - (K * tS^(1/2)) * (L^T)^(-1) * L^(-1) * tS^(1/2) * K =
329  // K - (tS^(1/2) * K)^T * (L^(-1))^T * L^(-1) * tS^(1/2) * K = K - V^T * V
330  eigen_Sigma=eigen_K*CMath::sq(m_scale)-eigen_V.adjoint()*eigen_V;
331 }
332 
334 {
335  // create eigen representation of posterior covariance matrix and tnu
336  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
337  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
338 
339  // create shogun and eigen representation of the approximate mean vector
340  CREATE_SGVECTOR(m_mu, m_tnu.vlen, float64_t);
341  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
342 
343  // compute mean vector of the approximate posterior: mu = Sigma * tnu
344  eigen_mu=eigen_Sigma*eigen_tnu;
345 }
346 
348 {
349  // create eigen representation of Sigma, L, mu, tnu, ttau
350  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
351  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
352  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
353  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
354  Map<VectorXd> eigen_ttau(m_ttau.vector, m_ttau.vlen);
355 
356  // get and create eigen representation of the mean vector
358  Map<VectorXd> eigen_m(m.vector, m.vlen);
359 
360  // compute vector of cavity parameter tau_n
361  VectorXd eigen_tau_n=(VectorXd::Ones(m_ttau.vlen)).cwiseQuotient(
362  eigen_Sigma.diagonal())-eigen_ttau;
363 
364  // compute vector of cavity parameter nu_n
365  VectorXd eigen_nu_n=eigen_mu.cwiseQuotient(eigen_Sigma.diagonal())-
366  eigen_tnu+eigen_m.cwiseProduct(eigen_tau_n);
367 
368  // compute cavity mean: mu_n=nu_n/tau_n
369  SGVector<float64_t> mu_n(m_ttau.vlen);
370  Map<VectorXd> eigen_mu_n(mu_n.vector, mu_n.vlen);
371 
372  eigen_mu_n=eigen_nu_n.cwiseQuotient(eigen_tau_n);
373 
374  // compute cavity variance: s2_n=1.0/tau_n
375  SGVector<float64_t> s2_n(m_ttau.vlen);
376  Map<VectorXd> eigen_s2_n(s2_n.vector, s2_n.vlen);
377 
378  eigen_s2_n=(VectorXd::Ones(m_ttau.vlen)).cwiseQuotient(eigen_tau_n);
379 
381  m_model->get_log_zeroth_moments(mu_n, s2_n, m_labels));
382 
383  // compute nlZ_part1=sum(log(diag(L)))-sum(lZ)-tnu'*Sigma*tnu/2
384  float64_t nlZ_part1=eigen_L.diagonal().array().log().sum()-lZ-
385  (eigen_tnu.adjoint()*eigen_Sigma).dot(eigen_tnu)/2.0;
386 
387  // compute nlZ_part2=sum(tnu.^2./(tau_n+ttau))/2-sum(log(1+ttau./tau_n))/2
388  float64_t nlZ_part2=(eigen_tnu.array().square()/
389  (eigen_tau_n+eigen_ttau).array()).sum()/2.0-(1.0+eigen_ttau.array()/
390  eigen_tau_n.array()).log().sum()/2.0;
391 
392  // compute nlZ_part3=-(nu_n-m.*tau_n)'*((ttau./tau_n.*(nu_n-m.*tau_n)-2*tnu)
393  // ./(ttau+tau_n))/2
394  float64_t nlZ_part3=-(eigen_nu_n-eigen_m.cwiseProduct(eigen_tau_n)).dot(
395  ((eigen_ttau.array()/eigen_tau_n.array()*(eigen_nu_n.array()-
396  eigen_m.array()*eigen_tau_n.array())-2*eigen_tnu.array())/
397  (eigen_ttau.array()+eigen_tau_n.array())).matrix())/2.0;
398 
399  // compute nlZ=nlZ_part1+nlZ_part2+nlZ_part3
400  m_nlZ=nlZ_part1+nlZ_part2+nlZ_part3;
401 }
402 
404 {
405  // create eigen representation of L, sstau, alpha
406  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
407  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
408  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
409 
410  // create shogun and eigen representation of F
412  Map<MatrixXd> eigen_F(m_F.matrix, m_F.num_rows, m_F.num_cols);
413 
414  // solve L*L^T * V = diag(sqrt(ttau))
415  MatrixXd V=eigen_L.triangularView<Upper>().adjoint().solve(
416  MatrixXd(eigen_sttau.asDiagonal()));
417  V=eigen_L.triangularView<Upper>().solve(V);
418 
419  // compute F=alpha*alpha'-repmat(sW,1,n).*solve_chol(L,diag(sW))
420  eigen_F=eigen_alpha*eigen_alpha.adjoint()-eigen_sttau.asDiagonal()*V;
421 }
422 
424  const TParameter* param)
425 {
426  REQUIRE(!strcmp(param->m_name, "scale"), "Can't compute derivative of "
427  "the nagative log marginal likelihood wrt %s.%s parameter\n",
428  get_name(), param->m_name)
429 
430  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
431  Map<MatrixXd> eigen_F(m_F.matrix, m_F.num_rows, m_F.num_cols);
432 
433  SGVector<float64_t> result(1);
434 
435  // compute derivative wrt kernel scale: dnlZ=-sum(F.*K*scale*2)/2
436  result[0]=-(eigen_F.cwiseProduct(eigen_K)*m_scale*2.0).sum()/2.0;
437 
438  return result;
439 }
440 
442  const TParameter* param)
443 {
445  return SGVector<float64_t>();
446 }
447 
449  const TParameter* param)
450 {
451  // create eigen representation of the matrix Q
452  Map<MatrixXd> eigen_F(m_F.matrix, m_F.num_rows, m_F.num_cols);
453 
454  SGVector<float64_t> result;
455 
456  if (param->m_datatype.m_ctype==CT_VECTOR ||
457  param->m_datatype.m_ctype==CT_SGVECTOR)
458  {
460  "Length of the parameter %s should not be NULL\n", param->m_name)
461  result=SGVector<float64_t>(*(param->m_datatype.m_length_y));
462  }
463  else
464  {
465  result=SGVector<float64_t>(1);
466  }
467 
468  for (index_t i=0; i<result.vlen; i++)
469  {
471 
472  if (result.vlen==1)
473  dK=m_kernel->get_parameter_gradient(param);
474  else
475  dK=m_kernel->get_parameter_gradient(param, i);
476 
477  Map<MatrixXd> eigen_dK(dK.matrix, dK.num_rows, dK.num_cols);
478 
479  // compute derivative wrt kernel parameter: dnlZ=-sum(F.*dK*scale^2)/2.0
480  result[i]=-(eigen_F.cwiseProduct(eigen_dK)*CMath::sq(m_scale)).sum()/2.0;
481  }
482 
483  return result;
484 }
485 
487  const TParameter* param)
488 {
490  return SGVector<float64_t>();
491 }
492 
493 #endif /* HAVE_EIGEN3 */

SHOGUN Machine Learning Toolbox - Documentation