41 using namespace Eigen;
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 class KLInferenceCostFunction:
public FirstOrderCostFunction
50 KLInferenceCostFunction():FirstOrderCostFunction() { init(); }
51 virtual ~KLInferenceCostFunction() {
SG_UNREF(m_obj); }
52 void set_target(CKLInference *obj)
62 void unset_target(
bool is_unref)
73 REQUIRE(m_obj,
"Object not set\n");
74 bool status = m_obj->precompute();
76 return CMath::NOT_A_NUMBER;
77 float64_t nlml=m_obj->get_nlml_wrt_parameters();
81 virtual SGVector<float64_t> obtain_variable_reference()
83 REQUIRE(m_obj,
"Object not set\n");
84 m_derivatives = SGVector<float64_t>((m_obj->m_alpha).vlen);
85 return m_obj->m_alpha;
87 virtual SGVector<float64_t> get_gradient()
89 REQUIRE(m_obj,
"Object not set\n");
90 m_obj->get_gradient_of_nlml_wrt_parameters(m_derivatives);
94 virtual const char* get_name()
const {
return "KLInferenceCostFunction"; }
96 SGVector<float64_t> m_derivatives;
100 m_derivatives = SGVector<float64_t>();
101 SG_ADD(&m_derivatives,
"KLInferenceCostFunction__m_derivatives",
103 SG_ADD((CSGObject **)&m_obj,
"KLInferenceCostFunction__m_obj",
108 #endif //DOXYGEN_SHOULD_SKIP_THIS
125 REQUIRE(mod,
"the likelihood model must not be NULL\n")
128 "The provided likelihood model (%s) must support variational Gaussian inference. ",
129 "Please use a Variational Gaussian Likelihood model\n",
139 void CKLInference::init()
146 "The noise factor used for correcting Kernel matrix",
149 "The exponential factor used for increasing noise_factor",
152 "The max number of attempt to correct Kernel matrix",
155 "The minimum coeefficient of kernel matrix in LDLT factorization used to check whether the kernel matrix is positive definite or not",
158 "Variational parameter sigma2",
161 "Variational parameter mu and posterior mean",
164 "Posterior covariance matrix Sigma",
202 REQUIRE(noise_factor>=0,
"The noise_factor %.20f should be non-negative\n", noise_factor);
208 REQUIRE(min_coeff_kernel>=0,
"The min_coeff_kernel %.20f should be non-negative\n", min_coeff_kernel);
214 REQUIRE(max_attempt>=0,
"The max_attempt %d should be non-negative. 0 means inifity attempts\n", max_attempt);
220 REQUIRE(exp_factor>1.0,
"The exp_factor %f should be greater than 1.0.\n", exp_factor);
245 SG_ERROR(
"The Kernel matrix is highly non-positive definite since the min_coeff_kernel is less than %.20f",
246 " even when adding %.20f noise to the diagonal elements at max %d attempts\n",
254 Kernel_D=ldlt.vectorD();
308 result[0]=-eigen_lp_dhyp.sum();
318 REQUIRE(param,
"Param not set\n");
320 int64_t len=
const_cast<TParameter *
>(param)->m_datatype.get_num_elements();
336 result[i]=-eigen_alpha.dot(eigen_dmu);
345 cost_fun->set_target(
this);
347 if(this->ref_count()>1)
352 REQUIRE(opt,
"FirstOrderMinimizer is required\n")
357 cost_fun->unset_target(cleanup);
365 REQUIRE(minimizer,
"Minimizer must set\n");
367 REQUIRE(opt,
"FirstOrderMinimizer is required\n");
374 REQUIRE(!strcmp(param->
m_name,
"log_scale"),
"Can't compute derivative of "
375 "the nagative log marginal likelihood wrt %s.%s parameter\n",
390 REQUIRE(param,
"Param not set\n");
392 int64_t len=
const_cast<TParameter *
>(param)->m_datatype.get_num_elements();
virtual const char * get_name() const =0
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
virtual float64_t optimization()
virtual void set_min_coeff_kernel(float64_t min_coeff_kernel)
virtual void update_parameter_hash()
virtual SGVector< float64_t > get_first_derivative_wrt_hyperparameter(const TParameter *param) const =0
virtual SGVector< float64_t > get_derivative_wrt_kernel(const TParameter *param)
virtual void set_max_attempt(index_t max_attempt)
The class Labels models labels, i.e. class assignments of objects.
virtual CVariationalGaussianLikelihood * get_variational_likelihood() const
virtual SGVector< float64_t > get_derivative_wrt_mean(const TParameter *param)
virtual void update_alpha()=0
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
virtual void update_chol()=0
virtual void set_model(CLikelihoodModel *mod)
An abstract class of the mean function.
virtual float64_t get_negative_log_marginal_likelihood_helper()=0
virtual void register_minimizer(Minimizer *minimizer)
The class wraps the Shogun's C-style LBFGS minimizer.
virtual Eigen::LDLT< Eigen::MatrixXd, 0x1 > update_init_helper()
friend class KLInferenceCostFunction
SGMatrix< float64_t > m_ktrtr
virtual void update_deriv()=0
virtual void check_variational_likelihood(CLikelihoodModel *mod) const
float64_t m_min_coeff_kernel
virtual SGVector< float64_t > get_derivative_wrt_inference_method(const TParameter *param)
virtual SGMatrix< float64_t > get_posterior_covariance()
virtual void set_noise_factor(float64_t noise_factor)
virtual SGVector< float64_t > get_posterior_mean()
virtual void unset_cost_function(bool is_unref=true)
virtual float64_t get_derivative_related_cov(SGMatrix< float64_t > dK)=0
virtual void compute_gradient()
virtual void set_model(CLikelihoodModel *mod)
SGVector< float64_t > m_mu
SGMatrix< float64_t > m_L
virtual SGVector< float64_t > get_derivative_wrt_likelihood_model(const TParameter *param)
virtual const char * get_name() const
virtual float64_t minimize()=0
SGMatrix< float64_t > m_Sigma
virtual void update_init()
virtual SGMatrix< float64_t > get_cholesky()
virtual void register_minimizer(Minimizer *minimizer)
SGVector< float64_t > m_s2
virtual SGVector< float64_t > get_parameter_derivative(const CFeatures *features, const TParameter *param, index_t index=-1)
virtual float64_t get_nlml_wrt_parameters()
all of classes and functions are contained in the shogun namespace
The Inference Method base class.
The class Features is the base class of all feature objects.
static float64_t exp(float64_t x)
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
The Variational Likelihood base class.
virtual float64_t get_negative_log_marginal_likelihood()
virtual void update_approx_cov()=0
The minimizer base class.
virtual void set_exp_factor(float64_t exp_factor)
virtual bool supports_derivative_wrt_hyperparameter() const =0
CLikelihoodModel * m_model
virtual void compute_gradient()
virtual bool parameter_hash_changed()
The Likelihood model base class.
virtual void set_cost_function(FirstOrderCostFunction *fun)
SGVector< float64_t > m_alpha
The first order minimizer base class.