Skip to content

Commit

Permalink
Made DimensionReductionPreprocessor class templated, adapted existing…
Browse files Browse the repository at this point in the history
… preprocessors to the new structure
  • Loading branch information
lisitsyn committed Oct 4, 2011
1 parent c0c4231 commit 5fab1f0
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 62 deletions.
9 changes: 7 additions & 2 deletions src/interfaces/modular/Preprocessor.i
Expand Up @@ -16,7 +16,6 @@
%rename(PruneVarSubMean) CPruneVarSubMean;
%rename(RandomFourierGaussPreproc) CRandomFourierGaussPreproc;

%rename(DimensionReductionPreprocessor) CDimensionReductionPreprocessor;
%rename(PCA) CPCA;
%rename(KernelPCA) CKernelPCA;
%rename(MultidimensionalScaling) CMultidimensionalScaling;
Expand Down Expand Up @@ -51,6 +50,13 @@ namespace shogun
%template(SimpleCharPreprocessor) CSimplePreprocessor<char>;
}

/* Templates Class DimensionReductionPreprocessor */
%include <shogun/preprocessor/DimensionReductionPreprocessor.h>
namespace shogun
{
%template(DimensionReductionPreprocessor) CDimensionReductionPreprocessor<float64_t>;
}

/* Templates Class StringPreprocessor*/
%include <shogun/preprocessor/StringPreprocessor.h>
namespace shogun
Expand All @@ -76,7 +82,6 @@ namespace shogun
%include <shogun/preprocessor/PruneVarSubMean.h>
%include <shogun/preprocessor/RandomFourierGaussPreproc.h>

%include <shogun/preprocessor/DimensionReductionPreprocessor.h>
%include <shogun/preprocessor/PCA.h>
%include <shogun/preprocessor/KernelPCA.h>
%include <shogun/preprocessor/MultidimensionalScaling.h>
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/DiffusionMaps.cpp
Expand Up @@ -27,7 +27,7 @@
using namespace shogun;

