SHOGUN  v3.0.0
 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=8;
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 {
101  if (update_parameter_hash())
102  update();
103 
104  return SGVector<float64_t>(m_mu);
105 }
106 
108 {
109  if (update_parameter_hash())
110  update();
111 
112  return SGMatrix<float64_t>(m_Sigma);
113 }
114 
116 {
117  // update kernel and feature matrix
119 
120  // get number of labels (trainig examples)
122 
123  // try to use tilde values from previous call
124  if (m_ttau.vlen==n)
125  {
126  update_chol();
130  }
131 
132  // get mean vector
134 
135  // get and scale diagonal of the kernel matrix
137  ktrtr_diag.scale(CMath::sq(m_scale));
138 
139  // marginal likelihood for ttau = tnu = 0
141  mean, ktrtr_diag, m_labels));
142 
143  // use zero values if we have no better guess or it's better
144  if (m_ttau.vlen!=n || m_nlZ>nlZ0)
145  {
146  CREATE_SGVECTOR(m_ttau, n, float64_t);
147  m_ttau.zero();
148 
149  CREATE_SGVECTOR(m_sttau, n, float64_t);
150  m_sttau.zero();
151 
152  CREATE_SGVECTOR(m_tnu, n, float64_t);
153  m_tnu.zero();
154 
156 
157  // copy data manually, since we don't have appropriate method
158  for (index_t i=0; i<m_ktrtr.num_rows; i++)
159  for (index_t j=0; j<m_ktrtr.num_cols; j++)
160  m_Sigma(i,j)=m_ktrtr(i,j)*CMath::sq(m_scale);
161 
162  CREATE_SGVECTOR(m_mu, n, float64_t);
163  m_mu.zero();
164 
165  // set marginal likelihood
166  m_nlZ=nlZ0;
167  }
168 
169  // create vector of the random permutation
171 
172  // cavity tau and nu vectors
173  SGVector<float64_t> tau_n(n);
174  SGVector<float64_t> nu_n(n);
175 
176  // cavity mu and s2 vectors
177  SGVector<float64_t> mu_n(n);
178  SGVector<float64_t> s2_n(n);
179 
180  float64_t nlZ_old=CMath::INFTY;
181  uint32_t sweep=0;
182 
183  while ((CMath::abs(m_nlZ-nlZ_old)>m_tol && sweep<m_max_sweep) ||
184  sweep<m_min_sweep)
185  {
186  nlZ_old=m_nlZ;
187  sweep++;
188 
189  // shuffle random permutation
190  randperm.permute();
191 
192  for (index_t j=0; j<n; j++)
193  {
194  index_t i=randperm[j];
195 
196  // find cavity paramters
197  tau_n[i]=1.0/m_Sigma(i,i)-m_ttau[i];
198  nu_n[i]=m_mu[i]/m_Sigma(i,i)+mean[i]*tau_n[i]-m_tnu[i];
199 
200  // compute cavity mean and variance
201  mu_n[i]=nu_n[i]/tau_n[i];
202  s2_n[i]=1.0/tau_n[i];
203 
204  // get moments
205  float64_t mu=m_model->get_first_moment(mu_n, s2_n, m_labels, i);
206  float64_t s2=m_model->get_second_moment(mu_n, s2_n, m_labels, i);
207 
208  // save old value of ttau
209  float64_t ttau_old=m_ttau[i];
210 
211  // compute ttau and sqrt(ttau)
212  m_ttau[i]=CMath::max(1.0/s2-tau_n[i], 0.0);
213  m_sttau[i]=CMath::sqrt(m_ttau[i]);
214 
215  // compute tnu
216  m_tnu[i]=mu/s2-nu_n[i];
217 
218  // compute difference ds2=ttau_new-ttau_old
219  float64_t ds2=m_ttau[i]-ttau_old;
220 
221  // create eigen representation of Sigma, tnu and mu
222  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows,
223  m_Sigma.num_cols);
224  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
225  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
226 
227  VectorXd eigen_si=eigen_Sigma.col(i);
228 
229  // rank-1 update Sigma
230  eigen_Sigma=eigen_Sigma-ds2/(1.0+ds2*eigen_si(i))*eigen_si*
231  eigen_si.adjoint();
232 
233  // update mu
234  eigen_mu=eigen_Sigma*eigen_tnu;
235  }
236 
237  // update upper triangular factor (L^T) of Cholesky decomposition of
238  // matrix B, approximate posterior covariance and mean, negative
239  // marginal likelihood
240  update_chol();
244  }
245 
246  if (sweep==m_max_sweep)
247  SG_WARNING("Maximum number of sweeps reached")
248 
249  // update vector alpha
250  update_alpha();
251 
252  // update matrices to compute derivatives
253  update_deriv();
254 }
255 
257 {
258  // create eigen representations kernel matrix, L^T, sqrt(ttau) and tnu
259  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
260  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
261  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
262  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
263 
264  // create shogun and eigen representation of the alpha vector
266  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
267 
268  // solve LL^T * v = tS^(1/2) * K * tnu
269  VectorXd eigen_v=eigen_L.triangularView<Upper>().adjoint().solve(
270  eigen_sttau.cwiseProduct(eigen_K*CMath::sq(m_scale)*eigen_tnu));
271  eigen_v=eigen_L.triangularView<Upper>().solve(eigen_v);
272 
273  // compute alpha = (I - tS^(1/2) * B^(-1) * tS(1/2) * K) * tnu =
274  // tnu - tS(1/2) * (L^T)^(-1) * L^(-1) * tS^(1/2) * K * tnu =
275  // tnu - tS(1/2) * v
276  eigen_alpha=eigen_tnu-eigen_sttau.cwiseProduct(eigen_v);
277 }
278 
280 {
281  // create eigen representations of kernel matrix and sqrt(ttau)
282  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
283  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
284 
285  // create shogun and eigen representation of the upper triangular factor
286  // (L^T) of the Cholesky decomposition of the matrix B
288  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
289 
290  // compute upper triangular factor L^T of the Cholesky decomposion of the
291  // matrix: B = tS^(1/2) * K * tS^(1/2) + I
292  LLT<MatrixXd> eigen_chol((eigen_sttau*eigen_sttau.adjoint()).cwiseProduct(
293  eigen_K*CMath::sq(m_scale))+
294  MatrixXd::Identity(m_L.num_rows, m_L.num_cols));
295 
296  eigen_L=eigen_chol.matrixU();
297 }
298 
300 {
301  // create eigen representations of kernel matrix, L^T matrix and sqrt(ttau)
302  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
303  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
304  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
305 
306  // create shogun and eigen representation of the approximate covariance
307  // matrix
309  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
310 
311  // compute V = L^(-1) * tS^(1/2) * K, using upper triangular factor L^T
312  MatrixXd eigen_V=eigen_L.triangularView<Upper>().adjoint().solve(
313  eigen_sttau.asDiagonal()*eigen_K*CMath::sq(m_scale));
314 
315  // compute covariance matrix of the posterior:
316  // Sigma = K - K * tS^(1/2) * (L * L^T)^(-1) * tS^(1/2) * K =
317  // K - (K * tS^(1/2)) * (L^T)^(-1) * L^(-1) * tS^(1/2) * K =
318  // K - (tS^(1/2) * K)^T * (L^(-1))^T * L^(-1) * tS^(1/2) * K = K - V^T * V
319  eigen_Sigma=eigen_K*CMath::sq(m_scale)-eigen_V.adjoint()*eigen_V;
320 }
321 
323 {
324  // create eigen representation of posterior covariance matrix and tnu
325  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
326  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
327 
328  // create shogun and eigen representation of the approximate mean vector
329  CREATE_SGVECTOR(m_mu, m_tnu.vlen, float64_t);
330  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
331 
332  // compute mean vector of the approximate posterior: mu = Sigma * tnu
333  eigen_mu=eigen_Sigma*eigen_tnu;
334 }
335 
337 {
338  // create eigen representation of Sigma, L, mu, tnu, ttau
339  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
340  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
341  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
342  Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
343  Map<VectorXd> eigen_ttau(m_ttau.vector, m_ttau.vlen);
344 
345  // get and create eigen representation of the mean vector
347  Map<VectorXd> eigen_m(m.vector, m.vlen);
348 
349  // compute vector of cavity parameter tau_n
350  VectorXd eigen_tau_n=(VectorXd::Ones(m_ttau.vlen)).cwiseQuotient(
351  eigen_Sigma.diagonal())-eigen_ttau;
352 
353  // compute vector of cavity parameter nu_n
354  VectorXd eigen_nu_n=eigen_mu.cwiseQuotient(eigen_Sigma.diagonal())-
355  eigen_tnu+eigen_m.cwiseProduct(eigen_tau_n);
356 
357  // compute cavity mean: mu_n=nu_n/tau_n
358  SGVector<float64_t> mu_n(m_ttau.vlen);
359  Map<VectorXd> eigen_mu_n(mu_n.vector, mu_n.vlen);
360 
361  eigen_mu_n=eigen_nu_n.cwiseQuotient(eigen_tau_n);
362 
363  // compute cavity variance: s2_n=1.0/tau_n
364  SGVector<float64_t> s2_n(m_ttau.vlen);
365  Map<VectorXd> eigen_s2_n(s2_n.vector, s2_n.vlen);
366 
367  eigen_s2_n=(VectorXd::Ones(m_ttau.vlen)).cwiseQuotient(eigen_tau_n);
368 
370  m_model->get_log_zeroth_moments(mu_n, s2_n, m_labels));
371 
372  // compute nlZ_part1=sum(log(diag(L)))-sum(lZ)-tnu'*Sigma*tnu/2
373  float64_t nlZ_part1=eigen_L.diagonal().array().log().sum()-lZ-
374  (eigen_tnu.adjoint()*eigen_Sigma).dot(eigen_tnu)/2.0;
375 
376  // compute nlZ_part2=sum(tnu.^2./(tau_n+ttau))/2-sum(log(1+ttau./tau_n))/2
377  float64_t nlZ_part2=(eigen_tnu.array().square()/
378  (eigen_tau_n+eigen_ttau).array()).sum()/2.0-(1.0+eigen_ttau.array()/
379  eigen_tau_n.array()).log().sum()/2.0;
380 
381  // compute nlZ_part3=-(nu_n-m.*tau_n)'*((ttau./tau_n.*(nu_n-m.*tau_n)-2*tnu)
382  // ./(ttau+tau_n))/2
383  float64_t nlZ_part3=-(eigen_nu_n-eigen_m.cwiseProduct(eigen_tau_n)).dot(
384  ((eigen_ttau.array()/eigen_tau_n.array()*(eigen_nu_n.array()-
385  eigen_m.array()*eigen_tau_n.array())-2*eigen_tnu.array())/
386  (eigen_ttau.array()+eigen_tau_n.array())).matrix())/2.0;
387 
388  // compute nlZ=nlZ_part1+nlZ_part2+nlZ_part3
389  m_nlZ=nlZ_part1+nlZ_part2+nlZ_part3;
390 }
391 
393 {
394  // create eigen representation of L, sstau, alpha
395  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
396  Map<VectorXd> eigen_sttau(m_sttau.vector, m_sttau.vlen);
397  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
398 
399  // create shogun and eigen representation of F
401  Map<MatrixXd> eigen_F(m_F.matrix, m_F.num_rows, m_F.num_cols);
402 
403  // solve L*L^T * V = diag(sqrt(ttau))
404  MatrixXd V=eigen_L.triangularView<Upper>().adjoint().solve(
405  MatrixXd(eigen_sttau.asDiagonal()));
406  V=eigen_L.triangularView<Upper>().solve(V);
407 
408  // compute F=alpha*alpha'-repmat(sW,1,n).*solve_chol(L,diag(sW))
409  eigen_F=eigen_alpha*eigen_alpha.adjoint()-eigen_sttau.asDiagonal()*V;
410 }
411 
413  const TParameter* param)
414 {
415  REQUIRE(!strcmp(param->m_name, "scale"), "Can't compute derivative of "
416  "the nagative log marginal likelihood wrt %s.%s parameter\n",
417  get_name(), param->m_name)
418 
419  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
420  Map<MatrixXd> eigen_F(m_F.matrix, m_F.num_rows, m_F.num_cols);
421 
422  SGVector<float64_t> result(1);
423 
424  // compute derivative wrt kernel scale: dnlZ=-sum(F.*K*scale*2)/2
425  result[0]=-(eigen_F.cwiseProduct(eigen_K)*m_scale*2.0).sum()/2.0;
426 
427  return result;
428 }
429 
431  const TParameter* param)
432 {
434  return SGVector<float64_t>();
435 }
436 
438  const TParameter* param)
439 {
440  // create eigen representation of the matrix Q
441  Map<MatrixXd> eigen_F(m_F.matrix, m_F.num_rows, m_F.num_cols);
442 
443  SGVector<float64_t> result;
444 
445  if (param->m_datatype.m_ctype==CT_VECTOR ||
446  param->m_datatype.m_ctype==CT_SGVECTOR)
447  {
449  "Length of the parameter %s should not be NULL\n", param->m_name)
450  result=SGVector<float64_t>(*(param->m_datatype.m_length_y));
451  }
452  else
453  {
454  result=SGVector<float64_t>(1);
455  }
456 
457  for (index_t i=0; i<result.vlen; i++)
458  {
460 
461  if (result.vlen==1)
462  dK=m_kernel->get_parameter_gradient(param);
463  else
464  dK=m_kernel->get_parameter_gradient(param, i);
465 
466  Map<MatrixXd> eigen_dK(dK.matrix, dK.num_rows, dK.num_cols);
467 
468  // compute derivative wrt kernel parameter: dnlZ=-sum(F.*dK*scale^2)/2.0
469  result[i]=-(eigen_F.cwiseProduct(eigen_dK)*CMath::sq(m_scale)).sum()/2.0;
470  }
471 
472  return result;
473 }
474 
476  const TParameter* param)
477 {
479  return SGVector<float64_t>();
480 }
481 
482 #endif /* HAVE_EIGEN3 */

SHOGUN Machine Learning Toolbox - Documentation