bool CLinearRidgeRegression::train_machine(CFeatures* data)
{
    REQUIRE(m_labels,"No labels set\n");

    if (!data)
    	data=features;

    REQUIRE(data,"No features provided and no featured previously set\n");

    REQUIRE(m_labels->get_num_labels() == data->get_num_vectors(),
    	"Number of training vectors (%d) does not match number of labels (%d)\n",
    	m_labels->get_num_labels(), data->get_num_vectors());

    REQUIRE(data->get_feature_class() == C_DENSE,
    	"Expected Dense Features (%d) but got (%d)\n",
    	C_DENSE, data->get_feature_class());

    REQUIRE(data->get_feature_type() == F_DREAL,
    	"Expected Real Features (%d) but got (%d)\n",
    	F_DREAL, data->get_feature_type());

    CDenseFeatures<float64_t>* feats=(CDenseFeatures<float64_t>*) data;
    int32_t num_feat=feats->get_num_features();
    int32_t num_vec=feats->get_num_vectors();

    SGMatrix<float64_t> kernel_matrix(num_feat,num_feat);
    SGMatrix<float64_t> feats_matrix(feats->get_feature_matrix());
    SGVector<float64_t> y(num_feat);
    SGVector<float64_t> tau_vector(num_feat);

    tau_vector.zero();
    tau_vector.add(m_tau);

    Map<MatrixXd> eigen_kernel_matrix(kernel_matrix.matrix, num_feat,num_feat);
    Map<MatrixXd> eigen_feats_matrix(feats_matrix.matrix, num_feat,num_vec);
    Map<VectorXd> eigen_y(y.vector, num_feat);
    Map<VectorXd> eigen_labels(((CRegressionLabels*)m_labels)->get_labels(),num_vec);
    Map<VectorXd> eigen_tau(tau_vector.vector, num_feat);

    eigen_kernel_matrix = eigen_feats_matrix*eigen_feats_matrix.transpose();

    eigen_kernel_matrix.diagonal() += eigen_tau;

    eigen_y = eigen_feats_matrix*eigen_labels ;

    LLT<MatrixXd> llt;
    llt.compute(eigen_kernel_matrix);
    if(llt.info() != Eigen::Success)
    {
    	SG_WARNING("Features covariance matrix was not positive definite\n");
    	return false;
    }
    eigen_y = llt.solve(eigen_y);

    set_w(y);
    return true;
}
Beispiel #2
0
bool CKernelRidgeRegression::solve_krr_system()
{
	SGMatrix<float64_t> kernel_matrix(kernel->get_kernel_matrix());
	int32_t n = kernel_matrix.num_rows;
	SGVector<float64_t> y = ((CRegressionLabels*)m_labels)->get_labels();

	for(index_t i=0; i<n; i++)
		kernel_matrix(i,i) += m_tau;

	Map<MatrixXd> eigen_kernel_matrix(kernel_matrix.matrix, n, n);
	Map<VectorXd> eigen_alphas(m_alpha.vector, n);
	Map<VectorXd> eigen_y(y.vector, n);

	LLT<MatrixXd> llt;
	llt.compute(eigen_kernel_matrix);
	if (llt.info() != Eigen::Success)
	{
		SG_WARNING("Features covariance matrix was not positive definite\n");
		return false;
	}
	eigen_alphas = llt.solve(eigen_y);
	return true;
}