CDiffusionMaps::CDiffusionMaps() :
CDimensionReductionPreprocessor()
CDimensionReductionPreprocessor<float64_t>()
{
m_t = 10;

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/DiffusionMaps.h
Expand Up @@ -23,7 +23,7 @@ class CFeatures;
class CKernel;

/** @brief */
class CDiffusionMaps: public CDimensionReductionPreprocessor
class CDiffusionMaps: public CDimensionReductionPreprocessor<float64_t>
{
public:

Expand Down
69 changes: 36 additions & 33 deletions src/shogun/preprocessor/DimensionReductionPreprocessor.cpp
Expand Up @@ -4,8 +4,11 @@

using namespace shogun;

CDimensionReductionPreprocessor::CDimensionReductionPreprocessor()
: CSimplePreprocessor<float64_t>()
namespace shogun
{
template<class ST>
CDimensionReductionPreprocessor<ST>::CDimensionReductionPreprocessor()
: CSimplePreprocessor<ST>()
{
m_target_dim = 1;
m_distance = new CEuclidianDistance();
Expand All @@ -14,45 +17,42 @@ CDimensionReductionPreprocessor::CDimensionReductionPreprocessor()
init();
}

CDimensionReductionPreprocessor::~CDimensionReductionPreprocessor()
template<class ST>
CDimensionReductionPreprocessor<ST>::~CDimensionReductionPreprocessor()
{
SG_UNREF(m_distance);
SG_UNREF(m_kernel);
}

bool CDimensionReductionPreprocessor::init(CFeatures* data)
template <class ST>
bool CDimensionReductionPreprocessor<ST>::init(CFeatures* data)
{
return true;
}

void CDimensionReductionPreprocessor::cleanup()
template <class ST>
void CDimensionReductionPreprocessor<ST>::cleanup()
{

}

SGMatrix<float64_t> CDimensionReductionPreprocessor::apply_to_feature_matrix(CFeatures* features)
{
return ((CSimpleFeatures<float64_t>*)features)->get_feature_matrix();
};

SGVector<float64_t> CDimensionReductionPreprocessor::apply_to_feature_vector(SGVector<float64_t> vector)
{
return vector;
};

EPreprocessorType CDimensionReductionPreprocessor::get_type() const { return P_DIMENSIONREDUCTIONPREPROCESSOR; };
template<class ST>
EPreprocessorType CDimensionReductionPreprocessor<ST>::get_type() const { return P_DIMENSIONREDUCTIONPREPROCESSOR; };

void CDimensionReductionPreprocessor::set_target_dim(int32_t dim)
template<class ST>
void CDimensionReductionPreprocessor<ST>::set_target_dim(int32_t dim)
{
m_target_dim = dim;
}

int32_t CDimensionReductionPreprocessor::get_target_dim() const
template<class ST>
int32_t CDimensionReductionPreprocessor<ST>::get_target_dim() const
{
return m_target_dim;
}

int32_t CDimensionReductionPreprocessor::calculate_effective_target_dim(int32_t dim)
template<class ST>
int32_t CDimensionReductionPreprocessor<ST>::calculate_effective_target_dim(int32_t dim)
{
if (m_target_dim<0)
{
Expand All @@ -67,44 +67,47 @@ int32_t CDimensionReductionPreprocessor::calculate_effective_target_dim(int32_t
return m_target_dim;
}

void CDimensionReductionPreprocessor::set_distance(CDistance* distance)
template<class ST>
void CDimensionReductionPreprocessor<ST>::set_distance(CDistance* distance)
{
SG_UNREF(m_distance);
SG_REF(distance);
m_distance = distance;
}

CDistance* CDimensionReductionPreprocessor::get_distance() const
template<class ST>
CDistance* CDimensionReductionPreprocessor<ST>::get_distance() const
{
SG_REF(m_distance);
return m_distance;
}

void CDimensionReductionPreprocessor::set_kernel(CKernel* kernel)
template<class ST>
void CDimensionReductionPreprocessor<ST>::set_kernel(CKernel* kernel)
{
SG_UNREF(m_kernel);
SG_REF(kernel);
m_kernel = kernel;
}

CKernel* CDimensionReductionPreprocessor::get_kernel() const
template<class ST>
CKernel* CDimensionReductionPreprocessor<ST>::get_kernel() const
{
SG_REF(m_kernel);
return m_kernel;
}

int32_t CDimensionReductionPreprocessor::detect_dim(SGMatrix<float64_t> distance_matrix)
template<class ST>
void CDimensionReductionPreprocessor<ST>::init()
{
SG_NOTIMPLEMENTED;
return 0;
}

void CDimensionReductionPreprocessor::init()
{
m_parameters->add(&m_target_dim, "target_dim",
this->m_parameters->add(&m_target_dim, "target_dim",
"target dimensionality of preprocessor");
m_parameters->add((CSGObject**)&m_distance, "distance",
this->m_parameters->add((CSGObject**)&m_distance, "distance",
"distance to be used for embedding");
m_parameters->add((CSGObject**)&m_kernel, "kernel",
this->m_parameters->add((CSGObject**)&m_kernel, "kernel",
"kernel to be used for embedding");
}

template class CDimensionReductionPreprocessor<float32_t>;
template class CDimensionReductionPreprocessor<float64_t>;
}
16 changes: 4 additions & 12 deletions src/shogun/preprocessor/DimensionReductionPreprocessor.h
Expand Up @@ -26,7 +26,8 @@ class CKernel;
* class for preprocessors used to lower the dimensionality of given
* simple features (dense matrices).
*/
class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>
template <class ST>
class CDimensionReductionPreprocessor: public CSimplePreprocessor<ST>
{
public:

Expand All @@ -51,12 +52,12 @@ class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>
/** apply preproc to feature matrix
* by default does nothing, returns given features' matrix
*/
virtual SGMatrix<float64_t> apply_to_feature_matrix(CFeatures* features);
virtual SGMatrix<ST> apply_to_feature_matrix(CFeatures* features) = 0;

/** apply preproc to feature vector
* by default does nothing, returns given feature vector
*/
virtual SGVector<float64_t> apply_to_feature_vector(SGVector<float64_t> vector);
virtual SGVector<ST> apply_to_feature_vector(SGVector<ST> vector) = 0;

/** get name */
virtual const char* get_name() const { return "DimensionReductionPreprocessor"; };
Expand Down Expand Up @@ -94,15 +95,6 @@ class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>
*/
CKernel* get_kernel() const;

protected:

/** detect dimensionality from distance matrix
* NOT YET IMPLEMENTED
* @param distance_matrix distance matrix to be used
* @return detected dimensionality
*/
int32_t detect_dim(SGMatrix<float64_t> distance_matrix);

protected:

/** calculates effective target dimensionality
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/preprocessor/KernelPCA.cpp
Expand Up @@ -26,12 +26,12 @@

using namespace shogun;

CKernelPCA::CKernelPCA() : CDimensionReductionPreprocessor()
CKernelPCA::CKernelPCA() : CDimensionReductionPreprocessor<float64_t>()
{
init();
}

CKernelPCA::CKernelPCA(CKernel* k) : CDimensionReductionPreprocessor()
CKernelPCA::CKernelPCA(CKernel* k) : CDimensionReductionPreprocessor<float64_t>()
{
init();
SG_REF(k);
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/KernelPCA.h
Expand Up @@ -32,7 +32,7 @@ class CKernel;
* Retrieved from http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.32.8744
*
*/
class CKernelPCA: public CDimensionReductionPreprocessor
class CKernelPCA: public CDimensionReductionPreprocessor<float64_t>
{
public:
/** default constructor
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/LaplacianEigenmaps.cpp
Expand Up @@ -22,7 +22,7 @@
using namespace shogun;

CLaplacianEigenmaps::CLaplacianEigenmaps() :
CDimensionReductionPreprocessor()
CDimensionReductionPreprocessor<float64_t>()
{
m_k = 3;
m_tau = 1.0;
Expand Down
3 changes: 1 addition & 2 deletions src/shogun/preprocessor/LaplacianEigenmaps.h
Expand Up @@ -20,7 +20,6 @@ namespace shogun
{

class CFeatures;

class CDistance;

/** @brief the class LaplacianEigenmaps used to preprocess
Expand All @@ -45,7 +44,7 @@ class CDistance;
* memory usage.
*
*/
class CLaplacianEigenmaps: public CDimensionReductionPreprocessor
class CLaplacianEigenmaps: public CDimensionReductionPreprocessor<float64_t>
{
public:

Expand Down
4 changes: 2 additions & 2 deletions src/shogun/preprocessor/LocallyLinearEmbedding.cpp
Expand Up @@ -97,7 +97,7 @@ struct SPARSEDOT_THREAD_PARAM
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

CLocallyLinearEmbedding::CLocallyLinearEmbedding() :
CDimensionReductionPreprocessor()
CDimensionReductionPreprocessor<float64_t>()
{
m_k = 3;

Expand Down Expand Up @@ -366,7 +366,7 @@ SGMatrix<float64_t> CLocallyLinearEmbedding::find_null_space(SGMatrix<float64_t>
// using ARPACK (faster)
eigenvalues_vector = SG_MALLOC(float64_t, dimension+1);
#ifdef HAVE_ARPACK
arpack_xsxupd<float64_t>(matrix.matrix,NULL,N,dimension+1,"LA",3,true,-1e-7,0.0,
arpack_xsxupd<float64_t>(matrix.matrix,NULL,N,dimension+1,"LA",3,true,0.0,0.0,
eigenvalues_vector,matrix.matrix,eigenproblem_status);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/LocallyLinearEmbedding.h
Expand Up @@ -46,7 +46,7 @@ class CDistance;
* false using set_posdef.
*
*/
class CLocallyLinearEmbedding: public CDimensionReductionPreprocessor
class CLocallyLinearEmbedding: public CDimensionReductionPreprocessor<float64_t>
{
public:

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/MultidimensionalScaling.cpp
Expand Up @@ -57,7 +57,7 @@ struct TRIANGULATION_THREAD_PARAM
};
#endif

CMultidimensionalScaling::CMultidimensionalScaling() : CDimensionReductionPreprocessor()
CMultidimensionalScaling::CMultidimensionalScaling() : CDimensionReductionPreprocessor<float64_t>()
{
m_eigenvalues = SGVector<float64_t>(NULL,0,false);
m_landmark_number = 3;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/MultidimensionalScaling.h
Expand Up @@ -57,7 +57,7 @@ class CDistance;
* values (30%-50% of total examples number) is pretty good for the
* most tasks.
*/
class CMultidimensionalScaling: public CDimensionReductionPreprocessor
class CMultidimensionalScaling: public CDimensionReductionPreprocessor<float64_t>
{
public:

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/PCA.cpp
Expand Up @@ -25,7 +25,7 @@
using namespace shogun;

CPCA::CPCA(bool do_whitening_, EPCAMode mode_, float64_t thresh_)
: CDimensionReductionPreprocessor(), num_dim(0), m_initialized(false),
: CDimensionReductionPreprocessor<float64_t>(), num_dim(0), m_initialized(false),
m_whitening(do_whitening_), m_mode(mode_), thresh(thresh_)
{
init();
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/PCA.h
Expand Up @@ -45,7 +45,7 @@ enum EPCAMode
* covariance matrix is of size num_feat*num_feat. Note that vectors don't have
* to have zero mean as it is substracted.
*/
class CPCA: public CDimensionReductionPreprocessor
class CPCA: public CDimensionReductionPreprocessor<float64_t>
{
public:

Expand Down

0 comments on commit 5fab1f0

Please sign in to comment.