Skip to content

Commit

Permalink
Added embed method to EmbeddingConverter, adapt structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Oct 18, 2011
1 parent 115a78d commit ea9ad9a
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 88 deletions.
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char** argv)
dmaps->set_target_dim(2);
dmaps->set_t(10);
dmaps->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = dmaps->apply(features);
CSimpleFeatures<double>* embedding = dmaps->embed(features);
SG_UNREF(embedding);
SG_UNREF(dmaps);
SG_UNREF(features);
Expand Down
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char** argv)
hlle->set_target_dim(2);
hlle->set_k(8);
hlle->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = hlle->apply(features);
CSimpleFeatures<double>* embedding = hlle->embed(features);
SG_UNREF(embedding);
SG_UNREF(hlle);
SG_UNREF(features);
Expand Down
2 changes: 1 addition & 1 deletion examples/undocumented/libshogun/converter_isomap.cpp
Expand Up @@ -31,7 +31,7 @@ int main(int argc, char** argv)
isomap->set_landmark(true);
isomap->set_k(4);
isomap->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = isomap->apply(features);
CSimpleFeatures<double>* embedding = isomap->embed(features);
SG_UNREF(embedding);
SG_UNREF(isomap);
SG_UNREF(features);
Expand Down
Expand Up @@ -33,7 +33,7 @@ int main(int argc, char** argv)
klle->set_k(4);
klle->set_kernel(kernel);
klle->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = klle->apply(features);
CSimpleFeatures<double>* embedding = klle->embed(features);
SG_UNREF(embedding);
SG_UNREF(klle);
SG_UNREF(features);
Expand Down
Expand Up @@ -33,7 +33,7 @@ int main(int argc, char** argv)
kltsa->set_k(4);
kltsa->set_kernel(kernel);
kltsa->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = kltsa->apply(features);
CSimpleFeatures<double>* embedding = kltsa->embed(features);
SG_UNREF(embedding);
SG_UNREF(kltsa);
SG_UNREF(features);
Expand Down
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char** argv)
lem->set_target_dim(2);
lem->set_k(10);
lem->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = lem->apply(features);
CSimpleFeatures<double>* embedding = lem->embed(features);
SG_UNREF(embedding);
SG_UNREF(lem);
SG_UNREF(features);
Expand Down
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char** argv)
lle->set_target_dim(2);
lle->set_k(4);
lle->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = lle->apply(features);
CSimpleFeatures<double>* embedding = lle->embed(features);
SG_UNREF(embedding);
SG_UNREF(lle);
SG_UNREF(features);
Expand Down
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char** argv)
ltsa->set_target_dim(2);
ltsa->set_k(4);
ltsa->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = ltsa->apply(features);
CSimpleFeatures<double>* embedding = ltsa->embed(features);
SG_UNREF(embedding);
SG_UNREF(ltsa);
SG_UNREF(features);
Expand Down
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char** argv)
mds->set_target_dim(2);
mds->set_landmark(true);
mds->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = mds->apply(features);
CSimpleFeatures<double>* embedding = mds->embed(features);
SG_UNREF(embedding);
SG_UNREF(mds);
SG_UNREF(features);
Expand Down
Expand Up @@ -41,7 +41,7 @@
features = RealFeatures(X)
preproc.set_target_dim(2)
preproc.io.set_loglevel(MSG_DEBUG)
new_feats = preproc.apply(features).get_feature_matrix()
new_feats = preproc.embed(features).get_feature_matrix()
if not new_mpl:
preproc_subplot = fig.add_subplot(1,3,i+1)
else:
Expand Down
Expand Up @@ -72,7 +72,7 @@
X = numpy.genfromtxt('../../../../data/toy/swissroll.dat',unpack=True).T
features = RealFeatures(X)
converter.set_target_dim(2)
new_feats = converter.apply(features).get_feature_matrix()
new_feats = converter.embed(features).get_feature_matrix()
if not new_mpl:
embedding_subplot = fig.add_subplot(4,2,i+1)
else:
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/converter/DiffusionMaps.cpp
Expand Up @@ -54,7 +54,7 @@ const char* CDiffusionMaps::get_name() const
return "DiffusionMaps";
};

