Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <shogun/features/streaming/StreamingDenseFeatures.h>
- #include <shogun/io/streaming/StreamingAsciiFile.h>
- #include <shogun/labels/MulticlassLabels.h>
- #include <shogun/kernel/GaussianKernel.h>
- #include <shogun/kernel/LinearKernel.h>
- #include <shogun/kernel/PolyKernel.h>
- #include <shogun/kernel/CombinedKernel.h>
- #include <shogun/classifier/mkl/MKLMulticlass.h>
- #include <iostream>
- using namespace shogun;
- void test_multiclass_mkl()
- {
- /* stream data from a file */
- int32_t num_vectors=50;
- int32_t num_feats=2;
- /* file data */
- char fname_feats[]=../data/fm_train_real.dat";
- char fname_labels[]="../data/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, num_vectors);
- SGVector<float64_t> vec;
- stream_features->start_parser();
- index_t count=0;
- while (stream_features->get_next_example() && count<num_vectors)
- {
- vec=stream_features->get_vector();
- for (int32_t i=0; i<num_feats; ++i)
- mat(i,count)=vec[i];
- stream_features->release_example();
- count++;
- }
- stream_features->end_parser();
- mat.num_cols=num_vectors;
- /* dense features from streamed matrix */
- CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat);
- CMulticlassLabels* labels=new CMulticlassLabels(num_vectors);
- SG_REF(features);
- SG_REF(labels);
- /* read labels from 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 and kernel */
- CCombinedFeatures *cfeats=new CCombinedFeatures();
- CCombinedKernel *cker=new CCombinedKernel();
- SG_REF(cfeats);
- SG_REF(cker);
- /** 1st kernel: gaussian */
- cfeats->append_feature_obj(features);
- cker->append_kernel(new CGaussianKernel(features, features, 1.2, 10));
- /** 3rd kernel: poly */
- cfeats->append_feature_obj(features);
- cker->append_kernel(new CPolyKernel(features, features, 2, true, 10));
- /** 2nd kernel: linear */
- cfeats->append_feature_obj(features);
- cker->append_kernel(new CLinearKernel(features, features));
- cker->init(cfeats, cfeats);
- /* create mkl instance */
- CMKLMulticlass* mkl=new CMKLMulticlass(1.2, cker, labels);
- SG_REF(mkl);
- mkl->set_epsilon(0.00001);
- mkl->set_mkl_epsilon(0.001);
- mkl->set_mkl_norm(1.5);
- /* train to see weights */
- mkl->train();
- cker->get_subkernel_weights().display_vector("weights");
- /* clean up */
- SG_UNREF(ffeats_train);
- SG_UNREF(flabels_train);
- SG_UNREF(stream_features);
- SG_UNREF(stream_labels);
- SG_UNREF(features);
- SG_UNREF(labels);
- SG_UNREF(cfeats);
- SG_UNREF(cker);
- SG_UNREF(mkl);
- }
- int main(int argc, char** argv){
- shogun::init_shogun_with_defaults();
- test_multiclass_mkl();
- exit_shogun();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement