Advertisement
Guest User

Untitled

a guest
Aug 28th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <shogun/features/streaming/StreamingDenseFeatures.h>
  2. #include <shogun/io/streaming/StreamingAsciiFile.h>
  3. #include <shogun/labels/MulticlassLabels.h>
  4. #include <shogun/kernel/GaussianKernel.h>
  5. #include <shogun/kernel/LinearKernel.h>
  6. #include <shogun/kernel/PolyKernel.h>
  7. #include <shogun/kernel/CombinedKernel.h>
  8. #include <shogun/classifier/mkl/MKLMulticlass.h>
  9.  
  10. #include <iostream>
  11.  
  12. using namespace shogun;
  13.  
  14. void test_multiclass_mkl()
  15. {
  16.    /* stream data from a file */
  17.    int32_t num_vectors=50;
  18.    int32_t num_feats=2;
  19.  
  20.    /* file data */
  21.    char fname_feats[]=../data/fm_train_real.dat";
  22.   char fname_labels[]="../data/label_train_multiclass.dat";
  23.   CStreamingAsciiFile* ffeats_train=new CStreamingAsciiFile(fname_feats);
  24.   CStreamingAsciiFile* flabels_train=new CStreamingAsciiFile(fname_labels);
  25.   SG_REF(ffeats_train);
  26.   SG_REF(flabels_train);
  27.  
  28.   /* streaming data */
  29.   CStreamingDenseFeatures<float64_t>* stream_features=
  30.         new CStreamingDenseFeatures<float64_t>(ffeats_train, false, 1024);
  31.   CStreamingDenseFeatures<float64_t>* stream_labels=
  32.         new CStreamingDenseFeatures<float64_t>(flabels_train, true, 1024);
  33.   SG_REF(stream_features);
  34.   SG_REF(stream_labels);
  35.  
  36.   /* matrix data */
  37.   SGMatrix<float64_t> mat=SGMatrix<float64_t>(num_feats, num_vectors);
  38.   SGVector<float64_t> vec;
  39.   stream_features->start_parser();
  40.  
  41.   index_t count=0;
  42.   while (stream_features->get_next_example() && count<num_vectors)
  43.   {
  44.      vec=stream_features->get_vector();
  45.      for (int32_t i=0; i<num_feats; ++i)
  46.         mat(i,count)=vec[i];
  47.  
  48.      stream_features->release_example();
  49.      count++;
  50.   }
  51.   stream_features->end_parser();
  52.   mat.num_cols=num_vectors;
  53.  
  54.   /* dense features from streamed matrix */
  55.   CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat);
  56.   CMulticlassLabels* labels=new CMulticlassLabels(num_vectors);
  57.   SG_REF(features);
  58.   SG_REF(labels);
  59.  
  60.   /* read labels from file */
  61.   int32_t idx=0;
  62.   stream_labels->start_parser();
  63.   while (stream_labels->get_next_example())
  64.   {
  65.      labels->set_int_label(idx++, (int32_t)stream_labels->get_label());
  66.      stream_labels->release_example();
  67.   }
  68.   stream_labels->end_parser();
  69.  
  70.   /* combined features and kernel */
  71.   CCombinedFeatures *cfeats=new CCombinedFeatures();
  72.   CCombinedKernel *cker=new CCombinedKernel();
  73.   SG_REF(cfeats);
  74.   SG_REF(cker);
  75.  
  76.  
  77.   /** 1st kernel: gaussian */
  78.   cfeats->append_feature_obj(features);
  79.   cker->append_kernel(new CGaussianKernel(features, features, 1.2, 10));
  80.  
  81.   /** 3rd kernel: poly */
  82.   cfeats->append_feature_obj(features);
  83.   cker->append_kernel(new CPolyKernel(features, features, 2, true, 10));
  84.  
  85.   /** 2nd kernel: linear */
  86.   cfeats->append_feature_obj(features);
  87.   cker->append_kernel(new CLinearKernel(features, features));
  88.  
  89.   cker->init(cfeats, cfeats);
  90.  
  91.   /* create mkl instance */
  92.   CMKLMulticlass* mkl=new CMKLMulticlass(1.2, cker, labels);
  93.   SG_REF(mkl);
  94.   mkl->set_epsilon(0.00001);
  95.   mkl->set_mkl_epsilon(0.001);
  96.   mkl->set_mkl_norm(1.5);
  97.  
  98.   /* train to see weights */
  99.   mkl->train();
  100.   cker->get_subkernel_weights().display_vector("weights");
  101.  
  102.  
  103.   /* clean up */
  104.   SG_UNREF(ffeats_train);
  105.   SG_UNREF(flabels_train);
  106.   SG_UNREF(stream_features);
  107.   SG_UNREF(stream_labels);
  108.   SG_UNREF(features);
  109.   SG_UNREF(labels);
  110.   SG_UNREF(cfeats);
  111.   SG_UNREF(cker);
  112.   SG_UNREF(mkl);
  113. }
  114.  
  115.  
  116. int main(int argc, char** argv){
  117.  
  118.   shogun::init_shogun_with_defaults();
  119.   test_multiclass_mkl();
  120.  
  121.   exit_shogun();
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement