Skip to content

Commit

Permalink
add support for director kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Mar 26, 2012
1 parent 97e8e21 commit dfbb2fd
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 4 deletions.
65 changes: 65 additions & 0 deletions examples/undocumented/python_modular/kernel_director_modular.py
@@ -0,0 +1,65 @@
#import numpy
#from _modshogun import DirectorKernel,Labels,CombinedKernel
#
#class MyKernel(DirectorKernel):
# def init(self, f1,f2):
# print "hallo"
# return true
#
# def compute(self, i,j):
# print i+j
# return i+j
#
#
#
#from tools.load import LoadMatrix
#lm=LoadMatrix()
##
##traindat = lm.load_numbers('../data/fm_train_real.dat')
##testdat = lm.load_numbers('../data/fm_test_real.dat')
#label_traindat = lm.load_labels('../data/label_train_twoclass.dat')
##
##parameter_list = [[traindat,testdat,label_traindat,2.1,1,1e-5],[traindat,testdat,label_traindat,2.2,1,1e-5]]
##
##def classifier_libsvm_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_twoclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
## from shogun.Features import RealFeatures, Labels
## from shogun.Kernel import GaussianKernel
## from shogun.Classifier import LibSVM
##
## feats_train=RealFeatures(fm_train_real)
## feats_train.parallel.set_num_threads(1)
## feats_test=RealFeatures(fm_test_real)
##
## kernel=MyKernel()
## labels=Labels(label_train_twoclass)
##
## svm=LibSVM(C, kernel, labels)
## svm.set_epsilon(epsilon)
## svm.train()
##
## kernel.init(feats_train, feats_test)
## labels = svm.apply().get_labels()
## supportvectors = sv_idx=svm.get_support_vectors()
## alphas=svm.get_alphas()
## predictions = svm.apply()
## return predictions, svm, predictions.get_labels()
##
##if __name__=='__main__':
## print 'LibSVM'
## classifier_libsvm_modular(*parameter_list[0])
#
#labels=Labels(label_traindat)
##kernel=DirectorKernel()
#kernel=MyKernel()
#print kernel.get_cache_size()
#print kernel.get_name()
#k=CombinedKernel()
#k.io.set_loglevel(0)
#k.append_kernel(kernel)
##
#k.list_kernels()
##
##svm=LibSVM(0.1, kernel, labels)
##svm.train()
##print kernel.compute(0,0)
#
9 changes: 9 additions & 0 deletions src/interfaces/modular/Kernel.i
Expand Up @@ -14,6 +14,13 @@

%ignore CWeightedDegreePositionStringKernel::set_position_weights(float64_t*);

%feature("director") shogun::CDirectorKernel;
%feature("director:except") {
if ($error != NULL) {
throw Swig::DirectorMethodException();
}
}

