Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
get minimal director dot features example to work
  • Loading branch information
Soeren Sonnenburg committed May 31, 2012
1 parent ea40ed5 commit 018e453
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
@@ -0,0 +1,40 @@
import numpy
try:
from shogun.Features import DirectorDotFeatures
except ImportError:
print "recompile shogun with --enable-swig-directors"
import sys
sys.exit(0)

data=None

class NumpyFeatures(DirectorDotFeatures):
def __init__(self, d):
global data
data = d
DirectorDotFeatures.__init__(self)

def add_to_dense_sgvec(self, alpha, vec_idx1, vec2, abs):
vec2+=alpha*numpy.abs(data[vec_idx1])

def get_num_vectors(self):
return data.shape[0]

def get_dim_feature_space(self):
return data.shape[1]

traindat = numpy.random.random_sample((10,10))
parameter_list=[[traindat]]

def features_director_dot_modular (fm_train_real=traindat):
feats_train=NumpyFeatures(fm_train_real)
feats_train.io.enable_file_and_line()
data=feats_train.get_computed_dot_feature_matrix()
print data
print fm_train_real

return data

if __name__=='__main__':
print('DirectorLinear')
features_director_dot_modular(*parameter_list[0])
4 changes: 4 additions & 0 deletions src/shogun/features/DirectorDotFeatures.h
Expand Up @@ -12,6 +12,9 @@
#ifndef _DIRECTORDOTFEATURES_H___
#define _DIRECTORDOTFEATURES_H___

#include <shogun/lib/config.h>

#ifdef USE_SWIG_DIRECTORS
#include <shogun/lib/common.h>
#include <shogun/features/Features.h>
#include <shogun/features/DotFeatures.h>
Expand Down Expand Up @@ -374,4 +377,5 @@ class CDirectorDotFeatures : public CDotFeatures
inline virtual const char* get_name() const { return "DirectorDotFeatures"; }
};
}
#endif // USE_SWIG_DIRECTORS
#endif // _DIRECTORDOTFEATURES_H___
27 changes: 26 additions & 1 deletion src/shogun/lib/SGVector.h
Expand Up @@ -88,7 +88,7 @@ template<class T> class SGVector : public SGReferencedData
}

/** clone vector */
SGVector<T> clone()
SGVector<T> clone() const
{
return SGVector<T>(clone_vector(vector, vlen), vlen);
}
Expand Down Expand Up @@ -202,6 +202,31 @@ template<class T> class SGVector : public SGReferencedData
return vector[index];
}

void add(const SGVector<T> x)
{
ASSERT(x.vector && vector);
ASSERT(x.vlen == vlen);

for (int32_t i=0; i<vlen; i++)
vector[i]+=x.vector[i];
}

SGVector<T> operator+ (SGVector<T> x)
{
ASSERT(x.vector && vector);
ASSERT(x.vlen == vlen);

SGVector<T> result=clone();
result.add(x);
return result;
}

SGVector<T> operator+= (SGVector<T> x)
{
add(x);
return *this;
}

/** display array size */
void display_size() const
{
Expand Down

0 comments on commit 018e453

Please sign in to comment.