From 5158986a4aa12ad68c6343fc4de8f797bcbade0b Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Wed, 21 Feb 2018 11:24:57 +0000 Subject: [PATCH] port kernel_svm meta example to use put/get --- .../binary/kernel_support_vector_machine.rst | 9 +++------ .../src/binary/kernel_support_vector_machine.sg | 15 ++++++--------- src/shogun/classifier/svm/LibSVM.cpp | 5 ++++- src/shogun/kernel/Kernel.cpp | 15 +++++++++++---- src/shogun/machine/Machine.cpp | 8 ++++++-- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst b/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst index b3eee1371f4..22182c76dc4 100644 --- a/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst +++ b/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst @@ -28,15 +28,12 @@ Imagine we have files with training and test data. We create CDenseFeatures (her .. sgexample:: kernel_support_vector_machine.sg:create_features -In order to run :sgclass:`CLibSVM`, we need to initialize a kernel like :sgclass:`CGaussianKernel` with training features and some parameters like :math:`C` and epsilon i.e. residual convergence parameter which is optional. - -.. sgexample:: kernel_support_vector_machine.sg:set_parameters - -We create an instance of the :sgclass:`CLibSVM` classifier by passing it regularization coefficient, kernel and labels. +In order to run :sgclass:`CLibSVM`, we first need to initialize a :sgclass:`CKernel` instance, such as :sgclass:`CGaussianKernel`. +We then create a :sgclass:`CKernelMachine` instance, here :sgclass:`CLibSVM`, and provide it with parameters like the regularization coefficient :math:`C`, the kernel, the training labels, and an optional residual convergence parameter epsilon. .. sgexample:: kernel_support_vector_machine.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CBinaryLabels`. +Then we train it on training data and apply it to test data, which here gives :sgclass:`CBinaryLabels`. .. sgexample:: kernel_support_vector_machine.sg:train_and_apply diff --git a/examples/meta/src/binary/kernel_support_vector_machine.sg b/examples/meta/src/binary/kernel_support_vector_machine.sg index 55d249ac2c6..629c8b2fa87 100644 --- a/examples/meta/src/binary/kernel_support_vector_machine.sg +++ b/examples/meta/src/binary/kernel_support_vector_machine.sg @@ -11,24 +11,21 @@ BinaryLabels labels_test(f_labels_test) #![create_features] #![set_parameters] -real C = 1.0 -real epsilon = 0.001 -GaussianKernel gauss_kernel(features_train, features_train, 15) +Kernel k = kernel("GaussianKernel", log_width=1.0074515102711323) #![set_parameters] #![create_instance] -LibSVM svm(C, gauss_kernel, labels_train) -svm.set_epsilon(epsilon) +KernelMachine svm = kernel_machine("LibSVM", C1=1.0, C2=1.0, kernel=k, labels=labels_train, epsilon=0.001) #![create_instance] #![train_and_apply] -svm.train() +svm.train(features_train) BinaryLabels labels_predict = svm.apply_binary(features_test) #![train_and_apply] #![extract_weights_bias] -RealVector alphas = svm.get_alphas() -real b = svm.get_bias() +RealVector alphas = svm.get_real_vector("m_alpha") +real b = svm.get_real("m_bias") #![extract_weights_bias] #![evaluate_accuracy] @@ -37,4 +34,4 @@ real accuracy = eval.evaluate(labels_predict, labels_test) #![evaluate_accuracy] # additional integration testing variables -RealVector output = labels_predict.get_labels() +RealVector output = labels_predict.get_real_vector("labels") diff --git a/src/shogun/classifier/svm/LibSVM.cpp b/src/shogun/classifier/svm/LibSVM.cpp index 5a7bb27935b..e2d8a317303 100644 --- a/src/shogun/classifier/svm/LibSVM.cpp +++ b/src/shogun/classifier/svm/LibSVM.cpp @@ -60,10 +60,14 @@ bool CLibSVM::train_machine(CFeatures* data) } kernel->init(data, data); } + REQUIRE(kernel->get_num_vec_lhs()==m_labels->get_num_labels(), + "Number of training data (%d) must match number of labels (%d)\n", + kernel->get_num_vec_lhs(), m_labels->get_num_labels()) problem.l=m_labels->get_num_labels(); SG_INFO("%d trainlabels\n", problem.l) + // set linear term if (m_linear_term.vlen>0) { @@ -100,7 +104,6 @@ bool CLibSVM::train_machine(CFeatures* data) float64_t weights[2]={1.0,get_C2()/get_C1()}; ASSERT(kernel && kernel->has_features()) - ASSERT(kernel->get_num_vec_lhs()==problem.l) switch (solver_type) { diff --git a/src/shogun/kernel/Kernel.cpp b/src/shogun/kernel/Kernel.cpp index 15e4d47d7aa..a2edaddce45 100644 --- a/src/shogun/kernel/Kernel.cpp +++ b/src/shogun/kernel/Kernel.cpp @@ -925,10 +925,17 @@ void CKernel::save_serializable_post() throw (ShogunException) void CKernel::register_params() { SG_ADD(&cache_size, "cache_size", "Cache size in MB.", MS_NOT_AVAILABLE); - SG_ADD((CSGObject**) &lhs, "lhs", - "Feature vectors to occur on left hand side.", MS_NOT_AVAILABLE); - SG_ADD((CSGObject**) &rhs, "rhs", - "Feature vectors to occur on right hand side.", MS_NOT_AVAILABLE); + +// SG_ADD((CSGObject**) &lhs, "lhs", +// "Feature vectors to occur on left hand side.", MS_NOT_AVAILABLE); + m_parameters->add((CSGObject**)&lhs, "lhs", "Feature vectors to occur on left hand side."); + watch_param("lhs", &lhs, AnyParameterProperties("Feature vectors to occur on left hand side.")); + +// SG_ADD((CSGObject**) &rhs, "rhs", +// "Feature vectors to occur on right hand side.", MS_NOT_AVAILABLE); + m_parameters->add((CSGObject**)&rhs, "rhs", "Feature vectors to occur on right hand side."); + watch_param("rhs", &rhs, AnyParameterProperties("Feature vectors to occur on right hand side.")); + SG_ADD(&lhs_equals_rhs, "lhs_equals_rhs", "If features on lhs are the same as on rhs.", MS_NOT_AVAILABLE); SG_ADD(&num_lhs, "num_lhs", "Number of feature vectors on left hand side.", diff --git a/src/shogun/machine/Machine.cpp b/src/shogun/machine/Machine.cpp index 88e474325a7..c4bc6487319 100644 --- a/src/shogun/machine/Machine.cpp +++ b/src/shogun/machine/Machine.cpp @@ -24,8 +24,12 @@ CMachine::CMachine() "Maximum training time.", MS_NOT_AVAILABLE); SG_ADD((machine_int_t*) &m_solver_type, "solver_type", "Type of solver.", MS_NOT_AVAILABLE); - SG_ADD((CSGObject**) &m_labels, "labels", - "Labels to be used.", MS_NOT_AVAILABLE); + +// SG_ADD((CSGObject**) &m_labels, "labels", +// "Labels to be used.", MS_NOT_AVAILABLE); + m_parameters->add((CSGObject**)&m_labels, "labels", "Labels to be used."); + watch_param("labels", &m_labels, AnyParameterProperties("Labels to be used.")); + SG_ADD(&m_store_model_features, "store_model_features", "Should feature data of model be stored after training?", MS_NOT_AVAILABLE); SG_ADD(&m_data_locked, "data_locked",