void CFactorGraphModel::w_to_fparams(SGVector<float64_t> w) { // if nothing changed if (m_w_cache.equals(w)) return; if (m_verbose) SG_SPRINT("****** update m_w_cache!\n"); ASSERT(w.size() == m_w_cache.size()); m_w_cache = w.clone(); int32_t offset = 0; for (int32_t fi = 0; fi < m_factor_types->get_num_elements(); ++fi) { CFactorType* ftype = dynamic_cast<CFactorType*>(m_factor_types->get_element(fi)); int32_t w_dim = ftype->get_w_dim(); offset += w_dim; SGVector<float64_t> fw(w_dim); SGVector<int32_t> fw_map = get_params_mapping(ftype->get_type_id()); for (int32_t wi = 0; wi < w_dim; wi++) fw[wi] = m_w_cache[fw_map[wi]]; ftype->set_w(fw); SG_UNREF(ftype); } ASSERT(offset == m_w_cache.size()); }
float64_t CMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t blen; bool bfree; float64_t* bvec = ((CSimpleFeatures<float64_t>*) rhs)-> get_feature_vector(idx_b, blen, bfree); ASSERT(blen == mean.vlen); SGVector<float64_t> diff(bvec, blen); for (int32_t i = 0 ; i<blen ; i++) diff[i] -= mean[i]; SGVector<float64_t> v = diff.clone(); cblas_dgemv(CblasColMajor, CblasNoTrans, icov.num_rows, icov.num_cols, 1.0, icov.matrix, diff.vlen, diff.vector, 1, 0.0, v.vector, 1); float64_t result = cblas_ddot(v.vlen, v.vector, 1, diff.vector, 1); ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(bvec, idx_b, bfree); v.destroy_vector(); if (disable_sqrt) return result; else return CMath::sqrt(result); }
int32_t CDisjointSet::get_unique_labeling(SGVector<int32_t> out_labels) { REQUIRE(m_num_elements > 0, "%s::get_unique_labeling(): m_num_elements <= 0.\n", get_name()); if (out_labels.size() != m_num_elements) out_labels.resize_vector(m_num_elements); SGVector<int32_t> roots(m_num_elements); SGVector<int32_t> flags(m_num_elements); SGVector<int32_t>::fill_vector(flags.vector, flags.vlen, -1); int32_t unilabel = 0; for (int32_t i = 0; i < m_num_elements; i++) { roots[i] = find_set(i); // if roots[i] never be found if (flags[roots[i]] < 0) flags[roots[i]] = unilabel++; } for (int32_t i = 0; i < m_num_elements; i++) out_labels[i] = flags[roots[i]]; return unilabel; }
SGVector<float64_t> CFactorGraphModel::fparams_to_w() { REQUIRE(m_factor_types != NULL, "%s::fparams_to_w(): no factor types!\n", get_name()); if (m_w_cache.size() != get_dim()) m_w_cache.resize_vector(get_dim()); int32_t offset = 0; for (int32_t fi = 0; fi < m_factor_types->get_num_elements(); ++fi) { CFactorType* ftype = dynamic_cast<CFactorType*>(m_factor_types->get_element(fi)); int32_t w_dim = ftype->get_w_dim(); offset += w_dim; SGVector<float64_t> fw = ftype->get_w(); SGVector<int32_t> fw_map = get_params_mapping(ftype->get_type_id()); ASSERT(fw_map.size() == fw.size()); for (int32_t wi = 0; wi < w_dim; wi++) m_w_cache[fw_map[wi]] = fw[wi]; SG_UNREF(ftype); } ASSERT(offset == m_w_cache.size()); return m_w_cache; }
SGVector<float64_t> CLogitLikelihood::get_log_zeroth_moments( SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels* lab) const { SGVector<float64_t> y; if (lab) { REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()), "Length of the vector of means (%d), length of the vector of " "variances (%d) and number of labels (%d) should be the same\n", mu.vlen, s2.vlen, lab->get_num_labels()) REQUIRE(lab->get_label_type()==LT_BINARY, "Labels must be type of CBinaryLabels\n") y=((CBinaryLabels*)lab)->get_labels(); } else { REQUIRE(mu.vlen==s2.vlen, "Length of the vector of means (%d) and " "length of the vector of variances (%d) should be the same\n", mu.vlen, s2.vlen) y=SGVector<float64_t>(mu.vlen); y.set_const(1.0); } // create an object of normal pdf function CNormalPDF* f=new CNormalPDF(); // create an object of sigmoid function CSigmoidFunction* g=new CSigmoidFunction(); // create an object of product of sigmoid and normal pdf functions CProductFunction* h=new CProductFunction(f, g); SG_REF(h); // compute probabilities using numerical integration SGVector<float64_t> r(mu.vlen); for (index_t i=0; i<mu.vlen; i++) { // set normal pdf parameters f->set_mu(mu[i]); f->set_sigma(CMath::sqrt(s2[i])); // set sigmoid parameters g->set_a(y[i]); // evaluate integral on (-inf, inf) r[i]=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+ CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY); } SG_UNREF(h); r.log(); return r; }
SGVector<float64_t> CFactor::get_energies() const { if (is_data_dependent() == false && m_energies.size() == 0) { const SGVector<float64_t> ft_energies = m_factor_type->get_w(); ASSERT(ft_energies.size() == m_factor_type->get_num_assignments()); return ft_energies; } return m_energies; }
void CMulticlassLabels::set_multiclass_confidences(int32_t i, SGVector<float64_t> confidences) { REQUIRE(confidences.size()==m_multiclass_confidences.num_rows, "%s::set_multiclass_confidences(): Length of confidences should " "match size of the matrix", get_name()); for (index_t j=0; j<confidences.size(); j++) m_multiclass_confidences(j,i) = confidences[j]; }
void CFactorGraph::loss_augmentation(SGVector<int32_t> states_gt, SGVector<float64_t> loss) { if (loss.size() == 0) { loss.resize_vector(states_gt.size()); SGVector<float64_t>::fill_vector(loss.vector, loss.vlen, 1.0 / states_gt.size()); } int32_t num_vars = states_gt.size(); ASSERT(num_vars == loss.size()); SGVector<int32_t> var_flags(num_vars); var_flags.zero(); // augment loss to incorrect states in the first factor containing the variable // since += L_i for each variable if it takes wrong state ever // TODO: augment unary factors for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi) { CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi)); SGVector<int32_t> vars = fac->get_variables(); for (int32_t vi = 0; vi < vars.size(); vi++) { int32_t vv = vars[vi]; ASSERT(vv < num_vars); if (var_flags[vv]) continue; SGVector<float64_t> energies = fac->get_energies(); for (int32_t ei = 0; ei < energies.size(); ei++) { CTableFactorType* ftype = fac->get_factor_type(); int32_t vstate = ftype->state_from_index(ei, vi); SG_UNREF(ftype); if (states_gt[vv] == vstate) continue; // -delta(y_n, y_i_n) fac->set_energy(ei, energies[ei] - loss[vv]); } var_flags[vv] = 1; } SG_UNREF(fac); } // make sure all variables have been checked int32_t min_var = SGVector<int32_t>::min(var_flags.vector, var_flags.vlen); ASSERT(min_var == 1); }
SGVector<float64_t> CExactInferenceMethod::get_diagonal_vector() { check_members(); float64_t m_sigma = dynamic_cast<CGaussianLikelihood*>(m_model)->get_sigma(); SGVector<float64_t> result = SGVector<float64_t>(m_features->get_num_vectors()); result.fill_vector(result.vector, m_features->get_num_vectors(), 1.0/m_sigma); return result; }
void CTwoStateModel::reshape_emission_params(SGVector< float64_t >& emission_weights, SGVector< float64_t > w, int32_t num_feats, int32_t num_obs) { emission_weights.zero(); // Legend for state indices: // 0 -> start state // 1 -> stop state // 2 -> negative state (label == 0) // 3 -> positive state (label == 1) // // start and stop states have no emission scores index_t em_idx, w_idx = m_num_transmission_params; for ( int32_t s = 2 ; s < m_num_states ; ++s ) { for ( int32_t f = 0 ; f < num_feats ; ++f ) { for ( int32_t o = 0 ; o < num_obs ; ++o ) { em_idx = s*num_feats*num_obs + f*num_obs + o; emission_weights[em_idx] = w[w_idx++]; } } } }
void CFactor::set_energies(SGVector<float64_t> ft_energies) { REQUIRE(m_factor_type->get_num_assignments() == ft_energies.size(), "%s::set_energies(): ft_energies is not a valid energy table!\n", get_name()); m_energies = ft_energies; }
void gen_rand_data(SGVector<float64_t> lab, SGMatrix<float64_t> feat, float64_t dist) { index_t dims=feat.num_rows; index_t num=lab.vlen; for (int32_t i=0; i<num; i++) { if (i<num/2) { lab[i]=-1.0; for (int32_t j=0; j<dims; j++) feat(j, i)=CMath::random(0.0, 1.0)+dist; } else { lab[i]=1.0; for (int32_t j=0; j<dims; j++) feat(j, i)=CMath::random(0.0, 1.0)-dist; } } lab.display_vector("lab"); feat.display_matrix("feat"); }
CDualLibQPBMSOSVM::CDualLibQPBMSOSVM( CStructuredModel* model, CStructuredLabels* labs, float64_t _lambda, SGVector< float64_t > W) : CLinearStructuredOutputMachine(model, labs) { init(); set_lambda(_lambda); // get dimension of w int32_t nDim=this->m_model->get_dim(); // Check for initial solution if (W.vlen==0) { set_w(SGVector< float64_t >(nDim)); get_w().zero(); } else { ASSERT(W.size() == nDim); set_w(W); } }
SGVector< float64_t > CFactorGraphModel::get_joint_feature_vector(int32_t feat_idx, CStructuredData* y) { // factor graph instance CFactorGraphFeatures* mf = CFactorGraphFeatures::obtain_from_generic(m_features); CFactorGraph* fg = mf->get_sample(feat_idx); // ground truth states CFactorGraphObservation* fg_states = CFactorGraphObservation::obtain_from_generic(y); SGVector<int32_t> states = fg_states->get_data(); // initialize psi SGVector<float64_t> psi(get_dim()); psi.zero(); // construct unnormalized psi CDynamicObjectArray* facs = fg->get_factors(); for (int32_t fi = 0; fi < facs->get_num_elements(); ++fi) { CFactor* fac = dynamic_cast<CFactor*>(facs->get_element(fi)); CTableFactorType* ftype = fac->get_factor_type(); int32_t id = ftype->get_type_id(); SGVector<int32_t> w_map = get_params_mapping(id); ASSERT(w_map.size() == ftype->get_w_dim()); SGVector<float64_t> dat = fac->get_data(); int32_t dat_size = dat.size(); ASSERT(w_map.size() == dat_size * ftype->get_num_assignments()); int32_t ei = ftype->index_from_universe_assignment(states, fac->get_variables()); for (int32_t di = 0; di < dat_size; di++) psi[w_map[ei*dat_size + di]] += dat[di]; SG_UNREF(ftype); SG_UNREF(fac); } // negation (-E(x,y) = <w,phi(x,y)>) psi.scale(-1.0); SG_UNREF(facs); SG_UNREF(fg); return psi; }
SGVector<float64_t> CProbitLikelihood::get_log_zeroth_moments( SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels* lab) const { SGVector<float64_t> y; if (lab) { REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()), "Length of the vector of means (%d), length of the vector of " "variances (%d) and number of labels (%d) should be the same\n", mu.vlen, s2.vlen, lab->get_num_labels()) REQUIRE(lab->get_label_type()==LT_BINARY, "Labels must be type of CBinaryLabels\n") y=((CBinaryLabels*)lab)->get_labels(); } else { REQUIRE(mu.vlen==s2.vlen, "Length of the vector of means (%d) and " "length of the vector of variances (%d) should be the same\n", mu.vlen, s2.vlen) y=SGVector<float64_t>(mu.vlen); y.set_const(1.0); } Map<VectorXd> eigen_y(y.vector, y.vlen); Map<VectorXd> eigen_mu(mu.vector, mu.vlen); Map<VectorXd> eigen_s2(s2.vector, s2.vlen); SGVector<float64_t> r(y.vlen); Map<VectorXd> eigen_r(r.vector, r.vlen); // compute: lp=log(normal_cdf((mu.*y)./sqrt(1+sigma^2))) eigen_r=eigen_mu.array()*eigen_y.array()/((1.0+eigen_s2.array()).sqrt()); for (index_t i=0; i<eigen_r.size(); i++) eigen_r[i]=CStatistics::lnormal_cdf(eigen_r[i]); return r; }
float64_t CMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b) { SGVector<float64_t> bvec = ((CDenseFeatures<float64_t>*) rhs)-> get_feature_vector(idx_b); SGVector<float64_t> diff; SGVector<float64_t> avec; if (use_mean) diff = mean.clone(); else { avec = ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a); diff=avec.clone(); } ASSERT(diff.vlen == bvec.vlen); for (int32_t i=0; i < diff.vlen; i++) diff[i] = bvec.vector[i] - diff[i]; SGVector<float64_t> v = diff.clone(); cblas_dgemv(CblasColMajor, CblasNoTrans, icov.num_rows, icov.num_cols, 1.0, icov.matrix, diff.vlen, diff.vector, 1, 0.0, v.vector, 1); float64_t result = cblas_ddot(v.vlen, v.vector, 1, diff.vector, 1); if (!use_mean) ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a); ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b); if (disable_sqrt) return result; else return CMath::sqrt(result); }
float64_t CFactorGraph::evaluate_energy(const SGVector<int32_t> state) const { ASSERT(state.size() == m_cardinalities.size()); float64_t energy = 0.0; for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi) { CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi)); energy += fac->evaluate_energy(state); SG_UNREF(fac); } return energy; }
void SGNDArray<T>::expand(SGNDArray &big_array, SGVector<index_t>& axes) { // TODO: A nice implementation would be a function like repmat in matlab REQUIRE(axes.size() <= 2, "Provided axes size (%d) must be smaller than 2.\n", axes.size()); REQUIRE(num_dims <= 2, "Number of dimensions (%d) must be smaller than 2. Only 1-d and 2-d array can be expanded currently.\n", num_dims); // Initialize indices in big array to zeros SGVector<index_t> inds_big(big_array.num_dims); inds_big.zero(); // Replicate the small array to the big one. // Go over the big one by one and take the corresponding value T* data_big = &big_array.array[0]; for (int32_t vi = 0; vi < big_array.len_array; vi++) { int32_t y = 0; if (axes.size() == 1) { y = inds_big[axes[0]]; } else if (axes.size() == 2) { int32_t ind1 = axes[0]; int32_t ind2 = axes[1]; y = inds_big[ind1] * dims[1] + inds_big[ind2]; } *data_big = array[y]; data_big++; // Move to the next index big_array.next_index(inds_big); } }
void SGNDArray<T>::next_index(SGVector<index_t>& curr_index) const { REQUIRE(curr_index.size() == num_dims, "The provided number of dimensions (%d) does not match the internal number of dimensions (%d).\n", curr_index.size(), num_dims); for (int32_t i = num_dims - 1; i >= 0; i--) { curr_index[i]++; if (curr_index[i] < dims[i]) break; curr_index[i] = 0; } }
void CFactorGraph::connect_components() { if (m_dset->get_connected()) return; // need to be reset once factor graph is updated m_dset->make_sets(); bool flag = false; for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi) { CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi)); SGVector<int32_t> vars = fac->get_variables(); int32_t r0 = m_dset->find_set(vars[0]); for (int32_t vi = 1; vi < vars.size(); vi++) { // for two nodes in a factor, should be an edge between them // but this time link() isn't performed, if they are linked already // means there is another path connected them, so cycle detected int32_t ri = m_dset->find_set(vars[vi]); if (r0 == ri) { flag = true; continue; } r0 = m_dset->link_set(r0, ri); } SG_UNREF(fac); } m_has_cycle = flag; m_dset->set_connected(true); }
template<class T> SGNDArray<T>::SGNDArray(const SGVector<index_t> dimensions, bool ref_counting) : SGReferencedData(ref_counting) { num_dims = dimensions.size(); dims = SG_MALLOC(index_t, num_dims); len_array = 1; for (int32_t i=0; i<num_dims; i++) { dims[i] = dimensions[i]; len_array *= dims[i]; } REQUIRE(len_array>0, "Length of array (%d) must be greater than 0\n", len_array); array = SG_MALLOC(T, len_array); }
CWeightedDegreeStringKernel::CWeightedDegreeStringKernel(SGVector<float64_t> w) : CStringKernel<char>(10) { init(); type=E_EXTERNAL; degree=w.vlen; weights=SG_MALLOC(float64_t, degree*(1+max_mismatch)); weights_degree=degree; weights_length=(1+max_mismatch); for (int32_t i=0; i<degree*(1+max_mismatch); i++) weights[i]=w.vector[i]; w.free_vector(); }
void CCARTree::prune_using_test_dataset(CDenseFeatures<float64_t>* feats, CLabels* gnd_truth, SGVector<float64_t> weights) { if (weights.vlen==0) { weights=SGVector<float64_t>(feats->get_num_vectors()); weights.fill_vector(weights.vector,weights.vlen,1); } CDynamicObjectArray* pruned_trees=prune_tree(this); int32_t min_index=0; float64_t min_error=CMath::MAX_REAL_NUMBER; for (int32_t i=0;i<m_alphas->get_num_elements();i++) { CSGObject* element=pruned_trees->get_element(i); bnode_t* root=NULL; if (element!=NULL) root=dynamic_cast<bnode_t*>(element); else SG_ERROR("%d element is NULL\n",i); CLabels* labels=apply_from_current_node(feats, root); float64_t error=compute_error(labels,gnd_truth,weights); if (error<min_error) { min_index=i; min_error=error; } SG_UNREF(labels); SG_UNREF(element); } CSGObject* element=pruned_trees->get_element(min_index); bnode_t* root=NULL; if (element!=NULL) root=dynamic_cast<bnode_t*>(element); else SG_ERROR("%d element is NULL\n",min_index); this->set_root(root); SG_UNREF(pruned_trees); SG_UNREF(element); }
T SGNDArray<T>::get_value(SGVector<index_t> index) const { int32_t y = 0; int32_t fact = 1; REQUIRE(index.size() == num_dims, "Provided number of dimensions (%d) does not match internal number of dimensions (%d).\n", index.size(), num_dims); for (int32_t i = num_dims - 1; i >= 0; i--) { REQUIRE(index[i] < dims[i], "Provided index (%d) on dimension %d must be smaller than %d. \n", index[i], i, dims[i]); y += index[i] * fact; fact *= dims[i]; } return array[y]; }
void CMultitaskROCEvaluation::set_indices(SGVector<index_t> indices) { indices.display_vector("indices"); ASSERT(m_task_relation) set<index_t> indices_set; for (int32_t i=0; i<indices.vlen; i++) indices_set.insert(indices[i]); if (m_num_tasks>0) { SG_FREE(m_tasks_indices); } m_num_tasks = m_task_relation->get_num_tasks(); m_tasks_indices = SG_MALLOC(SGVector<index_t>, m_num_tasks); SGVector<index_t>* tasks_indices = m_task_relation->get_tasks_indices(); for (int32_t t=0; t<m_num_tasks; t++) { vector<index_t> task_indices_cut; SGVector<index_t> task_indices = tasks_indices[t]; //task_indices.display_vector("task indices"); for (int32_t i=0; i<task_indices.vlen; i++) { if (indices_set.count(task_indices[i])) { //SG_SPRINT("%d is in %d task\n",task_indices[i],t) task_indices_cut.push_back(task_indices[i]); } } SGVector<index_t> cutted(task_indices_cut.size()); for (int32_t i=0; i<cutted.vlen; i++) cutted[i] = task_indices_cut[i]; //cutted.display_vector("cutted"); m_tasks_indices[t] = cutted; } SG_FREE(tasks_indices); }
int main(int argc, char **argv) { init_shogun_with_defaults(); /* create some data and labels */ SGMatrix<float64_t> matrix = SGMatrix<float64_t>(dim_vectors, num_vectors); SGMatrix<float64_t> matrix2 = SGMatrix<float64_t>(dim_vectors, num_vectors); CRegressionLabels* labels=new CRegressionLabels(num_vectors); build_matrices(matrix2, matrix, labels); /* create training features */ CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> (); features->set_feature_matrix(matrix); /* create testing features */ CDenseFeatures<float64_t>* features2=new CDenseFeatures<float64_t> (); features2->set_feature_matrix(matrix2); SG_REF(features); SG_REF(features2); SG_REF(labels); /*Allocate our Kernel*/ CGaussianKernel* test_kernel = new CGaussianKernel(10, 2); test_kernel->init(features, features); /*Allocate our mean function*/ CZeroMean* mean = new CZeroMean(); /*Allocate our likelihood function*/ CGaussianLikelihood* lik = new CGaussianLikelihood(); /*Allocate our inference method*/ CExactInferenceMethod* inf = new CExactInferenceMethod(test_kernel, features, mean, labels, lik); SG_REF(inf); /*Finally use these to allocate the Gaussian Process Object*/ CGaussianProcessRegression* gp = new CGaussianProcessRegression(inf, features, labels); SG_REF(gp); /*Build the parameter tree for model selection*/ CModelSelectionParameters* root = build_tree(inf, lik, test_kernel); /*Criterion for gradient search*/ CGradientCriterion* crit = new CGradientCriterion(); /*This will evaluate our inference method for its derivatives*/ CGradientEvaluation* grad=new CGradientEvaluation(gp, features, labels, crit); grad->set_function(inf); gp->print_modsel_params(); root->print_tree(); /* handles all of the above structures in memory */ CGradientModelSelection* grad_search=new CGradientModelSelection( root, grad); /* set autolocking to false to get rid of warnings */ grad->set_autolock(false); /*Search for best parameters*/ CParameterCombination* best_combination=grad_search->select_model(true); /*Output all the results and information*/ if (best_combination) { SG_SPRINT("best parameter(s):\n"); best_combination->print_tree(); best_combination->apply_to_machine(gp); } CGradientResult* result=(CGradientResult*)grad->evaluate(); if(result->get_result_type() != GRADIENTEVALUATION_RESULT) SG_SERROR("Evaluation result not a GradientEvaluationResult!"); result->print_result(); SGVector<float64_t> alpha = inf->get_alpha(); SGVector<float64_t> labe = labels->get_labels(); SGVector<float64_t> diagonal = inf->get_diagonal_vector(); SGMatrix<float64_t> cholesky = inf->get_cholesky(); gp->set_return_type(CGaussianProcessRegression::GP_RETURN_COV); CRegressionLabels* covariance = gp->apply_regression(features); gp->set_return_type(CGaussianProcessRegression::GP_RETURN_MEANS); CRegressionLabels* predictions = gp->apply_regression(); alpha.display_vector("Alpha Vector"); labe.display_vector("Labels"); diagonal.display_vector("sW Matrix"); covariance->get_labels().display_vector("Predicted Variances"); predictions->get_labels().display_vector("Mean Predictions"); cholesky.display_matrix("Cholesky Matrix L"); matrix.display_matrix("Training Features"); matrix2.display_matrix("Testing Features"); /*free memory*/ SG_UNREF(features); SG_UNREF(features2); SG_UNREF(predictions); SG_UNREF(covariance); SG_UNREF(labels); SG_UNREF(inf); SG_UNREF(gp); SG_UNREF(grad_search); SG_UNREF(best_combination); SG_UNREF(result); SG_UNREF(mean); exit_shogun(); return 0; }
void CFactorDataSource::set_data(SGVector<float64_t> dense) { m_dense = dense.clone(); }
void CFactor::set_data(SGVector<float64_t> data_dense) { m_data = data_dense.clone(); m_is_data_dep = true; }
void CFactor::set_variables(SGVector<int32_t> vars) { m_var_index = vars.clone(); }
int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); int32_t num_vectors=4; int32_t dim_vectors=3; /* create some data and labels */ SGMatrix<float64_t> matrix = SGMatrix<float64_t>(dim_vectors, num_vectors); matrix[0] = -1; matrix[1] = -1; matrix[2] = -1; matrix[3] = 1; matrix[4] = 1; matrix[5] = 1; matrix[6] = -10; matrix[7] = -10; matrix[8] = -10; matrix[9] = 3; matrix[10] = 2; matrix[11] = 1; SGMatrix<float64_t> matrix2 = SGMatrix<float64_t>(dim_vectors, num_vectors); for (int32_t i=0; i<num_vectors*dim_vectors; i++) matrix2[i]=i*sin(i)*.96; /* create training features */ CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> (); features->set_feature_matrix(matrix); /* create testing features */ CDenseFeatures<float64_t>* features2=new CDenseFeatures<float64_t> (); features2->set_feature_matrix(matrix2); SG_REF(features); SG_REF(features2); CRegressionLabels* labels=new CRegressionLabels(num_vectors); /* create labels, two classes */ for (index_t i=0; i<num_vectors; ++i) { if(i%2 == 0) labels->set_label(i, 1); else labels->set_label(i, -1); } SG_REF(labels); CGaussianKernel* test_kernel = new CGaussianKernel(10, 2); test_kernel->init(features, features); CZeroMean* mean = new CZeroMean(); CGaussianLikelihood* lik = new CGaussianLikelihood(); lik->set_sigma(0.01); CExactInferenceMethod* inf = new CExactInferenceMethod(test_kernel, features, mean, labels, lik); SG_REF(inf); CGaussianProcessRegression* gp = new CGaussianProcessRegression(inf, features, labels); CModelSelectionParameters* root=new CModelSelectionParameters(); CModelSelectionParameters* c1 = new CModelSelectionParameters("inference_method", inf); root->append_child(c1); CModelSelectionParameters* c2 = new CModelSelectionParameters("scale"); c1 ->append_child(c2); c2->build_values(0.01, 4.0, R_LINEAR); CModelSelectionParameters* c3 = new CModelSelectionParameters("likelihood_model", lik); c1->append_child(c3); CModelSelectionParameters* c4=new CModelSelectionParameters("sigma"); c3->append_child(c4); c4->build_values(0.001, 4.0, R_LINEAR); CModelSelectionParameters* c5 = new CModelSelectionParameters("kernel", test_kernel); c1->append_child(c5); CModelSelectionParameters* c6 = new CModelSelectionParameters("width"); c5->append_child(c6); c6->build_values(0.001, 4.0, R_LINEAR); /* cross validation class for evaluation in model selection */ SG_REF(gp); CGradientCriterion* crit = new CGradientCriterion(); CGradientEvaluation* grad=new CGradientEvaluation(gp, features, labels, crit); grad->set_function(inf); gp->print_modsel_params(); root->print_tree(); /* handles all of the above structures in memory */ CGradientModelSelection* grad_search=new CGradientModelSelection( root, grad); /* set autolocking to false to get rid of warnings */ grad->set_autolock(false); CParameterCombination* best_combination=grad_search->select_model(true); grad_search->set_max_evaluations(5); if (best_combination) { SG_SPRINT("best parameter(s):\n"); best_combination->print_tree(); best_combination->apply_to_machine(gp); } CGradientResult* result=(CGradientResult*)grad->evaluate(); if(result->get_result_type() != GRADIENTEVALUATION_RESULT) SG_SERROR("Evaluation result not a GradientEvaluationResult!"); result->print_result(); SGVector<float64_t> alpha = inf->get_alpha(); SGVector<float64_t> labe = labels->get_labels(); SGVector<float64_t> diagonal = inf->get_diagonal_vector(); SGMatrix<float64_t> cholesky = inf->get_cholesky(); gp->set_return_type(CGaussianProcessRegression::GP_RETURN_COV); CRegressionLabels* covariance = gp->apply_regression(features); gp->set_return_type(CGaussianProcessRegression::GP_RETURN_MEANS); CRegressionLabels* predictions = gp->apply_regression(); alpha.display_vector("Alpha Vector"); labe.display_vector("Labels"); diagonal.display_vector("sW Matrix"); covariance->get_labels().display_vector("Predicted Variances"); predictions->get_labels().display_vector("Mean Predictions"); cholesky.display_matrix("Cholesky Matrix L"); matrix.display_matrix("Training Features"); matrix2.display_matrix("Testing Features"); /*free memory*/ SG_UNREF(features); SG_UNREF(features2); SG_UNREF(predictions); SG_UNREF(covariance); SG_UNREF(labels); SG_UNREF(inf); SG_UNREF(gp); SG_UNREF(grad_search); SG_UNREF(best_combination); SG_UNREF(result); exit_shogun(); return 0; }