Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @author thereisnoknife@gmail.com */
- // stl
- #include <iostream>
- // shogun init
- #include <shogun/io/SGIO.h>
- #include <shogun/lib/ShogunException.h>
- // features
- #include <shogun/features/StreamingDenseFeatures.h>
- #include <shogun/io/StreamingAsciiFile.h>
- #include <shogun/labels/MulticlassLabels.h>
- // kernel
- #include <shogun/kernel/GaussianKernel.h>
- #include <shogun/kernel/LinearKernel.h>
- #include <shogun/kernel/PolyKernel.h>
- // mkl
- #include <shogun/kernel/CustomKernel.h>
- #include <shogun/kernel/CombinedKernel.h>
- #include <shogun/classifier/mkl/MKLMulticlass.h>
- // evaluation
- #include <shogun/evaluation/StratifiedCrossValidationSplitting.h>
- #include <shogun/evaluation/CrossValidation.h>
- #include <shogun/evaluation/MulticlassAccuracy.h>
- using namespace shogun;
- int main(int argc, char** argv){
- /** init */
- shogun::init_shogun_with_defaults();
- int32_t num_vectors = 0;
- int32_t num_feats = 2;
- /** file data */
- char fname_feats[] = "/home/eric/buildsys/shogun.data/toy/fm_train_real.dat";
- char fname_labels[] = "/home/eric/buildsys/shogun.data/toy/label_train_multiclass.dat";
- CStreamingAsciiFile* ffeats_train = new CStreamingAsciiFile(fname_feats);
- CStreamingAsciiFile* flabels_train = new CStreamingAsciiFile(fname_labels);
- SG_REF(ffeats_train);
- SG_REF(flabels_train);
- /** streaming data */
- CStreamingDenseFeatures< float64_t >* stream_features =
- new CStreamingDenseFeatures< float64_t >(ffeats_train, false, 1024);
- CStreamingDenseFeatures< float64_t >* stream_labels =
- new CStreamingDenseFeatures< float64_t >(flabels_train, true, 1024);
- SG_REF(stream_features);
- SG_REF(stream_labels);
- /** matrix data */
- SGMatrix< float64_t > mat = SGMatrix< float64_t >(num_feats, 1000);
- SGVector< float64_t > vec;
- stream_features->start_parser();
- while ( stream_features->get_next_example() ){
- vec = stream_features->get_vector();
- for ( int32_t i = 0 ; i < num_feats ; ++i )
- mat.matrix[num_vectors*num_feats + i] = vec[i];
- num_vectors++;
- stream_features->release_example();
- }
- stream_features->end_parser();
- mat.num_cols = num_vectors;
- /** dense features */
- CDenseFeatures< float64_t >* features = new CDenseFeatures<float64_t>(mat);
- CMulticlassLabels* labels = new CMulticlassLabels(num_vectors);
- SG_REF(features);
- SG_REF(labels);
- // Read the labels from the file
- int32_t idx = 0;
- stream_labels->start_parser();
- while ( stream_labels->get_next_example() ){
- labels->set_int_label( idx++, (int32_t)stream_labels->get_label() );
- stream_labels->release_example();
- }
- stream_labels->end_parser();
- /** combined features */
- CCombinedFeatures *cfeats = new CCombinedFeatures();
- CCombinedKernel *cker = new CCombinedKernel();
- /** 1st kernel: gaussian */
- CGaussianKernel *gker = new CGaussianKernel(features,features,1.2,10);
- cfeats->append_feature_obj(features);
- cker->append_kernel(gker);
- /** 2nd kernel: linear */
- CLinearKernel *lker = new CLinearKernel(features,features);
- cfeats->append_feature_obj(features);
- cker->append_kernel(lker);
- /** 3rd kernel: poly */
- CPolyKernel *pker = new CPolyKernel(features, features, 2, true, 10);
- cfeats->append_feature_obj(features);
- cker->append_kernel(pker);
- cker->init(cfeats,cfeats);
- CMKLMulticlass *mkl = new CMKLMulticlass(1.2,cker,labels);
- mkl->set_epsilon(0.00001);
- mkl->parallel->set_num_threads(1);
- mkl->set_mkl_epsilon(0.001);
- mkl->set_mkl_norm(1.5);
- mkl->train();
- cker->get_subkernel_weights().display_vector("weights");
- index_t n_folds=2;
- CMulticlassAccuracy* eval_crit = new CMulticlassAccuracy();
- CStratifiedCrossValidationSplitting* splitting = new CStratifiedCrossValidationSplitting(labels, n_folds);
- CCrossValidation *cross= new CCrossValidation(mkl,cfeats,labels,splitting,eval_crit);
- cross->set_autolock(false);
- cross->set_num_runs(5);
- cross->set_conf_int_alpha(0.05);
- CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
- std::cout << "Mean= " << result->mean << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement