{"body":"/*\n * This program is free software; you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation; either version 3 of the License, or\n * (at your option) any later version.\n *\n * Written (W) 2013 Evangelos Anagnostopoulos\n */\n#include <shogun/base/init.h>\n#include <shogun/features/RandomFourierDotFeatures.h>\n#include <shogun/features/SparseFeatures.h>\n#include <shogun/io/LibSVMFile.h>\n#include <shogun/labels/BinaryLabels.h>\n#include <shogun/labels/MulticlassLabels.h>\n#include <shogun/classifier/svm/SVMOcas.h>\n#include <shogun/classifier/svm/LibLinear.h>\n#include <shogun/classifier/LPBoost.h>\n#include <shogun/evaluation/PRCEvaluation.h>\n#include <shogun/evaluation/ROCEvaluation.h>\n#include <shogun/evaluation/ContingencyTableEvaluation.h>\n#include <shogun/lib/Time.h>\n\n#include <stdio.h>\n\nusing namespace shogun;\n\nconst char* filepath = 0;\nconst char* testpath = 0;\nint32_t D = 300;\nfloat64_t C = 0.1;\nfloat64_t epsilon = 0.01;\nfloat64_t width = 8;\nint32_t correct_dimension = -1;\n\nSGSparseMatrix<float64_t> load_data(const char* filepath, float64_t*& label_vec)\n{\n\tFILE* data_file = fopen(filepath, \"r\");\n\tSGSparseMatrix<float64_t> sparse_data;\n\n\tCLibSVMFile* file_reader = new CLibSVMFile(data_file);\n\tfile_reader->get_sparse_matrix(sparse_data.sparse_matrix, sparse_data.num_features, sparse_data.num_vectors,\n\t\t\tlabel_vec);\n\n\tif (correct_dimension!=-1)\n\t\tsparse_data.num_features = correct_dimension;\n\n\tSG_UNREF(file_reader);\n\n\treturn sparse_data;\n}\n\nvoid print_help_message()\n{\n\tSG_SPRINT(\"Usage : ./rf_classify --dataset path_to_data [--testset path_to_test_data] [-D number_of_samples]\\n\");\n\tSG_SPRINT(\"\t\t[-C C_for_SVM] [--epsilon SVM_epsilon] [--width gaussian_kernel_width] [--dimension feature_dimension]\\n\");\n\tSG_SPRINT(\"\\nPerforms binary classification on provided data using Random Fourier features with a linear SVM solver,\\n\");\n\tSG_SPRINT(\"namely SVMOcas.\\nParameter explanation :\\n\");\n\tSG_SPRINT(\"\\ndataset  : Path to data in LibSVM format. Required.\");\n\tSG_SPRINT(\"\\ntestset  : Path to test data in LibSVM format. Optional.\");\n\tSG_SPRINT(\"\\nD         : Number of samples for the Random Fourier features. Default value = 300\");\n\tSG_SPRINT(\"\\nC         : SVM parameter C. Default value = 0.1\");\n\tSG_SPRINT(\"\\nepsilon   : SVM epsilon. Default value = 0.01\");\n\tSG_SPRINT(\"\\nwidth     : Gaussian Kernel width parameter. Default value = 8\");\n\tSG_SPRINT(\"\\ndimension : Correct feature dimension. Optional\\n\");\n}\n\nvoid parse_arguments(int argv, char** argc)\n{\n\tif (argv%2!=1)\n\t{\n\t\tprint_help_message();\n\t\texit_shogun();\n\t\texit(0);\n\t}\n\n\tfor (index_t i=1; i<argv; i++)\n\t{\n\t\tif (strcmp(argc[i],\"--dataset\")==0)\n\t\t\tfilepath = argc[++i];\n\t\telse if (strcmp(argc[i],\"--testset\")==0)\n\t\t\ttestpath = argc[++i];\n\t\telse if (strcmp(argc[i],\"-D\")==0)\n\t\t\tD = atoi(argc[++i]);\n\t\telse if (strcmp(argc[i],\"-C\")==0)\n\t\t\tC = atof(argc[++i]);\n\t\telse if (strcmp(argc[i],\"--epsilon\")==0)\n\t\t\tepsilon = atof(argc[++i]);\n\t\telse if (strcmp(argc[i],\"--width\")==0)\n\t\t\twidth = atof(argc[++i]);\n\t\telse if (strcmp(argc[i],\"--dimension\")==0)\n\t\t\tcorrect_dimension = atoi(argc[++i]);\n\t}\n\n\tif (filepath==0)\n\t{\n\t\tprint_help_message();\n\t\texit_shogun();\n\t\texit(0);\n\t}\n}\n\nint main(int argv, char** argc)\n{\n\tinit_shogun_with_defaults();\n\n\tparse_arguments(argv, argc);\n\n\t/** Reading data */\n\tfloat64_t* label_vec = 0;\n\tSGSparseMatrix<float64_t> sparse_data = load_data(filepath, label_vec);\n\tSGVector<float64_t> label(label_vec, sparse_data.num_vectors);\n\n\n\t/** Creating features */\n\tCBinaryLabels* labels = new CBinaryLabels(label);\n\tSG_REF(labels);\n\n\tCSparseFeatures<float64_t>* s_feats = new CSparseFeatures<float64_t>(sparse_data);\n\tSGVector<float64_t> params(1);\n\tparams[0] = width;\n\t//CRandomFourierDotFeatures* r_feats = new CRandomFourierDotFeatures(\n\t\t\t//s_feats, D, KernelName::GAUSSIAN, params);\n\n\n\t/** Training */\n    float64_t svm_C1=1.0;\n    float64_t svm_C2=1.0;\n    float64_t svm_epsilon = 1e-5;\n    float64_t max_train_time = 0;\n    bool svm_use_bias = true;\n\tCLPBoost* svm = new CLPBoost();\n    svm->set_features(s_feats);\n    svm->set_labels(labels);\n    svm->set_C(svm_C1, svm_C2);\n    svm->set_epsilon(svm_epsilon);\n    svm->set_bias_enabled(svm_use_bias);\n    svm->set_max_train_time(max_train_time);\n\n\t//CLPBoost* svm = new CLPBoost(C, s_feats, labels);\n\t//CLibLinear* svm = new CLibLinear(C, s_feats, labels);\n\t//CSVMOcas* svm = new CSVMOcas(C, r_feats, labels);\n\tsvm->set_epsilon(epsilon);\n\tSG_SPRINT(\"Starting training\\n\");\n\tCTime* timer = new CTime();\n\t//svm->train();\n\tfloat64_t secs = timer->cur_runtime_diff_sec();\n\ttimer->stop();\n\tSG_UNREF(timer);\n\tSG_SPRINT(\"Training completed, took %fs\\n\", secs);\n\t/** Training completed */\n\n\tSG_UNREF(svm);\n\tSG_UNREF(labels);\n\texit_shogun();\n}\n","name":"","extension":"txt","url":"https://www.irccloud.com/pastebin/Jw49xhzS","modified":1427653459,"id":"Jw49xhzS","size":4768,"lines":155,"own_paste":false,"theme":"","date":1427653459}