Skip to content

Commit

Permalink
port custom kernel meta example
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed May 9, 2018
1 parent 54e6d3a commit fbaa510
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
2 changes: 1 addition & 1 deletion data
9 changes: 8 additions & 1 deletion examples/meta/src/base_api/factory.sg
@@ -1,12 +1,19 @@
RealMatrix real_matrix(2,2)

Machine lib_svm = machine("LibSVM")
Machine lda = machine("LDA")
Kernel kernel_gaussian = kernel("GaussianKernel")
Kernel kernel_linear = kernel("LinearKernel")
Kernel kernel_from_object = kernel(kernel_gaussian)
Kernel kernel_custom_from_matrix = kernel(real_matrix)

RealMatrix real_matrix(2,2)
Features features_from_matrix = features(real_matrix)

IntVector dims(2)
dims[0] = 0
dims[1] = 1
Features features_with_subset = features_subset(features_from_matrix, dims)

File features_file = csv_file("../../data/classifier_binary_2d_nonlinear_features_train.dat")
Features features_from_file = features(features_file)

Expand Down
39 changes: 39 additions & 0 deletions examples/meta/src/kernel/custom_kernel_machine.sg
@@ -0,0 +1,39 @@
File f_feats_train = csv_file("../../data/classifier_binary_2d_nonlinear_features_train.dat")
File f_feats_test = csv_file("../../data/classifier_binary_2d_nonlinear_features_test.dat")
File f_labels_train = csv_file("../../data/classifier_binary_2d_nonlinear_labels_train.dat")
File f_labels_test = csv_file("../../data/classifier_binary_2d_nonlinear_labels_test.dat")

#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![create_kernel]
Kernel k_obj = kernel("GaussianKernel", log_width=1.0)
k_obj.init(features_train, features_train)
RealMatrix km_train = k_obj.get_kernel_matrix()
k_obj.init(features_train, features_test)
RealMatrix km_test = k_obj.get_kernel_matrix()
Kernel k_train = kernel(km_train)
Kernel k_test = kernel(km_test)
#![create_kernel]

#![create_machine]
Machine svm = machine("LibSVM", C1=1.0, C2=1.0, kernel=k_train, labels=labels_train, epsilon=0.001)
#![create_machine]

#![train_and_apply]
svm.train()
svm.put("kernel", k_test)
Labels labels_predict = svm.apply()
RealVector labels = labels_predict.get_real_vector("labels")
#![train_and_apply]

#![evaluate_accuracy]
Evaluation eval = evaluation("AccuracyMeasure")
real accuracy = eval.evaluate(labels_predict, labels_test)
#![evaluate_accuracy]


26 changes: 0 additions & 26 deletions examples/undocumented/python/classifier_custom_kernel.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/interfaces/swig/shogun.i
Expand Up @@ -253,4 +253,8 @@ PUT_ADD(CECOCEncoder)
PUT_ADD(CECOCDecoder)
PUT_ADD(CMulticlassStrategy)

%template(kernel) kernel<float64_t, float64_t>;
%template(features) features<float64_t>;


} // namespace shogun
13 changes: 11 additions & 2 deletions src/shogun/util/factory.h
Expand Up @@ -13,13 +13,13 @@
#include <shogun/features/DenseSubsetFeatures.h>
#include <shogun/io/CSVFile.h>
#include <shogun/io/SGIO.h>
#include <shogun/kernel/CustomKernel.h>
#include <shogun/kernel/Kernel.h>
#include <shogun/labels/DenseLabels.h>
#include <shogun/machine/Machine.h>
#include <shogun/multiclass/MulticlassStrategy.h>
#include <shogun/multiclass/ecoc/ECOCEncoder.h>
#include <shogun/multiclass/ecoc/ECOCDecoder.h>

#include <shogun/multiclass/ecoc/ECOCEncoder.h>

namespace shogun
{
Expand Down Expand Up @@ -104,6 +104,15 @@ namespace shogun
return result;
}

template <typename T, typename T2 = typename std::enable_if_t<
std::is_floating_point<T>::value>>
CKernel* kernel(SGMatrix<T> kernel_matrix)
{
CKernel* result = new CCustomKernel(kernel_matrix);
SG_REF(result);
return result;
}

CLabels* labels(CFile* file)
{
REQUIRE(file, "No file provided.\n");
Expand Down

0 comments on commit fbaa510

Please sign in to comment.