Skip to content

Commit

Permalink
Merge pull request #1105 from vladislav-horbatiuk/vgorbati-develop
Browse files Browse the repository at this point in the history
Added full implementation of manifold sculpting DR method and a smoke test for it.
  • Loading branch information
lisitsyn committed May 16, 2013
2 parents 34c42ef + f6db86c commit 1cfce0c
Show file tree
Hide file tree
Showing 17 changed files with 731 additions and 54 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/modular/Converter.i
Expand Up @@ -23,6 +23,7 @@
%rename(StochasticProximityEmbedding) CStochasticProximityEmbedding;
%rename(FactorAnalysis) CFactorAnalysis;
%rename (TDistributedStochasticNeighborEmbedding) CTDistributedStochasticNeighborEmbedding;
%rename (ManifoldSculpting) CManifoldSculpting;

%newobject shogun::CEmbeddingConverter::apply;
%newobject shogun::*::embed_kernel;
Expand All @@ -44,3 +45,4 @@
%include <shogun/converter/StochasticProximityEmbedding.h>
%include <shogun/converter/FactorAnalysis.h>
%include <shogun/converter/TDistributedStochasticNeighborEmbedding.h>
%include <shogun/converter/ManifoldSculpting.h>
1 change: 1 addition & 0 deletions src/interfaces/modular/Converter_includes.i
Expand Up @@ -15,4 +15,5 @@
#include <shogun/converter/StochasticProximityEmbedding.h>
#include <shogun/converter/FactorAnalysis.h>
#include <shogun/converter/TDistributedStochasticNeighborEmbedding.h>
#include <shogun/converter/ManifoldSculpting.h>
%}
103 changes: 103 additions & 0 deletions src/shogun/converter/ManifoldSculpting.cpp
@@ -0,0 +1,103 @@
/*
* 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) 2013 Vladyslav S. Gorbatiuk
* Copyright (C) 2011-2013 Vladyslav S. Gorbatiuk
*/

#include <shogun/converter/ManifoldSculpting.h>
#ifdef HAVE_EIGEN3
#include <shogun/lib/tapkee/tapkee_shogun.hpp>
#include <shogun/features/DenseFeatures.h>
#include <shogun/distance/EuclideanDistance.h>

using namespace shogun;

CManifoldSculpting::CManifoldSculpting() :
CEmbeddingConverter()
{
// Default values
m_k = 10;
m_squishing_rate = 0.8;
m_max_iteration = 80;
init();
}

void CManifoldSculpting::init()
{
SG_ADD(&m_k, "k", "number of neighbors", MS_NOT_AVAILABLE);
SG_ADD(&m_squishing_rate, "quishing_rate",
"squishing rate",MS_NOT_AVAILABLE);
SG_ADD(&m_max_iteration, "max_iteration",
"maximum number of algorithm's iterations", MS_NOT_AVAILABLE);
}

CManifoldSculpting::~CManifoldSculpting()
{
}

const char* CManifoldSculpting::get_name() const
{
return "ManifoldSculpting";
}

void CManifoldSculpting::set_k(const int32_t k)
{
ASSERT(k>0)
m_k = k;
}

int32_t CManifoldSculpting::get_k() const
{
return m_k;
}

void CManifoldSculpting::set_squishing_rate(const float64_t squishing_rate)
{
ASSERT(squishing_rate >= 0 && squishing_rate < 1)
m_squishing_rate = squishing_rate;
}

float64_t CManifoldSculpting::get_squishing_rate() const
{
return m_squishing_rate;
}

void CManifoldSculpting::set_max_iteration(const int32_t max_iteration)
{
ASSERT(max_iteration > 0)
m_max_iteration = max_iteration;
}

int32_t CManifoldSculpting::get_max_iteration() const
{
return m_max_iteration;
}

