Skip to content

Commit

Permalink
Various structure improvements for DR preprocessors
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Sep 18, 2011
1 parent 4e58cf6 commit 828a0f4
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 131 deletions.
65 changes: 62 additions & 3 deletions src/shogun/preprocessor/DimensionReductionPreprocessor.h
Expand Up @@ -14,11 +14,15 @@
#include <shogun/preprocessor/SimplePreprocessor.h>
#include <shogun/features/Features.h>
#include <shogun/distance/Distance.h>
#include <shogun/distance/EuclidianDistance.h>
#include <shogun/kernel/GaussianKernel.h>

namespace shogun
{

class CFeatures;
class CDistance;
class CKernel;

/** @brief the class DimensionReductionPreprocessor, a base
* class for preprocessors used to lower the dimensionality of given
Expand All @@ -32,12 +36,24 @@ class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>
CDimensionReductionPreprocessor() : CSimplePreprocessor<float64_t>()
{
m_target_dim = 1;
m_distance = new CEuclidianDistance();
m_distance->parallel = this->parallel;
SG_REF(this->parallel);
m_kernel = new CGaussianKernel();
m_kernel->parallel = this->parallel;
SG_REF(this->parallel);

init();
};

/* destructor */
virtual ~CDimensionReductionPreprocessor() {};
virtual ~CDimensionReductionPreprocessor()
{
delete m_distance;
SG_UNREF(this->parallel);
delete m_kernel;
SG_UNREF(this->parallel);
};

/** init
* set true by default, should be defined if dimension reduction
Expand Down Expand Up @@ -84,37 +100,80 @@ class CDimensionReductionPreprocessor: public CSimplePreprocessor<float64_t>
*/
void inline set_target_dim(int32_t dim)
{
ASSERT(dim>0);
ASSERT(dim>0 || dim==AUTO_TARGET_DIM);
m_target_dim = dim;
}

/** getter for target dimension
* @return target dimension
*/
int32_t inline get_target_dim()
int32_t inline get_target_dim() const
{
return m_target_dim;
}

/** setter for distance
* @param distance distance to set
*/
void inline set_distance(CDistance* distance)
{
SG_UNREF(m_distance->parallel);
SG_UNREF(m_distance);
SG_REF(distance);
m_distance = distance;
m_distance->parallel = this->parallel;
SG_REF(this->parallel);
}

/** setter for kernel
* @param kernel kernel to set
*/
void inline set_kernel(CKernel* kernel)
{
SG_UNREF(m_kernel->parallel);
SG_UNREF(m_kernel);
SG_REF(kernel);
m_kernel = kernel;
m_kernel->parallel = this->parallel;
SG_REF(this->parallel);
}

public:

/** const indicating target dimensionality should be determined automagically */
static const int32_t AUTO_TARGET_DIM = -1;

protected:

virtual int32_t detect_dim(SGMatrix<float64_t> distance_matrix)
{
SG_NOTIMPLEMENTED;
return 0;
}


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

protected:

/** target dim of dimensionality reduction preprocessor */
int32_t m_target_dim;

/** distance to be used */
CDistance* m_distance;

/** kernel to be used */
CKernel* m_kernel;

};
}

Expand Down
11 changes: 7 additions & 4 deletions src/shogun/preprocessor/HessianLocallyLinearEmbedding.cpp
Expand Up @@ -15,7 +15,7 @@
#include <shogun/lib/common.h>
#include <shogun/mathematics/Math.h>
#include <shogun/io/SGIO.h>
#include <shogun/distance/EuclidianDistance.h>
#include <shogun/distance/Distance.h>
#include <shogun/lib/Signal.h>

