From 897bfd16879d7755fbcce8f6217610c9d76f883f Mon Sep 17 00:00:00 2001 From: MikeLing Date: Thu, 22 Jun 2017 20:38:57 +0800 Subject: [PATCH] add unittest for SVM Light --- .../unit/classifier/svm/SVMLight_unittest.cc | 71 +++++++++++++++++++ tests/unit/classifier/svm/SVMOcas_unittest.cc | 2 +- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/unit/classifier/svm/SVMLight_unittest.cc diff --git a/tests/unit/classifier/svm/SVMLight_unittest.cc b/tests/unit/classifier/svm/SVMLight_unittest.cc new file mode 100644 index 00000000000..7c00f61b013 --- /dev/null +++ b/tests/unit/classifier/svm/SVMLight_unittest.cc @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016, Shogun-Toolbox e.V. + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: 2016 MikeLing, Viktor Gal, Sergey Lisitsyn, Heiko Strathmann + */ + +#include +#include +#include +#include +#include + +#include "environments/LinearTestEnvironment.h" + +using namespace shogun; +#ifdef USE_SVMLIGHT +TEST(SVMLight, train) +{ + auto C = 1.0; + auto epsilon = 0.001; + std::shared_ptr mockData = + LinearTestEnvironment::instance().getBinaryLabelData(); + + CDenseFeatures* train_feats = mockData->get_features_train(); + CDenseFeatures* test_feats = mockData->get_features_test(); + + CBinaryLabels* ground_truth = (CBinaryLabels*)mockData->get_labels_train(); + + CGaussianKernel* gauss_kernel = + new CGaussianKernel(train_feats, train_feats, 15); + CSVMLight* svml = new CSVMLight(C, gauss_kernel, ground_truth); + + svml->set_epsilon(epsilon); + svml->train(); + + CLabels* pred = svml->apply(test_feats); + + CAccuracyMeasure evaluate = CAccuracyMeasure(); + evaluate.evaluate(pred, mockData->get_labels_test()); + EXPECT_GT(evaluate.get_accuracy(), 0.99); + + SG_UNREF(svml); + SG_UNREF(pred); +} +#endif // USE_SVMLIGHT diff --git a/tests/unit/classifier/svm/SVMOcas_unittest.cc b/tests/unit/classifier/svm/SVMOcas_unittest.cc index 27d294df9ef..1559bde562c 100644 --- a/tests/unit/classifier/svm/SVMOcas_unittest.cc +++ b/tests/unit/classifier/svm/SVMOcas_unittest.cc @@ -20,7 +20,7 @@ TEST(SVMOcasTest,train) CDenseFeatures* train_feats = mockData->get_features_train(); CDenseFeatures* test_feats = mockData->get_features_test(); - CBinaryLabels* ground_truth = (CBinaryLabels*)mockData->get_labels_test(); + CBinaryLabels* ground_truth = (CBinaryLabels*)mockData->get_labels_train(); CSVMOcas* ocas = new CSVMOcas(1.0, train_feats, ground_truth); ocas->parallel->set_num_threads(1);