50 using namespace Eigen;
74 SG_SERROR(
"Provided inference is not of type CKLDualInferenceMethod!\n")
97 "The provided likelihood model is not a variational dual Likelihood model.\n");
113 void CKLDualInferenceMethod::init()
119 "Square root of noise matrix W",
122 "the gradient of the variational expection wrt sigma2",
125 "the gradient of the variational expection wrt mu",
127 SG_ADD(&m_is_dual_valid,
"is_dual_valid",
128 "whether the lambda (m_W) is valid or not",
130 m_is_dual_valid=
false;
142 if (!m_is_dual_valid)
148 eigen_alpha=-eigen_alpha;
151 eigen_sW=eigen_W.array().sqrt().matrix();
158 eigen_V=eigen_L.triangularView<Upper>().adjoint().solve(eigen_sW.asDiagonal()*eigen_K*
CMath::exp(
m_log_scale*2.0));
162 eigen_s2=(eigen_K.diagonal().array()*
CMath::exp(
m_log_scale*2.0)-(eigen_V.array().pow(2).colwise().sum().transpose())).abs().matrix();
175 if (!m_is_dual_valid)
187 float64_t result=0.5*eigen_alpha.dot(eigen_mu-eigen_mean)+a;
188 result+=eigen_mean.dot(eigen_alpha);
189 result-=eigen_L.diagonal().array().log().sum();
197 "The length of gradients (%d) should the same as the length of parameters (%d)\n",
200 if (!m_is_dual_valid)
214 eigen_gradient=-eigen_mu-0.5*eigen_s2+eigen_d_lambda;
237 MatrixXd eigen_t=eigen_L.triangularView<Upper>().adjoint().solve(MatrixXd::Identity(eigen_L.rows(),eigen_L.cols()));
239 for(
index_t idx=0; idx<eigen_t.rows(); idx++)
240 trace +=(eigen_t.col(idx).array().pow(2)).sum();
243 float64_t result=-a+eigen_L.diagonal().array().log().
sum();
245 result+=0.5*(-eigen_K.rows()+eigen_alpha.dot(eigen_mu-eigen_mean)+trace);
273 MatrixXd eigen_U=eigen_L.triangularView<Upper>().adjoint().solve(
MatrixXd(eigen_sW.asDiagonal()));
276 MatrixXd eigen_A=MatrixXd::Identity(len, len)-eigen_V.transpose()*eigen_U;
282 VectorXd z=AdK.diagonal()+(eigen_A.array()*AdK.array()).rowwise().sum().matrix()
283 -(eigen_A.transpose().array()*AdK.array()).colwise().sum().transpose().matrix();
285 float64_t result=eigen_alpha.dot(eigen_dK*(eigen_alpha/2.0-eigen_df))-z.dot(eigen_dv);
307 eigen_sW=eigen_W.array().sqrt().matrix();
316 eigen_alpha=-eigen_alpha;
328 eigen_s2=(eigen_K.diagonal().array()*
CMath::exp(
m_log_scale*2.0)-(eigen_V.array().pow(2).colwise().sum().transpose())).abs().matrix();
332 nlml_def=get_nlml_wrapper(alpha_tmp, mu_tmp, L_tmp);
334 if (nlml_new<=nlml_def)
368 float64_t CKLDualInferenceMethod::adjust_step(
void *obj,
396 bool status=obj_prt->lbfgs_precompute();
399 float64_t nlml=obj_prt->get_dual_objective_wrt_parameters();
401 SGVector<float64_t> sg_gradient(gradient, dim,
false);
403 obj_prt->get_gradient_of_dual_objective_wrt_parameters(sg_gradient);
431 void * obj_prt =
static_cast<void *
>(
this);
435 CKLDualInferenceMethod::evaluate,
436 NULL, obj_prt, &lbfgs_param, CKLDualInferenceMethod::adjust_step);
float64_t m_orthantwise_c
virtual CDualVariationalGaussianLikelihood * get_dual_variational_likelihood() const
SGVector< float64_t > m_alpha
int32_t lbfgs(int32_t n, float64_t *x, float64_t *ptr_fx, lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress, void *instance, lbfgs_parameter_t *_param, lbfgs_adjust_step_t proc_adjust_step)
The Inference Method base class.
virtual void set_dual_parameters(SGVector< float64_t > the_lambda, const CLabels *lab)
static SGMatrix< float64_t > get_choleksy(SGVector< float64_t > W, SGVector< float64_t > sW, SGMatrix< float64_t > kernel, float64_t scale)
virtual SGVector< float64_t > get_mu_dual_parameter() const =0
The class Labels models labels, i.e. class assignments of objects.
static const float64_t INFTY
infinity
virtual int32_t get_num_labels() const =0
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)
virtual bool dual_parameters_valid() const
TParameter * get_parameter(int32_t idx)
virtual SGVector< float64_t > get_dual_objective_value()=0
virtual SGVector< float64_t > get_mean_vector(const CFeatures *features) const =0
An abstract class of the mean function.
virtual float64_t adjust_step_wrt_dual_parameter(SGVector< float64_t > direction, const float64_t step) const
virtual float64_t lbfgs_optimization()
SGMatrix< float64_t > m_Sigma
virtual void check_dual_inference(CLikelihoodModel *mod) const
The dual KL approximation inference method class.
virtual void update_chol()
SGMatrix< float64_t > m_L
void set_model(CLikelihoodModel *mod)
virtual void get_gradient_of_dual_objective_wrt_parameters(SGVector< float64_t > gradient)
virtual void update_approx_cov()
virtual SGVector< float64_t > get_alpha()
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual SGVector< float64_t > get_dual_first_derivative(const TParameter *param) const =0
static T sum(T *vec, int32_t len)
Return sum(vec)
virtual void update_deriv()
Matrix< float64_t,-1,-1, 0,-1,-1 > MatrixXd
The KL approximation inference method class.
virtual float64_t get_derivative_related_cov(SGMatrix< float64_t > dK)
virtual SGVector< float64_t > get_variational_expection()
all of classes and functions are contained in the shogun namespace
The class Features is the base class of all feature objects.
static float64_t exp(float64_t x)
SGVector< float64_t > m_mu
SGVector< float64_t > m_s2
static CKLDualInferenceMethod * obtain_from_generic(CInferenceMethod *inference)
virtual EInferenceType get_inference_type() const
Binary Labels for binary classification.
virtual float64_t get_dual_objective_wrt_parameters()
virtual void update_alpha()
virtual float64_t get_negative_log_marginal_likelihood_helper()
virtual bool lbfgs_precompute()
virtual SGVector< float64_t > get_diagonal_vector()
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const
virtual void set_model(CLikelihoodModel *mod)
virtual bool parameter_hash_changed()
Class that models dual variational likelihood.
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr
static const float64_t NOT_A_NUMBER
not a number
CLikelihoodModel * m_model
virtual ~CKLDualInferenceMethod()