CSimpleFeatures<float64_t>* CDiffusionMaps::apply(CFeatures* features)
CFeatures* CDiffusionMaps::apply(CFeatures* features)
{
ASSERT(features);
if (!(features->get_feature_class()==C_SIMPLE &&
Expand Down Expand Up @@ -162,7 +162,7 @@ CSimpleFeatures<float64_t>* CDiffusionMaps::apply(CFeatures* features)
kernel_matrix.destroy_matrix();

SG_UNREF(features);
return new CSimpleFeatures<float64_t>(new_feature_matrix);
return (CFeatures*)(new CSimpleFeatures<float64_t>(new_feature_matrix));
}

#endif /* HAVE_LAPACK */
2 changes: 1 addition & 1 deletion src/shogun/converter/DiffusionMaps.h
Expand Up @@ -38,7 +38,7 @@ class CDiffusionMaps: public CEmbeddingConverter
/** apply preprocessor to features
* @param features
*/
virtual CSimpleFeatures<float64_t>* apply(CFeatures* features);
virtual CFeatures* apply(CFeatures* features);

/** setter for t parameter
* @param t t value
Expand Down
12 changes: 9 additions & 3 deletions src/shogun/converter/EmbeddingConverter.cpp
Expand Up @@ -9,6 +9,7 @@
*/

#include <shogun/converter/EmbeddingConverter.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/kernel/LinearKernel.h>
#include <shogun/distance/EuclidianDistance.h>

Expand All @@ -32,6 +33,11 @@ CEmbeddingConverter::~CEmbeddingConverter()
SG_UNREF(m_kernel);
}

CSimpleFeatures<float64_t>* CEmbeddingConverter::embed(CFeatures* features)
{
return (CSimpleFeatures<float64_t>*)apply(features);
}

void CEmbeddingConverter::set_target_dim(int32_t dim)
{
ASSERT(dim>0);
Expand Down Expand Up @@ -72,10 +78,10 @@ CKernel* CEmbeddingConverter::get_kernel() const
void CEmbeddingConverter::init()
{
this->m_parameters->add(&m_target_dim, "target_dim",
"target dimensionality of preprocessor");
"target dimensionality of preprocessor");
this->m_parameters->add((CSGObject**)&m_distance, "distance",
"distance to be used for embedding");
"distance to be used for embedding");
this->m_parameters->add((CSGObject**)&m_kernel, "kernel",
"kernel to be used for embedding");
"kernel to be used for embedding");
}
}
9 changes: 8 additions & 1 deletion src/shogun/converter/EmbeddingConverter.h
Expand Up @@ -38,10 +38,17 @@ class CEmbeddingConverter: public CConverter
virtual ~CEmbeddingConverter();

/** applies to the given data, returns embedding
* @param features features to embed
* @return embedding features
*/
virtual CFeatures* apply(CFeatures* features) = 0;

/** embed given features, acts the same as apply, but returns
* SimpleFeatures
* @param features features to embed
* @return embedding simple features
*/
virtual CSimpleFeatures<float64_t>* apply(CFeatures* features) = 0;
CSimpleFeatures<float64_t>* embed(CFeatures* features);

/** setter for target dimension
* @param dim target dimension
Expand Down
27 changes: 2 additions & 25 deletions src/shogun/converter/Isomap.cpp
Expand Up @@ -85,32 +85,9 @@ const char* CIsomap::get_name() const
return "Isomap";
}

CSimpleFeatures<float64_t>* CIsomap::apply(CDistance* distance)
SGMatrix<float64_t> CIsomap::process_distance_matrix(SGMatrix<float64_t> distance_matrix)
{
ASSERT(distance);
SGMatrix<float64_t> geodesic_distance_matrix = isomap_distance(distance->get_distance_matrix());
// compute embedding for geodesic distance
SGMatrix<float64_t> new_feature_matrix;
if (m_landmark)
new_feature_matrix = CMultidimensionalScaling::landmark_embedding(geodesic_distance_matrix);
else
new_feature_matrix = CMultidimensionalScaling::classic_embedding(geodesic_distance_matrix);

geodesic_distance_matrix.destroy_matrix();
return new CSimpleFeatures<float64_t>(new_feature_matrix);
}

CSimpleFeatures<float64_t>* CIsomap::apply(CFeatures* features)
{
SG_REF(features);
ASSERT(m_distance);

m_distance->init(features, features);
CSimpleFeatures<float64_t>* embedding = apply(m_distance);
m_distance->remove_lhs_and_rhs();

SG_UNREF(features);
return embedding;
return isomap_distance(distance_matrix);
}

SGMatrix<float64_t> CIsomap::isomap_distance(SGMatrix<float64_t> D_matrix)
Expand Down
30 changes: 15 additions & 15 deletions src/shogun/converter/Isomap.h
Expand Up @@ -52,18 +52,6 @@ class CIsomap: public CMultidimensionalScaling
/* destructor */
virtual ~CIsomap();

