48 #include <shogun/lib/external/brent.h>
51 using namespace Eigen;
56 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59 class CMultiPsiLine :
public func_base
73 virtual double operator() (
double x)
81 (*alpha)=start_alpha+x*dalpha;
86 eigen_f.block(bl*n,0,n,1)=K*alpha->block(bl*n,0,n,1)*
CMath::sq(scale);
87 result+=alpha->block(bl*n,0,n,1).dot(eigen_f.block(bl*n,0,n,1))/2.0;
88 eigen_f.block(bl*n,0,n,1)+=eigen_m;
114 void CMultiLaplacianInferenceMethod::init()
130 "Labels must be type of CMulticlassLabels\n");
132 "likelihood model should support multi-classification\n");
177 eigen_U.block(bl*n,0,n,n)=eigen_K*
CMath::sq(
m_scale)*eigen_E.block(0,bl*n,n,n);
178 eigen_Sigma.block(bl*n,bl*n,n,n)=(MatrixXd::Identity(n,n)-eigen_U.block(bl*n,0,n,n))*(eigen_K*
CMath::sq(
m_scale));
180 MatrixXd eigen_V=eigen_M.triangularView<Upper>().adjoint().solve(eigen_U.transpose());
181 eigen_Sigma+=eigen_V.transpose()*eigen_V;
198 VectorXd max_coeff=eigen_mu_matrix.rowwise().maxCoeff();
199 eigen_dpi_matrix=eigen_mu_matrix.array().colwise()-max_coeff.array();
200 VectorXd log_sum_exp=((eigen_dpi_matrix.array().exp()).rowwise().sum()).array().log();
201 eigen_dpi_matrix=(eigen_dpi_matrix.array().colwise()-log_sum_exp.array()).exp();
220 VectorXd eigen_mean=eigen_mean_bl.replicate(C,1);
249 eigen_mu.block(bl*n,0,n,1)=eigen_ktrtr*
CMath::sq(
m_scale)*alpha.block(bl*n,0,n,1);
252 Psi_New=alpha.dot(eigen_mu)/2.0;
254 eigen_mu+=eigen_mean;
259 if (Psi_Def < Psi_New)
293 VectorXd eigen_sD=eigen_dpi.block(bl*n,0,n,1).cwiseSqrt();
294 LLT<MatrixXd> chol_tmp((eigen_sD*eigen_sD.transpose()).cwiseProduct(eigen_ktrtr*
CMath::sq(
m_scale))+
296 MatrixXd eigen_L_tmp=chol_tmp.matrixU();
297 MatrixXd eigen_E_bl=eigen_L_tmp.triangularView<Upper>().adjoint().solve(
MatrixXd(eigen_sD.asDiagonal()));
298 eigen_E_bl=eigen_E_bl.transpose()*eigen_E_bl;
299 eigen_E.block(0,bl*n,n,n)=eigen_E_bl;
304 m_nlz+=eigen_L_tmp.diagonal().array().log().sum();
307 LLT<MatrixXd> chol_tmp(eigen_M);
308 eigen_M = chol_tmp.matrixU();
309 m_nlz+=eigen_M.diagonal().array().log().sum();
311 VectorXd eigen_b=eigen_dlp;
313 tmp1=eigen_dpi.array()*(eigen_mu-eigen_mean).array();
315 VectorXd tmp2=m_tmp.array().rowwise().sum();
318 eigen_b.block(bl*n,0,n,1)+=eigen_dpi.block(bl*n,0,n,1).cwiseProduct(eigen_mu.block(bl*n,0,n,1)-eigen_mean_bl-tmp2);
322 eigen_c.block(bl*n,0,n,1)=eigen_E.block(0,bl*n,n,n)*(eigen_ktrtr*
CMath::sq(
m_scale)*eigen_b.block(bl*n,0,n,1));
326 VectorXd tmp3=c_tmp.array().rowwise().sum();
327 VectorXd tmp4=eigen_M.triangularView<Upper>().adjoint().solve(tmp3);
329 VectorXd &eigen_dalpha=eigen_b;
330 eigen_dalpha+=eigen_E.transpose()*(eigen_M.triangularView<Upper>().solve(tmp4))-eigen_c-eigen_alpha;
337 func.dalpha=eigen_dalpha;
338 func.start_alpha=eigen_alpha;
339 func.alpha=&eigen_alpha;
358 Map<MatrixXd> eigen_U(m_U.matrix, m_U.num_rows, m_U.num_cols);
361 eigen_U=eigen_M.triangularView<Upper>().adjoint().solve(eigen_E);
370 Map<MatrixXd> eigen_U(m_U.matrix, m_U.num_rows, m_U.num_cols);
377 result+=((eigen_E.block(0,bl*n,n,n)-eigen_U.block(0,bl*n,n,n).transpose()*eigen_U.block(0,bl*n,n,n)).array()
378 *eigen_dK.array()).
sum();
379 result-=(eigen_dK*eigen_alpha.block(bl*n,0,n,1)).
dot(eigen_alpha.block(bl*n,0,n,1));
388 REQUIRE(!strcmp(param->
m_name,
"scale"),
"Can't compute derivative of "
389 "the nagative log marginal likelihood wrt %s.%s parameter\n",
397 Map<
MatrixXd> eigen_dK(dK.matrix, dK.num_rows, dK.num_cols);
415 if (param->m_datatype.m_ctype==CT_VECTOR ||
416 param->m_datatype.m_ctype==CT_SGVECTOR)
418 REQUIRE(param->m_datatype.m_length_y,
419 "Length of the parameter %s should not be NULL\n", param->m_name)
460 "Length of the parameter %s should not be NULL\n", param->
m_name)
482 result[i]-=eigen_alpha.block(bl*n,0,n,1).
dot(eigen_dmu);
virtual SGVector< float64_t > get_log_probability_f(const CLabels *lab, SGVector< float64_t > func) const =0
SGVector< float64_t > m_dlp
virtual bool supports_multiclass() const
virtual ELabelType get_label_type() const =0
virtual void update_alpha()=0
SGVector< float64_t > m_alpha
virtual const char * get_name() const
virtual void update_approx_cov()=0
Vector::Scalar dot(Vector a, Vector b)
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
multi-class labels 0,1,...
virtual float64_t get_negative_log_marginal_likelihood()
The Laplace approximation inference method base class.
CMultiLaplacianInferenceMethod()
virtual SGVector< float64_t > get_mean_vector(const CFeatures *features) const =0
An abstract class of the mean function.
virtual SGVector< float64_t > get_derivative_wrt_likelihood_model(const TParameter *param)=0
Multiclass Labels for multi-class classification.
virtual void get_dpi_helper()
SGMatrix< float64_t > m_L
SGMatrix< float64_t > m_E
Matrix::Scalar sum(Matrix m, bool no_diag=false)
SGVector< float64_t > m_mu
The Laplace approximation inference method class for multi classification.
virtual ~CMultiLaplacianInferenceMethod()
static T sum(T *vec, int32_t len)
Return sum(vec)
virtual SGVector< float64_t > get_derivative_wrt_inference_method(const TParameter *param)=0
Matrix< float64_t,-1,-1, 0,-1,-1 > MatrixXd
virtual SGVector< float64_t > get_derivative_wrt_kernel(const TParameter *param)=0
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
virtual SGVector< float64_t > get_parameter_derivative(const CFeatures *features, const TParameter *param, index_t index=-1)
all of classes and functions are contained in the shogun namespace
virtual SGVector< float64_t > get_derivative_wrt_mean(const TParameter *param)=0
The class Features is the base class of all feature objects.
float64_t m_opt_tolerance
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
virtual void update_chol()=0
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
virtual void check_members() const
virtual SGVector< float64_t > get_log_probability_derivative_f(const CLabels *lab, SGVector< float64_t > func, index_t i) const =0
SGVector< float64_t > m_W
SGMatrix< float64_t > m_Sigma
virtual void update_deriv()=0
virtual bool parameter_hash_changed()
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr
CLikelihoodModel * m_model
static void * get_derivative_helper(void *p)