#ifdef HAVE_PTHREAD
Expand Down Expand Up @@ -116,8 +116,11 @@ SGMatrix<float64_t> CHessianLocallyLinearEmbedding::apply_to_feature_matrix(CFea
ASSERT(m_k>=(1+m_target_dim+dp));

// compute distance matrix
CDistance* distance = new CEuclidianDistance(simple_features,simple_features);
SGMatrix<int32_t> neighborhood_matrix = get_neighborhood_matrix(distance);
ASSERT(m_distance);
m_distance->init(simple_features,simple_features);
SGMatrix<float64_t> distance_matrix = m_distance->get_distance_matrix();
SGMatrix<int32_t> neighborhood_matrix = get_neighborhood_matrix(distance_matrix);
distance_matrix.destroy_matrix();

// init W (weight) matrix
float64_t* W_matrix = SG_CALLOC(float64_t, N*N);
Expand Down Expand Up @@ -217,7 +220,7 @@ SGMatrix<float64_t> CHessianLocallyLinearEmbedding::apply_to_feature_matrix(CFea

// finally construct embedding
SGMatrix<float64_t> W_sgmatrix = SGMatrix<float64_t>(W_matrix,N,N);
simple_features->set_feature_matrix(find_null_space(W_sgmatrix,m_target_dim,false));
simple_features->set_feature_matrix(find_null_space(W_sgmatrix,m_target_dim));
W_sgmatrix.destroy_matrix();

SG_UNREF(features);
Expand Down
21 changes: 5 additions & 16 deletions src/shogun/preprocessor/Isomap.cpp
Expand Up @@ -56,13 +56,13 @@ struct DIJKSTRA_THREAD_PARAM

CIsomap::CIsomap() : CMultidimensionalScaling()
{
m_k = 3;

init();
}

void CIsomap::init()
{
m_k = 3;

m_parameters->add(&m_k, "k", "number of neighbors");
}

Expand Down Expand Up @@ -102,27 +102,16 @@ SGMatrix<float64_t> CIsomap::apply_to_feature_matrix(CFeatures* features)
CSimpleFeatures<float64_t>* simple_features =
(CSimpleFeatures<float64_t>*) features;
SG_REF(features);
CDistance* euclidian_distance = new CEuclidianDistance();
euclidian_distance->init(simple_features,simple_features);

Parallel* distance_parallel = euclidian_distance->parallel;
euclidian_distance->parallel = this->parallel;

SGMatrix<float64_t> geodesic_distance_matrix = isomap_distance(euclidian_distance->get_distance_matrix());

ASSERT(m_distance);
m_distance->init(simple_features, simple_features);
SGMatrix<float64_t> geodesic_distance_matrix = isomap_distance(m_distance->get_distance_matrix());
SGMatrix<float64_t> new_features;

if (m_landmark)
new_features = CMultidimensionalScaling::landmark_embedding(geodesic_distance_matrix);
else
new_features = CMultidimensionalScaling::classic_embedding(geodesic_distance_matrix);

geodesic_distance_matrix.destroy_matrix();
euclidian_distance->parallel = distance_parallel;
delete euclidian_distance;

simple_features->set_feature_matrix(new_features);

SG_UNREF(features);
return simple_features->get_feature_matrix();
}
Expand Down
13 changes: 4 additions & 9 deletions src/shogun/preprocessor/KernelLocallyLinearEmbedding.cpp
Expand Up @@ -98,18 +98,16 @@ CKernelLocallyLinearEmbedding::CKernelLocallyLinearEmbedding() :
CKernelLocallyLinearEmbedding::CKernelLocallyLinearEmbedding(CKernel* kernel)
{
init();
SG_REF(kernel);
m_kernel = kernel;

set_kernel(kernel);
}

void CKernelLocallyLinearEmbedding::init()
{
m_kernel = NULL;
}

CKernelLocallyLinearEmbedding::~CKernelLocallyLinearEmbedding()
{
SG_UNREF(m_kernel);
}

bool CKernelLocallyLinearEmbedding::init(CFeatures* features)
Expand All @@ -124,7 +122,6 @@ void CKernelLocallyLinearEmbedding::cleanup()
SGMatrix<float64_t> CKernelLocallyLinearEmbedding::apply_to_feature_matrix(CFeatures* features)
{
ASSERT(features);
ASSERT(m_kernel);
SG_REF(features);

// get dimensionality and number of vectors of data
Expand All @@ -137,12 +134,10 @@ SGMatrix<float64_t> CKernelLocallyLinearEmbedding::apply_to_feature_matrix(CFeat
int32_t i,j,t;

// compute kernel matrix
ASSERT(m_kernel);
m_kernel->init(features,features);
Parallel* kernel_parallel = m_kernel->parallel;
m_kernel->parallel = this->parallel;
SGMatrix<float64_t> kernel_matrix = m_kernel->get_kernel_matrix();
SGMatrix<int32_t> neighborhood_matrix = get_neighborhood_matrix(kernel_matrix);
m_kernel->parallel = kernel_parallel;
m_kernel->cleanup();

// init W (weight) matrix
Expand Down Expand Up @@ -274,7 +269,7 @@ SGMatrix<float64_t> CKernelLocallyLinearEmbedding::apply_to_feature_matrix(CFeat
SG_FREE(nz_idxs);
SG_FREE(W_matrix);

SGMatrix<float64_t> nullspace = find_null_space(M_matrix,m_target_dim,false);
SGMatrix<float64_t> nullspace = find_null_space(M_matrix,m_target_dim);

if ((features->get_feature_class()==C_SIMPLE) &&
(features->get_feature_type()==F_DREAL))
Expand Down
24 changes: 0 additions & 24 deletions src/shogun/preprocessor/KernelLocallyLinearEmbedding.h
Expand Up @@ -66,25 +66,6 @@ class CKernelLocallyLinearEmbedding: public CLocallyLinearEmbedding
*/
virtual SGVector<float64_t> apply_to_feature_vector(SGVector<float64_t> vector);

/** setter for kernel
* @param kernel
*/
void inline set_kernel(CKernel* kernel)
{
SG_UNREF(m_kernel);
SG_REF(kernel);
m_kernel = kernel;
};

/** getter for kernel
* @return kernel
*/
CKernel* get_kernel() const
{
SG_REF(m_kernel);
return m_kernel;
};

/** get name */
virtual inline const char* get_name() const { return "KernelLocallyLinearEmbedding"; };

Expand Down Expand Up @@ -113,11 +94,6 @@ class CKernelLocallyLinearEmbedding: public CLocallyLinearEmbedding
*/
SGMatrix<int32_t> get_neighborhood_matrix(SGMatrix<float64_t> kernel_matrix);

protected:

/** number of neighbors */
CKernel* m_kernel;

};
}

Expand Down
14 changes: 7 additions & 7 deletions src/shogun/preprocessor/LaplacianEigenmaps.cpp
Expand Up @@ -17,22 +17,22 @@
#include <shogun/lib/FibonacciHeap.h>
#include <shogun/mathematics/Math.h>
#include <shogun/io/SGIO.h>
#include <shogun/distance/EuclidianDistance.h>
#include <shogun/distance/Distance.h>
#include <shogun/lib/Signal.h>

using namespace shogun;

CLaplacianEigenmaps::CLaplacianEigenmaps() :
CDimensionReductionPreprocessor()
{
m_k = 3;
m_tau = 1.0;

init();
}

void CLaplacianEigenmaps::init()
{
m_k = 3;
m_tau = 1.0;

m_parameters->add(&m_k, "k", "number of neighbors");
m_parameters->add(&m_tau, "tau", "heat distribution coefficient");
}
Expand Down Expand Up @@ -67,11 +67,11 @@ SGMatrix<float64_t> CLaplacianEigenmaps::apply_to_feature_matrix(CFeatures* feat
int32_t i,j;

// compute distance matrix
CDistance* distance = new CEuclidianDistance(simple_features,simple_features);
SGMatrix<float64_t> W_sgmatrix = distance->get_distance_matrix();
ASSERT(m_distance);
m_distance->init(simple_features,simple_features);
SGMatrix<float64_t> W_sgmatrix = m_distance->get_distance_matrix();
// shorthand
float64_t* W_matrix = W_sgmatrix.matrix;
delete distance;

// init heap to use
CFibonacciHeap* heap = new CFibonacciHeap(N);
Expand Down
13 changes: 7 additions & 6 deletions src/shogun/preprocessor/LocalTangentSpaceAlignment.cpp
Expand Up @@ -16,7 +16,7 @@
#include <shogun/lib/common.h>
#include <shogun/mathematics/Math.h>
#include <shogun/io/SGIO.h>
#include <shogun/distance/EuclidianDistance.h>
#include <shogun/distance/Distance.h>
#include <shogun/lib/Signal.h>

#ifdef HAVE_PTHREAD
Expand Down Expand Up @@ -111,10 +111,11 @@ SGMatrix<float64_t> CLocalTangentSpaceAlignment::apply_to_feature_matrix(CFeatur
int32_t t;

// compute distance matrix
CDistance* distance = new CEuclidianDistance(simple_features,simple_features);
SG_UNREF(distance->parallel);
distance->parallel = this->parallel;
SGMatrix<int32_t> neighborhood_matrix = get_neighborhood_matrix(distance);
ASSERT(m_distance);
m_distance->init(simple_features,simple_features);
SGMatrix<float64_t> distance_matrix = m_distance->get_distance_matrix();
SGMatrix<int32_t> neighborhood_matrix = get_neighborhood_matrix(distance_matrix);
distance_matrix.destroy_matrix();

// init W (weight) matrix
float64_t* W_matrix = SG_CALLOC(float64_t, N*N);
Expand Down Expand Up @@ -201,7 +202,7 @@ SGMatrix<float64_t> CLocalTangentSpaceAlignment::apply_to_feature_matrix(CFeatur

// finally construct embedding
SGMatrix<float64_t> W_sgmatrix(W_matrix,N,N);
simple_features->set_feature_matrix(find_null_space(W_sgmatrix,m_target_dim,false));
simple_features->set_feature_matrix(find_null_space(W_sgmatrix,m_target_dim));
W_sgmatrix.destroy_matrix();

SG_UNREF(features);
Expand Down

0 comments on commit 828a0f4

Please sign in to comment.