void test() { /* dense features from matrix */ CAsciiFile* feature_file = new CAsciiFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CAsciiFile* label_file = new CAsciiFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); // Create liblinear svm classifier with L2-regularized L2-loss CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); SG_REF(svm); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(true); CECOCDiscriminantEncoder *encoder = new CECOCDiscriminantEncoder(); encoder->set_features(features); encoder->set_labels(labels); // Create a multiclass svm classifier that consists of several of the previous one CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( new CECOCStrategy(encoder, new CECOCHDDecoder()), (CDotFeatures*) features, svm, labels); SG_REF(mc_svm); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results CMulticlassLabels* output = CLabelsFactory::to_multiclass(mc_svm->apply()); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::display_vector(out_labels.vector, out_labels.vlen); // Free resources SG_UNREF(mc_svm); SG_UNREF(svm); SG_UNREF(output); SG_UNREF(features); SG_UNREF(labels); }
int main(int argc, char** argv) { int32_t num_vectors = 0; int32_t num_feats = 2; init_shogun_with_defaults(); // Prepare to read a file for the training 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); 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); // Create a matrix with enough space to read all the feature vectors SGMatrix< float64_t > mat = SGMatrix< float64_t >(num_feats, 1000); // Read the values from the file and store them in mat 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[num_vectors*num_feats + i] = vec[i]; num_vectors++; stream_features->release_example(); } stream_features->end_parser(); // Create features with the useful values from mat CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(mat.matrix, num_feats, num_vectors); CLabels* labels = new CLabels(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(); // Create liblinear svm classifier with L2-regularized L2-loss CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); SG_REF(svm); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(true); // Create a multiclass svm classifier that consists of several of the previous one CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( new CECOCStrategy(new CECOCRandomDenseEncoder(), new CECOCHDDecoder()), (CDotFeatures*) features, svm, labels); SG_REF(mc_svm); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results CLabels* output = mc_svm->apply(); SGVector< int32_t > out_labels = output->get_int_labels(); CMath::display_vector(out_labels.vector, out_labels.vlen); // Free resources SG_UNREF(mc_svm); SG_UNREF(svm); SG_UNREF(output); SG_UNREF(features); SG_UNREF(labels); //SG_UNREF(ffeats_train); //SG_UNREF(flabels_train); SG_UNREF(stream_features); SG_UNREF(stream_labels); exit_shogun(); return 0; }
void test() { // Prepare to read a file for the training 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); 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); stream_features->start_parser(); // Read the values from the file and store them in features CDenseFeatures< float64_t >* features= (CDenseFeatures< float64_t >*) stream_features->get_streamed_features(1000); stream_features->end_parser(); CMulticlassLabels* labels = new CMulticlassLabels(features->get_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(); // Create liblinear svm classifier with L2-regularized L2-loss CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); SG_REF(svm); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(true); CECOCDiscriminantEncoder *encoder = new CECOCDiscriminantEncoder(); encoder->set_features(features); encoder->set_labels(labels); // Create a multiclass svm classifier that consists of several of the previous one CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( new CECOCStrategy(encoder, new CECOCHDDecoder()), (CDotFeatures*) features, svm, labels); SG_REF(mc_svm); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results CMulticlassLabels* output = CMulticlassLabels::obtain_from_generic(mc_svm->apply()); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::display_vector(out_labels.vector, out_labels.vlen); // Free resources SG_UNREF(mc_svm); SG_UNREF(svm); SG_UNREF(output); SG_UNREF(features); SG_UNREF(labels); SG_UNREF(ffeats_train); SG_UNREF(flabels_train); SG_UNREF(stream_features); SG_UNREF(stream_labels); }
int main(int argc, char ** argv) { init_shogun_with_defaults(); SGVector< float64_t > labs(NUM_CLASSES*NUM_SAMPLES); SGMatrix< float64_t > feats(DIMS, NUM_CLASSES*NUM_SAMPLES); gen_rand_data(labs, feats); //read_data(labs, feats); // Create train labels CMulticlassSOLabels* labels = new CMulticlassSOLabels(labs); CMulticlassLabels* mlabels = new CMulticlassLabels(labs); // Create train features CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feats); // Create structured model CMulticlassModel* model = new CMulticlassModel(features, labels); // Create loss function CHingeLoss* loss = new CHingeLoss(); // Create SO-SVM CPrimalMosekSOSVM* sosvm = new CPrimalMosekSOSVM(model, loss, labels); CDualLibQPBMSOSVM* bundle = new CDualLibQPBMSOSVM(model, loss, labels, 1000); bundle->set_verbose(false); SG_REF(sosvm); SG_REF(bundle); CTime start; float64_t t1; sosvm->train(); SG_SPRINT(">>>> PrimalMosekSOSVM trained in %9.4f\n", (t1 = start.cur_time_diff(false))); bundle->train(); SG_SPRINT(">>>> BMRM trained in %9.4f\n", start.cur_time_diff(false)-t1); CStructuredLabels* out = CStructuredLabels::obtain_from_generic(sosvm->apply()); CStructuredLabels* bout = CStructuredLabels::obtain_from_generic(bundle->apply()); // Create liblinear svm classifier with L2-regularized L2-loss CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(false); // Create a multiclass svm classifier that consists of several of the previous one CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( new CMulticlassOneVsRestStrategy(), (CDotFeatures*) features, svm, mlabels); SG_REF(mc_svm); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); CMulticlassLabels* mout = CMulticlassLabels::obtain_from_generic(mc_svm->apply()); SGVector< float64_t > w = sosvm->get_w(); for ( int32_t i = 0 ; i < w.vlen ; ++i ) SG_SPRINT("%10f ", w[i]); SG_SPRINT("\n\n"); for ( int32_t i = 0 ; i < NUM_CLASSES ; ++i ) { CLinearMachine* lm = (CLinearMachine*) mc_svm->get_machine(i); SGVector< float64_t > mw = lm->get_w(); for ( int32_t j = 0 ; j < mw.vlen ; ++j ) SG_SPRINT("%10f ", mw[j]); SG_UNREF(lm); // because of CLinearMulticlassMachine::get_machine() } SG_SPRINT("\n"); CStructuredAccuracy* structured_evaluator = new CStructuredAccuracy(); CMulticlassAccuracy* multiclass_evaluator = new CMulticlassAccuracy(); SG_REF(structured_evaluator); SG_REF(multiclass_evaluator); SG_SPRINT("SO-SVM: %5.2f%\n", 100.0*structured_evaluator->evaluate(out, labels)); SG_SPRINT("BMRM: %5.2f%\n", 100.0*structured_evaluator->evaluate(bout, labels)); SG_SPRINT("MC: %5.2f%\n", 100.0*multiclass_evaluator->evaluate(mout, mlabels)); // Free memory SG_UNREF(multiclass_evaluator); SG_UNREF(structured_evaluator); SG_UNREF(mout); SG_UNREF(mc_svm); SG_UNREF(bundle); SG_UNREF(sosvm); SG_UNREF(bout); SG_UNREF(out); exit_shogun(); return 0; }