/** apply preprocessor to CDistance
* @param distance distance
* @return embedded features
*/
virtual CSimpleFeatures<float64_t>* apply(CDistance* distance);

/** apply preprocessor to features
* @param features
* @return embedded feature matrix
*/
virtual CSimpleFeatures<float64_t>* apply(CFeatures* features);

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

Expand All @@ -77,16 +65,28 @@ class CIsomap: public CMultidimensionalScaling
*/
int32_t get_k() const;

/// HELPERS
protected:

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

/** process distance matrix (redefined in isomap, for mds does nothing)
* @param distance_matrix distance matrix
* @return processed distance matrix
*/
virtual SGMatrix<float64_t> process_distance_matrix(SGMatrix<float64_t> distance_matrix);


/// FIELDS
protected:

/** k, number of neighbors for K-Isomap */
int32_t m_k;

/// THREADS
protected:

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

/** run dijkstra thread
* @param p thread params
*/
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/converter/KernelLocallyLinearEmbedding.cpp
Expand Up @@ -108,7 +108,7 @@ CKernelLocallyLinearEmbedding::~CKernelLocallyLinearEmbedding()
{
}

CSimpleFeatures<float64_t>* CKernelLocallyLinearEmbedding::apply(CFeatures* features)
CFeatures* CKernelLocallyLinearEmbedding::apply(CFeatures* features)
{
ASSERT(features);
SG_REF(features);
Expand All @@ -130,11 +130,11 @@ CSimpleFeatures<float64_t>* CKernelLocallyLinearEmbedding::apply(CFeatures* feat
SGMatrix<float64_t> M_matrix = construct_weight_matrix(kernel_matrix,neighborhood_matrix);
neighborhood_matrix.destroy_matrix();

SGMatrix<float64_t> nullspace = find_null_space(M_matrix,m_target_dim);
SGMatrix<float64_t> nullspace = construct_embedding(features,M_matrix,m_target_dim);
M_matrix.destroy_matrix();

SG_UNREF(features);
return new CSimpleFeatures<float64_t>(nullspace);
return (CFeatures*)(new CSimpleFeatures<float64_t>(nullspace));
}

SGMatrix<float64_t> CKernelLocallyLinearEmbedding::construct_weight_matrix(SGMatrix<float64_t> kernel_matrix,
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/converter/KernelLocallyLinearEmbedding.h
Expand Up @@ -46,7 +46,7 @@ class CKernelLocallyLinearEmbedding: public CLocallyLinearEmbedding
/** destructor */
virtual ~CKernelLocallyLinearEmbedding();

virtual CSimpleFeatures<float64_t>* apply(CFeatures* features);
virtual CFeatures* apply(CFeatures* features);

/** get name */
virtual const char* get_name() const;
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/converter/LaplacianEigenmaps.cpp
Expand Up @@ -66,7 +66,7 @@ const char* CLaplacianEigenmaps::get_name() const
return "LaplacianEigenmaps";
};

CSimpleFeatures<float64_t>* CLaplacianEigenmaps::apply(CFeatures* features)
CFeatures* CLaplacianEigenmaps::apply(CFeatures* features)
{
// shorthand for simplefeatures
SG_REF(features);
Expand Down Expand Up @@ -200,6 +200,6 @@ CSimpleFeatures<float64_t>* CLaplacianEigenmaps::apply(CFeatures* features)
W_sgmatrix.destroy_matrix();

SG_UNREF(features);
return new CSimpleFeatures<float64_t>(new_features);
return (CFeatures*)(new CSimpleFeatures<float64_t>(new_features));
}
#endif /* HAVE_LAPACK */
2 changes: 1 addition & 1 deletion src/shogun/converter/LaplacianEigenmaps.h
Expand Up @@ -54,7 +54,7 @@ class CLaplacianEigenmaps: public CEmbeddingConverter
/** destructor */
virtual ~CLaplacianEigenmaps();

virtual CSimpleFeatures<float64_t>* apply(CFeatures* features);
virtual CFeatures* apply(CFeatures* features);

/** setter for K parameter
* @param k k value
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/converter/LocallyLinearEmbedding.cpp
Expand Up @@ -201,7 +201,7 @@ const char* CLocallyLinearEmbedding::get_name() const
return "LocallyLinearEmbedding";
}

CSimpleFeatures<float64_t>* CLocallyLinearEmbedding::apply(CFeatures* features)
CFeatures* CLocallyLinearEmbedding::apply(CFeatures* features)
{
ASSERT(features);
// check features
Expand Down Expand Up @@ -249,11 +249,11 @@ CSimpleFeatures<float64_t>* CLocallyLinearEmbedding::apply(CFeatures* features)

// find null space of weight matrix
SG_DEBUG("Finding nullspace\n");
SGMatrix<float64_t> new_feature_matrix = find_null_space(weight_matrix,m_target_dim);
SGMatrix<float64_t> new_feature_matrix = construct_embedding(features,weight_matrix,m_target_dim);
weight_matrix.destroy_matrix();

SG_UNREF(features);
return new CSimpleFeatures<float64_t>(new_feature_matrix);
return (CFeatures*)(new CSimpleFeatures<float64_t>(new_feature_matrix));
}

int32_t CLocallyLinearEmbedding::estimate_k(CSimpleFeatures<float64_t>* simple_features, SGMatrix<int32_t> neighborhood_matrix)
Expand Down Expand Up @@ -475,7 +475,7 @@ SGMatrix<float64_t> CLocallyLinearEmbedding::construct_weight_matrix(CSimpleFeat
return M_matrix;
}

SGMatrix<float64_t> CLocallyLinearEmbedding::find_null_space(SGMatrix<float64_t> matrix, int dimension)
SGMatrix<float64_t> CLocallyLinearEmbedding::construct_embedding(CFeatures* features,SGMatrix<float64_t> matrix,int dimension)
{
int i,j;
ASSERT(matrix.num_cols==matrix.num_rows);
Expand Down
15 changes: 8 additions & 7 deletions src/shogun/converter/LocallyLinearEmbedding.h
Expand Up @@ -64,7 +64,7 @@ class CLocallyLinearEmbedding: public CEmbeddingConverter
/** apply preprocessor to features
* @param features
*/
virtual CSimpleFeatures<float64_t>* apply(CFeatures* features);
virtual CFeatures* apply(CFeatures* features);

/** setter for k parameter
* @param k k value
Expand Down Expand Up @@ -143,19 +143,20 @@ class CLocallyLinearEmbedding: public CEmbeddingConverter
virtual SGMatrix<float64_t> construct_weight_matrix(CSimpleFeatures<float64_t>* simple_features,float64_t* W_matrix,
SGMatrix<int32_t> neighborhood_matrix);

/** finds null space of given matrix
* @param matrix given matrix
* @param dimension dimension of null space to be computed
* @return null-space approximation feature matrix
/** constructs embedding
* @param features features to be used
* @param matrix computed weight matrix
* @param dimension dimension of embedding
* @return embedding features
*/
SGMatrix<float64_t> find_null_space(SGMatrix<float64_t> matrix, int dimension);
virtual SGMatrix<float64_t> construct_embedding(CFeatures* features,SGMatrix<float64_t> matrix,int dimension);

/** constructs neighborhood matrix by distance
* @param distance_matrix distance matrix to be used
* @param k number of neighbors
* @return matrix containing indexes of neighbors of i-th vector in i-th column
*/
SGMatrix<int32_t> get_neighborhood_matrix(SGMatrix<float64_t> distance_matrix, int32_t k);
virtual SGMatrix<int32_t> get_neighborhood_matrix(SGMatrix<float64_t> distance_matrix, int32_t k);

/** estimates k using ternary search
* @param simple_features simple features to use
Expand Down

0 comments on commit ea9ad9a

Please sign in to comment.