CFeatures* CManifoldSculpting::apply(CFeatures* features)
{
CDenseFeatures<float64_t>* feats = (CDenseFeatures<float64_t>*)features;
SG_REF(feats);
CDistance* euclidean_distance =
new CEuclideanDistance(feats, feats);

TAPKEE_PARAMETERS_FOR_SHOGUN parameters;
parameters.n_neighbors = m_k;
parameters.squishing_rate = m_squishing_rate;
parameters.max_iteration = m_max_iteration;
parameters.features = feats;
parameters.distance = euclidean_distance;

parameters.method = SHOGUN_MANIFOLD_SCULPTING;
parameters.target_dimension = m_target_dim;
CDenseFeatures<float64_t>* embedding = tapkee_embed(parameters);

SG_UNREF(euclidean_distance);

return embedding;
}

#endif /* HAVE_EIGEN */
105 changes: 105 additions & 0 deletions src/shogun/converter/ManifoldSculpting.h
@@ -0,0 +1,105 @@
/*
* 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) 2013 Vladyslav S. Gorbatiuk
* Copyright (C) 2011-2013 Vladyslav S. Gorbatiuk
*/

#ifndef MANIFOLDSCULPTING_H_
#define MANIFOLDSCULPTING_H_
#include <shogun/lib/config.h>
#ifdef HAVE_EIGEN3
#include <shogun/converter/EmbeddingConverter.h>
#include <shogun/features/Features.h>

namespace shogun
{

/** @class class CManifoldSculpting used to embed
* data using manifold sculpting embedding algorithm.
*
* Uses implementation from the Tapkee library.
*
*/
class CManifoldSculpting : public CEmbeddingConverter
{
public:

/** constructor */
CManifoldSculpting();

/** destructor */
virtual ~CManifoldSculpting();

/** get name */
virtual const char* get_name() const;

/** apply preprocessor to features
*
* @param features features to embed
*/
virtual CFeatures* apply(CFeatures* features);

/** setter for the k
*
* @param k the number of neighbors
*/
void set_k(const int32_t k);

/** getter for the number of neighbors
*
* @return the number of neighbors k
*/
int32_t get_k() const;

/** setter for squishing_rate
*
* @param squishing_rate the squishing rate
*/
void set_squishing_rate(const float64_t squishing_rate);

/** getter for squishing_rate
*
* @return squishing_rate
*/
float64_t get_squishing_rate() const;

/** setter for the maximum number of iterations
*
* @param max_iteration the maximum number of iterations
*/
void set_max_iteration(const int32_t max_iteration);

/** getter for the maximum number of iterations
*
* @return the maximum number of iterations
*/
int32_t get_max_iteration() const;

private:

/** default init */
void init();

private:

/** k - number of neighbors */
float64_t m_k;

/** squishing_rate */
float64_t m_squishing_rate;

/** max_iteration - the maximum number of algorithm's
* iterations
*/
float64_t m_max_iteration;

}; /* class CManifoldSculpting */

} /* namespace shogun */

#endif /* HAVE_EIGEN3 */
#endif /* MANIFOLDSCULPTING_H_ */
15 changes: 15 additions & 0 deletions src/shogun/lib/tapkee/defines/keywords.hpp
Expand Up @@ -126,6 +126,7 @@ namespace tapkee
* - @ref tapkee::LandmarkIsomap
* - @ref tapkee::StochasticProximityEmbedding (with local strategy, i.e.
* when @ref tapkee::keywords::spe_global_strategy is set to false)
* - @ref tapkee::ManifoldSculpting
*
* Default value is @ref tapkee::CoverTree if available, @ref tapkee::Brute otherwise.
*
Expand All @@ -150,6 +151,7 @@ namespace tapkee
* - @ref tapkee::LandmarkIsomap
* - @ref tapkee::StochasticProximityEmbedding (with local strategy, i.e.
* when @ref tapkee::keywords::spe_global_strategy is set to false)
* - @ref tapkee::ManifoldSculpting
*
* Default value is 5.
*
Expand Down Expand Up @@ -207,6 +209,7 @@ namespace tapkee
* Used by the following iterative methods:
* - @ref tapkee::StochasticProximityEmbedding
* - @ref tapkee::FactorAnalysis
* - @ref tapkee::ManifoldSculpting
*
* Default value is 100.
*
Expand Down Expand Up @@ -320,6 +323,7 @@ namespace tapkee
* - @ref tapkee::LandmarkIsomap
* - @ref tapkee::StochasticProximityEmbedding (with local strategy, i.e.
* when @ref tapkee::keywords::spe_global_strategy is set to false)
* - @ref tapkee::ManifoldSculpting
*
* Default is true.
*
Expand Down Expand Up @@ -396,6 +400,17 @@ namespace tapkee
*/
const ParameterKeyword<ScalarType> sne_theta("SNE theta", 0.5);