/* Remove C Prefix */
%rename(Kernel) CKernel;
%rename(KernelNormalizer) CKernelNormalizer;
Expand All @@ -40,6 +47,7 @@
%rename(DistanceKernel) CDistanceKernel;
%rename(FixedDegreeStringKernel) CFixedDegreeStringKernel;
%rename(GaussianKernel) CGaussianKernel;
%rename(DirectorKernel) CDirectorKernel;
%rename(WaveletKernel) CWaveletKernel;
%rename(GaussianShiftKernel) CGaussianShiftKernel;
%rename(GaussianShortRealKernel) CGaussianShortRealKernel;
Expand Down Expand Up @@ -155,6 +163,7 @@ namespace shogun
%include <shogun/kernel/ExponentialKernel.h>
%include <shogun/kernel/FixedDegreeStringKernel.h>
%include <shogun/kernel/GaussianKernel.h>
%include <shogun/kernel/DirectorKernel.h>
%include <shogun/kernel/GaussianShiftKernel.h>
%include <shogun/kernel/GaussianShortRealKernel.h>
%include <shogun/kernel/HistogramIntersectionKernel.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Kernel_includes.i
Expand Up @@ -24,6 +24,7 @@
#include <shogun/kernel/DistanceKernel.h>
#include <shogun/kernel/FixedDegreeStringKernel.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/kernel/DirectorKernel.h>
#include <shogun/kernel/GaussianShiftKernel.h>
#include <shogun/kernel/GaussianShortRealKernel.h>
#include <shogun/kernel/HistogramIntersectionKernel.h>
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/modular/SGBase.i
Expand Up @@ -228,6 +228,12 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav
{
$action
}
#ifdef SWIGPYTHON
catch (Swig::DirectorException &e)
{
SWIG_fail;
}
#endif
catch (std::bad_alloc)
{
SWIG_exception(SWIG_MemoryError, const_cast<char*>("Out of memory error.\n"));
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/modular/modshogun.i
Expand Up @@ -11,7 +11,7 @@
"The `modshogun` module gathers all modules available in the SHOGUN toolkit."
%enddef

%module(docstring=DOCSTR) modshogun
%module(directors="1", docstring=DOCSTR) modshogun
#undef DOCSTR

%include "modshogun_ignores.i"
Expand Down
99 changes: 99 additions & 0 deletions src/shogun/kernel/DirectorKernel.h
@@ -0,0 +1,99 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Soeren Sonnenburg
* Copyright (C) 2011 Soeren Sonnenburg
*/

#ifndef _DIRECTORKERNEL_H___
#define _DIRECTORKERNEL_H___

#include <shogun/lib/common.h>
#include <shogun/kernel/Kernel.h>

namespace shogun
{
class CDirectorKernel: public CKernel
{
public:
/** default constructor
*
*/
CDirectorKernel() : CKernel()
{
}

/** constructor
*
*/
CDirectorKernel(int32_t size) : CKernel(size)
{
}

/** default constructor
*
*/
virtual ~CDirectorKernel()
{
cleanup();
}

/** initialize kernel
*
* @param l features of left-hand side
* @param r features of right-hand side
* @return if initializing was successful
*/
virtual bool init(CFeatures* l, CFeatures* r)
{
}

/** clean up kernel */
virtual void cleanup()
{
}

/** return what type of kernel we are
*
* @return kernel type DIRECTOR
*/
virtual EKernelType get_kernel_type() { return K_DIRECTOR; }

/** return what type of features kernel can deal with
*
* @return feature type ANY
*/
virtual EFeatureType get_feature_type() { return F_ANY; }

/** return what class of features kernel can deal with
*
* @return feature class ANY
*/
virtual EFeatureClass get_feature_class() { return C_ANY; }

/** return the kernel's name
*
* @return name Director
*/
inline virtual const char* get_name() const { return "DirectorKernel"; }

protected:
/** compute kernel function for features a and b
* idx_{a,b} denote the index of the feature vectors
* in the corresponding feature object
*
* @param idx_a index a
* @param idx_b index b
* @return computed kernel function at indices a,b
*/
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
{
SG_ERROR("Compute method of Director Kernel needs to be overridden.\n");
return 0;
}
};
}
#endif /* _GAUSSIANKERNEL_H__ */
1 change: 1 addition & 0 deletions src/shogun/kernel/Kernel.cpp
Expand Up @@ -752,6 +752,7 @@ void CKernel::list_kernel()
ENUM_CASE(K_DISTANTSEGMENTS)
ENUM_CASE(K_BESSEL)
ENUM_CASE(K_JENSENSHANNON)
ENUM_CASE(K_DIRECTOR)
}

switch (get_feature_class())
Expand Down
5 changes: 2 additions & 3 deletions src/shogun/kernel/Kernel.h
Expand Up @@ -24,8 +24,6 @@
#include <shogun/features/Features.h>
#include <shogun/kernel/KernelNormalizer.h>

#include <vector>

namespace shogun
{
class CFile;
Expand Down Expand Up @@ -107,7 +105,8 @@ enum EKernelType
K_INVERSEMULTIQUADRIC = 440,
K_DISTANTSEGMENTS = 450,
K_BESSEL = 460,
K_JENSENSHANNON = 470
K_JENSENSHANNON = 470,
K_DIRECTOR = 480
};

/** kernel property */
Expand Down
1 change: 1 addition & 0 deletions src/shogun/kernel/SpectrumMismatchRBFKernel.h
Expand Up @@ -21,6 +21,7 @@
#include <shogun/lib/Array.h>
#include <shogun/lib/Array2.h>
#include <string>
#include <vector>

namespace shogun
{
Expand Down

0 comments on commit dfbb2fd

Please sign in to comment.