Skip to content

Commit

Permalink
Merge pull request #557 from gsomix/director_kernel
Browse files Browse the repository at this point in the history
preliminary version of DirectorKernel
  • Loading branch information
Soeren Sonnenburg committed May 28, 2012
2 parents d8a7270 + 92b1eec commit 2dc985c
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 44 deletions.
@@ -0,0 +1,60 @@
import numpy
from shogun.Kernel import DirectorKernel

class DirectorLinearKernel(DirectorKernel):
def __init__(self):
DirectorKernel.__init__(self)
def has_features(self):
return True
def kernel_function(self, idx_a, idx_b):
return numpy.dot(traindat[:,idx_a], traindat[:,idx_b])

traindat = numpy.random.random_sample((10,10))
testdat = numpy.random.random_sample((5,5))
parameter_list=[[traindat,testdat,1.2],[traindat,testdat,1.4]]

def kernel_director_linear_modular (fm_train_real=traindat,fm_test_real=testdat,scale=1.2):

from shogun.Features import RealFeatures
from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer
from modshogun import Time

feats_train=RealFeatures(fm_train_real)
feats_train.io.set_loglevel(0)
feats_train.parallel.set_num_threads(1)
feats_test=RealFeatures(fm_test_real)

kernel=LinearKernel()
kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
kernel.init(feats_train, feats_train)

dkernel=DirectorLinearKernel()
dkernel.set_normalizer(AvgDiagKernelNormalizer(scale))
dkernel.set_num_vec_lhs(traindat.shape[0])
dkernel.set_num_vec_rhs(traindat.shape[1])

print "km_train"
t=Time()
km_train=kernel.get_kernel_matrix()
t1=t.cur_time_diff(True)

print "dkm_train"
t=Time()
dkm_train=dkernel.get_kernel_matrix()
t2=t.cur_time_diff(True)

#dkm_train=dkernel.get_kernel_matrix()
#dkm_train=numpy.zeros((5,5))
#for i in xrange(5):
# for j in xrange(5):
# dkm_train[i,j]=dkernel.kernel(i,j)


print "km_train", km_train
print "dkm_train", dkm_train

return km_train, dkm_train

if __name__=='__main__':
print('DirectorLinear')
kernel_director_linear_modular(*parameter_list[0])
111 changes: 79 additions & 32 deletions src/shogun/kernel/DirectorKernel.h
Expand Up @@ -40,7 +40,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel
/** default constructor
*
*/
virtual ~CDirectorKernel()
virtual ~CDirectorKernel()
{
cleanup();
}
Expand All @@ -53,12 +53,44 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel
*/
virtual bool init(CFeatures* l, CFeatures* r)
{
return false;
return CKernel::init(l, r);
}

/** clean up kernel */
/** set the current kernel normalizer
*
* @return if successful
*/
virtual bool set_normalizer(CKernelNormalizer* normalizer)
{
return CKernel::set_normalizer(normalizer);
}

/** obtain the current kernel normalizer
*
* @return the kernel normalizer
*/
virtual CKernelNormalizer* get_normalizer()
{
return CKernel::get_normalizer();
}

/** initialize the current kernel normalizer
* @return if init was successful
*/
virtual bool init_normalizer()
{
return CKernel::init_normalizer();
}

/** clean up your kernel
*
* base method only removes lhs and rhs
* overload to add further cleanup but make sure CKernel::cleanup() is
* called
*/
virtual void cleanup()
{
CKernel::cleanup();
}

virtual float64_t kernel_function(int32_t idx_a, int32_t idx_b)
Expand All @@ -67,32 +99,67 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel
return 0;
}

/** get number of vectors of lhs features
*
* @return number of vectors of left-hand side
*/
virtual int32_t get_num_vec_lhs()
{
return CKernel::get_num_vec_lhs();
}

/** get number of vectors of rhs features
*
* @return number of vectors of right-hand side
*/
virtual int32_t get_num_vec_rhs()
{
return CKernel::get_num_vec_rhs();
}

/** set number of vectors of lhs features
*
* @return number of vectors of left-hand side
*/
virtual void set_num_vec_lhs(int32_t num)
{
num_lhs=num;
}

/** set number of vectors of rhs features
*
* @return number of vectors of right-hand side
*/
virtual void set_num_vec_rhs(int32_t num)
{
num_rhs=num;
}

/** test whether features have been assigned to lhs and rhs
*
* @return true if features are assigned
*/
virtual bool has_features()
{
return true;
return CKernel::has_features();
}

SGMatrix<float64_t> get_km()
/** remove lhs and rhs from kernel */
virtual void remove_lhs_and_rhs()
{
SGMatrix<float64_t> km(num_lhs, num_rhs);
CKernel::remove_lhs_and_rhs();
}

for (int32_t i=0; i<num_lhs; i++)
{
for (int32_t j=0; j<num_rhs; j++)
km[j*num_rhs+i]=kernel(i,j);
}
/** remove lhs from kernel */
virtual void remove_lhs()
{
CKernel::remove_lhs();
}

return km;
/** remove rhs from kernel */
virtual void remove_rhs()
{
CKernel::remove_rhs();
}

/** return what type of kernel we are
Expand Down Expand Up @@ -138,26 +205,6 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel
CKernel::set_subkernel_weights(weights);
}

/**
* get row i
*
* @return the ith row of the kernel matrix
*/
virtual SGVector<float64_t> get_kernel_row(int32_t i)
{
return CKernel::get_kernel_row(i);
}

/**
* get column j
*
* @return the jth column of the kernel matrix
*/
virtual SGVector<float64_t> get_kernel_col(int32_t j)
{
return CKernel::get_kernel_col(j);
}

protected:
/** creates a new TParameter instance, which contains migrated data from
* the version that is provided. The provided parameter data base is used
Expand Down
24 changes: 12 additions & 12 deletions src/shogun/kernel/Kernel.h
Expand Up @@ -245,15 +245,15 @@ class CKernel : public CSGObject
* @return the jth column of the kernel matrix
*/
virtual SGVector<float64_t> get_kernel_col(int32_t j)
{
{

SGVector<float64_t> col = SGVector<float64_t>(num_rhs);
SGVector<float64_t> col = SGVector<float64_t>(num_rhs);

for (int32_t i=0; i!=num_rhs; i++)
col[i] = kernel(i,j);
for (int32_t i=0; i!=num_rhs; i++)
col[i] = kernel(i,j);

return col;
}
return col;
}


/**
Expand All @@ -262,14 +262,14 @@ class CKernel : public CSGObject
* @return the ith row of the kernel matrix
*/
virtual SGVector<float64_t> get_kernel_row(int32_t i)
{
SGVector<float64_t> row = SGVector<float64_t>(num_lhs);
{
SGVector<float64_t> row = SGVector<float64_t>(num_lhs);

for (int32_t j=0; j!=num_lhs; j++)
row[j] = kernel(i,j);
for (int32_t j=0; j!=num_lhs; j++)
row[j] = kernel(i,j);

return row;
}
return row;
}

/** get kernel matrix real
*
Expand Down

0 comments on commit 2dc985c

Please sign in to comment.