/** The keyword for the value that stores the squishingRate
* parameter of the Manifold Sculpting algorithm.
*
* Used by @ref tapkee::ManifoldSculpting.
*
* Default value is 0.99.
*
* The corresponding value should have type @ref tapkee::ScalarType.
*/
const ParameterKeyword<ScalarType> squishing_rate("squishing rate", 0.99);

/** The default value - assigning any keyword to this
* static struct produces a parameter with its default value.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/shogun/lib/tapkee/defines/methods.hpp
Expand Up @@ -63,6 +63,9 @@ namespace tapkee
/** t-SNE and Barnes-Hut-SNE as described in
* @cite vanDerMaaten2008 and @cite vanDerMaaten2013 */
tDistributedStochasticNeighborEmbedding,
/** Manifold Sculpting as described in
* @cite Gashler2007 */
ManifoldSculpting,
/** Passing through (doing nothing just passes the
* data through) */
PassThru
Expand All @@ -89,6 +92,7 @@ namespace tapkee
METHOD_THAT_NEEDS_NOTHING_IS(PassThru);
METHOD_THAT_NEEDS_ONLY_FEATURES_IS(FactorAnalysis);
METHOD_THAT_NEEDS_ONLY_FEATURES_IS(tDistributedStochasticNeighborEmbedding);
METHOD_THAT_NEEDS_DISTANCE_AND_FEATURES_IS(ManifoldSculpting);
#endif // DOXYGEN_SHOULD_SKIP_THS

//! Neighbors computation methods
Expand Down
1 change: 1 addition & 0 deletions src/shogun/lib/tapkee/defines/synonyms.hpp
Expand Up @@ -42,6 +42,7 @@ namespace tapkee_internal
typedef TAPKEE_INTERNAL_VECTOR<tapkee::IndexType> Landmarks;
typedef TAPKEE_INTERNAL_PAIR<tapkee::SparseWeightMatrix,tapkee::DenseDiagonalMatrix> Laplacian;
typedef TAPKEE_INTERNAL_PAIR<tapkee::DenseSymmetricMatrix,tapkee::DenseSymmetricMatrix> DenseSymmetricMatrixPair;
typedef TAPKEE_INTERNAL_PAIR<tapkee::SparseMatrix,tapkee::tapkee_internal::Neighbors> SparseMatrixNeighborsPair;

#if defined(TAPKEE_USE_PRIORITY_QUEUE) && defined(TAPKEE_USE_FIBONACCI_HEAP)
#error "Can't use both priority queue and fibonacci heap at the same time"
Expand Down
2 changes: 2 additions & 0 deletions src/shogun/lib/tapkee/defines/types.hpp
Expand Up @@ -27,6 +27,8 @@ namespace tapkee
typedef Eigen::DiagonalMatrix<tapkee::ScalarType,Eigen::Dynamic> DenseDiagonalMatrix;
//! sparse weight matrix type (non-overridable)
typedef Eigen::SparseMatrix<tapkee::ScalarType> SparseWeightMatrix;
//! sparse matrix type (non-overridable)
typedef Eigen::SparseMatrix<tapkee::ScalarType> SparseMatrix;
//! selfadjoint solver (non-overridable)
typedef Eigen::SelfAdjointEigenSolver<tapkee::DenseMatrix> DenseSelfAdjointEigenSolver;
//! dense solver (non-overridable)
Expand Down

0 comments on commit 1cfce0c

Please sign in to comment.