SHOGUN  3.2.1
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
KLCovarianceInferenceMethod.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  * Nickisch, Hannes, and Carl Edward Rasmussen.
36  * "Approximations for Binary Gaussian Process Classification."
37  * Journal of Machine Learning Research 9.10 (2008).
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 CKLCovarianceInferenceMethod::CKLCovarianceInferenceMethod() : CKLInferenceMethod()
55 {
56  init();
57 }
58 
60  CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod)
61  : CKLInferenceMethod(kern, feat, m, lab, mod)
62 {
63  init();
64 }
65 
66 void CKLCovarianceInferenceMethod::init()
67 {
68  SG_ADD(&m_V, "V",
69  "V is L'*V=diag(sW)*K",
71  SG_ADD(&m_A, "A",
72  "A is A=I-K*diag(sW)*inv(L)'*inv(L)*diag(sW)",
74  SG_ADD(&m_W, "W",
75  "noise matrix W",
77  SG_ADD(&m_sW, "sW",
78  "Square root of noise matrix W",
80  SG_ADD(&m_dv, "dv",
81  "the gradient of the variational expection wrt sigma2",
83  SG_ADD(&m_df, "df",
84  "the gradient of the variational expection wrt mu",
86 }
87 
88 
90 {
99  update();
100 
101  index_t len=m_alpha.vlen/2;
102  SGVector<float64_t> result(len);
103 
104  Map<VectorXd> eigen_result(result.vector, len);
105  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
106 
107  eigen_result=eigen_alpha;
108 
109  return result;
110 }
111 
113 {
114 }
115 
117 {
119  Map<VectorXd> eigen_mean(mean.vector, mean.vlen);
120 
121  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
122 
123  index_t len=m_alpha.vlen/2;
124  //construct mu
125  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
126 
127  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
128  //mu=K*alpha+m
129  eigen_mu=eigen_K*CMath::sq(m_scale)*eigen_alpha+eigen_mean;
130 
131  //construct s2
132  Map<VectorXd> eigen_log_neg_lambda(m_alpha.vector+len, len);
133 
134  Map<VectorXd> eigen_W(m_W.vector, m_W.vlen);
135  eigen_W=(2.0*eigen_log_neg_lambda.array().exp()).matrix();
136 
137  Map<VectorXd> eigen_sW(m_sW.vector, m_sW.vlen);
138  eigen_sW=eigen_W.array().sqrt().matrix();
139 
141  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
142 
143  //solve L'*V=diag(sW)*K
144  Map<MatrixXd> eigen_V(m_V.matrix, m_V.num_rows, m_V.num_cols);
145  eigen_V=eigen_L.triangularView<Upper>().adjoint().solve(eigen_sW.asDiagonal()*eigen_K*CMath::sq(m_scale));
146  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
147  //Sigma=inv(inv(K)-2*diag(lambda))=K-K*diag(sW)*inv(L)'*inv(L)*diag(sW)*K
148  //v=abs(diag(Sigma))
149  eigen_s2=(eigen_K.diagonal().array()*CMath::sq(m_scale)-(eigen_V.array().pow(2).colwise().sum().transpose())).abs().matrix();
150 
153 }
154 
156 {
157  REQUIRE(gradient.vlen==m_alpha.vlen,
158  "The length of gradients (%d) should the same as the length of parameters (%d)\n",
159  gradient.vlen, m_alpha.vlen);
160 
161  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
162  Map<VectorXd> eigen_sW(m_sW.vector, m_sW.vlen);
163  Map<MatrixXd> eigen_V(m_V.matrix, m_V.num_rows, m_V.num_cols);
164  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
165  Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
166 
167  index_t len=m_alpha.vlen/2;
168  Map<VectorXd> eigen_alpha(m_alpha.vector, len);
169  Map<VectorXd> eigen_log_neg_lambda(m_alpha.vector+len, len);
170 
173 
174  //[a,df,dV] = a_related2(mu,s2,y,lik);
175  TParameter* s2_param=lik->m_parameters->get_parameter("sigma2");
176  m_dv=lik->get_variational_first_derivative(s2_param);
177  Map<VectorXd> eigen_dv(m_dv.vector, m_dv.vlen);
178 
179  TParameter* mu_param=lik->m_parameters->get_parameter("mu");
180  m_df=lik->get_variational_first_derivative(mu_param);
181  Map<VectorXd> eigen_df(m_df.vector, m_df.vlen);
182  //U=inv(L')*diag(sW)
183  MatrixXd eigen_U=eigen_L.triangularView<Upper>().adjoint().solve(MatrixXd(eigen_sW.asDiagonal()));
184  Map<MatrixXd> eigen_A(m_A.matrix, m_A.num_rows, m_A.num_cols);
185  // A=I-K*diag(sW)*inv(L)*inv(L')*diag(sW)
186  eigen_A=MatrixXd::Identity(len, len)-eigen_V.transpose()*eigen_U;
187 
189  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
190 
191  Map<VectorXd> eigen_dnlz_alpha(gradient.vector, len);
192  Map<VectorXd> eigen_dnlz_log_neg_lambda(gradient.vector+len, len);
193 
194  //dlZ_alpha = K*(df-alpha);
195  eigen_dnlz_alpha=eigen_K*CMath::sq(m_scale)*(-eigen_df+eigen_alpha);
196 
197  //dlZ_lambda = 2*(Sigma.*Sigma)*dV +v -sum(Sigma.*A,2); % => fast diag(V*VinvK')
198  //dlZ_log_neg_lambda = dlZ_lambda .* lambda;
199  //dnlZ = -[dlZ_alpha; dlZ_log_neg_lambda];
200  eigen_dnlz_log_neg_lambda=(eigen_Sigma.array().pow(2)*2.0).matrix()*eigen_dv+eigen_s2;
201  eigen_dnlz_log_neg_lambda=eigen_dnlz_log_neg_lambda-(eigen_Sigma.array()*eigen_A.array()).rowwise().sum().matrix();
202  eigen_dnlz_log_neg_lambda=(eigen_log_neg_lambda.array().exp()*eigen_dnlz_log_neg_lambda.array()).matrix();
203 }
204 
205 
207 {
208  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
209  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen/2);
210  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
211  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
212  //get mean vector and create eigen representation of it
214  Map<VectorXd> eigen_mean(mean.vector, mean.vlen);
215 
218 
219  float64_t trace=0;
220  //L_inv=L\eye(n);
221  //trace(L_inv'*L_inv) %V*inv(K)
222  MatrixXd eigen_t=eigen_L.triangularView<Upper>().adjoint().solve(MatrixXd::Identity(eigen_L.rows(),eigen_L.cols()));
223 
224  for(index_t idx=0; idx<eigen_t.rows(); idx++)
225  trace +=(eigen_t.col(idx).array().pow(2)).sum();
226 
227  //nlZ = -a -logdet(V*inv(K))/2 -n/2 +(alpha'*K*alpha)/2 +trace(V*inv(K))/2;
228  float64_t result=-a+eigen_L.diagonal().array().log().sum();
229  result+=0.5*(-eigen_K.rows()+eigen_alpha.dot(eigen_mu-eigen_mean)+trace);
230  return result;
231 }
232 
234 {
235  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
236  Map<VectorXd> eigen_W(m_W.vector, m_W.vlen);
237  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
238  Map<VectorXd> eigen_sW(m_sW.vector, m_sW.vlen);
239  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols);
240  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen/2);
241  Map<MatrixXd> eigen_A(m_A.matrix, m_A.num_rows, m_A.num_cols);
242 
243  Map<VectorXd> eigen_dv(m_dv.vector, m_dv.vlen);
244  Map<VectorXd> eigen_df(m_df.vector, m_df.vlen);
245 
246  //AdK = A*dK;
247  MatrixXd AdK=eigen_A*eigen_dK;
248 
249  //z = diag(AdK) + sum(A.*AdK,2) - sum(A'.*AdK,1)';
250  VectorXd z=AdK.diagonal()+(eigen_A.array()*AdK.array()).rowwise().sum().matrix()
251  -(eigen_A.transpose().array()*AdK.array()).colwise().sum().transpose().matrix();
252 
253  //dnlZ(j) = alpha'*dK*(alpha/2-df) - z'*dv;
254  return eigen_alpha.dot(eigen_dK*(eigen_alpha/2.0-eigen_df))-z.dot(eigen_dv);
255 }
256 
258 {
259  float64_t nlml_new=0;
260  float64_t nlml_def=0;
261 
262  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
263 
264  if (m_alpha.vlen == m_labels->get_num_labels()*2)
265  {
267 
268  float64_t trace=0;
269  LLT<MatrixXd> llt((eigen_K*CMath::sq(m_scale))+
270  MatrixXd::Identity(eigen_K.rows(), eigen_K.cols()));
271  MatrixXd LL=llt.matrixU();
272  MatrixXd tt=LL.triangularView<Upper>().adjoint().solve(MatrixXd::Identity(LL.rows(),LL.cols()));
273 
274  for(index_t idx=0; idx<tt.rows(); idx++)
275  trace+=(tt.col(idx).array().pow(2)).sum();
276 
277  MatrixXd eigen_V=LL.triangularView<Upper>().adjoint().solve(eigen_K*CMath::sq(m_scale));
278  SGVector<float64_t> s2_tmp(m_s2.vlen);
279  Map<VectorXd> eigen_s2(s2_tmp.vector, s2_tmp.vlen);
280  eigen_s2=(eigen_K.diagonal().array()*CMath::sq(m_scale)-(eigen_V.array().pow(2).colwise().sum().transpose())).abs().matrix();
282 
284  lik->set_variational_distribution(mean, s2_tmp, m_labels);
286 
287  nlml_def=-a+LL.diagonal().array().log().sum();
288  nlml_def+=0.5*(-eigen_K.rows()+trace);
289 
290  if (nlml_new<=nlml_def)
292  }
293 
294  if (m_alpha.vlen != m_labels->get_num_labels()*2 || nlml_def<nlml_new)
295  {
296  if(m_alpha.vlen != m_labels->get_num_labels()*2)
298 
299  //init
300  for (index_t i=0; i<m_alpha.vlen; i++)
301  {
302  if (i<m_alpha.vlen/2)
303  m_alpha[i]=0;
304  else
305  m_alpha[i]=CMath::log(0.5);
306  }
307 
308  index_t len=m_alpha.vlen/2;
309  m_W=SGVector<float64_t>(len);
310  m_sW=SGVector<float64_t>(len);
313  m_V=SGMatrix<float64_t>(len, len);
314  m_Sigma=SGMatrix<float64_t>(len, len);
315  m_A=SGMatrix<float64_t>(len, len);
316  }
317 
318  nlml_new=lbfgs_optimization();
319 }
320 
322 {
324  update();
325 
326  return SGVector<float64_t>(m_sW);
327 }
328 
330 {
334 }
335 
337 {
341 }
342 
344 {
350 }
351 
352 } /* namespace shogun */
353 
354 #endif /* HAVE_EIGEN3 */
SGVector< float64_t > m_alpha
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const =0
static SGMatrix< float64_t > get_choleksy(SGVector< float64_t > W, SGVector< float64_t > sW, SGMatrix< float64_t > kernel, float64_t scale)
int32_t index_t
Definition: common.h:62
virtual void get_gradient_of_nlml_wrt_parameters(SGVector< float64_t > gradient)
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:507
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
static SGMatrix< float64_t > get_inverse(SGMatrix< float64_t > L, SGMatrix< float64_t > kernel, SGVector< float64_t > sW, SGMatrix< float64_t > V, float64_t scale)
void log()
natural logarithm of vector elements
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
virtual SGVector< float64_t > get_mean_vector(const CFeatures *features) const =0
An abstract class of the mean function.
Definition: MeanFunction.h:28
virtual void set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual float64_t get_derivative_related_cov(Eigen::MatrixXd eigen_dK)
SGMatrix< float64_t > m_Sigma
SGMatrix< float64_t > m_L
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 KL approximation inference method class.
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
static float64_t log(float64_t v)
Definition: Math.h:505
virtual SGVector< float64_t > get_diagonal_vector()
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 机器学习工具包 - 项目文档