Skip to content

Commit

Permalink
Introduced NeighborhoodPreservingEmbedding (NPE)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Oct 18, 2011
1 parent ea9ad9a commit 99027e8
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -47,6 +47,7 @@ TARGETS = basic_minimal classifier_libsvm classifier_minimal_svm \
converter_isomap \
converter_diffusionmaps \
converter_laplacianeigenmaps \
converter_neighborhoodpreservingembedding \

all: $(TARGETS)

Expand Down
@@ -0,0 +1,40 @@
/*
* 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) 2011 Sergey Lisitsyn
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

#include <shogun/base/init.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/converter/NeighborhoodPreservingEmbedding.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

int main(int argc, char** argv)
{
init_shogun();

int N = 100;
int dim = 3;
float64_t* matrix = new double[N*dim];
for (int i=0; i<N*dim; i++)
matrix[i] = CMath::random(1,100);

CSimpleFeatures<double>* features = new CSimpleFeatures<double>(SGMatrix<double>(matrix,dim,N));
SG_REF(features);
CNeighborhoodPreservingEmbedding* npe = new CNeighborhoodPreservingEmbedding();
npe->set_target_dim(2);
npe->set_k(15);
npe->parallel->set_num_threads(4);
CSimpleFeatures<double>* embedding = npe->embed(features);
SG_UNREF(embedding);
SG_UNREF(npe);
SG_UNREF(features);
exit_shogun();
return 0;
}
2 changes: 2 additions & 0 deletions src/interfaces/modular/Converter.i
Expand Up @@ -10,6 +10,7 @@

%rename(EmbeddingConverter) CEmbeddingConverter;
%rename(LocallyLinearEmbedding) CLocallyLinearEmbedding;
%rename(NeighborhoodPreservingEmbedding) CNeighborhoodPreservingEmbedding;
%rename(LocalTangentSpaceAlignment) CLocalTangentSpaceAlignment;
%rename(HessianLocallyLinearEmbedding) CHessianLocallyLinearEmbedding;
%rename(KernelLocallyLinearEmbedding) CKernelLocallyLinearEmbedding;
Expand All @@ -24,6 +25,7 @@
%include <shogun/converter/Converter.h>
%include <shogun/converter/EmbeddingConverter.h>
%include <shogun/converter/LocallyLinearEmbedding.h>
%include <shogun/converter/NeighborhoodPreservingEmbedding.h>
%include <shogun/converter/LocalTangentSpaceAlignment.h>
%include <shogun/converter/HessianLocallyLinearEmbedding.h>
%include <shogun/converter/KernelLocallyLinearEmbedding.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Converter_includes.i
Expand Up @@ -2,6 +2,7 @@
#include <shogun/converter/Converter.h>
#include <shogun/converter/EmbeddingConverter.h>
#include <shogun/converter/LocallyLinearEmbedding.h>
#include <shogun/converter/NeighborhoodPreservingEmbedding.h>
#include <shogun/converter/LocalTangentSpaceAlignment.h>
#include <shogun/converter/HessianLocallyLinearEmbedding.h>
#include <shogun/converter/KernelLocallyLinearEmbedding.h>
Expand Down
86 changes: 86 additions & 0 deletions src/shogun/converter/NeighborhoodPreservingEmbedding.cpp
@@ -0,0 +1,86 @@
/*
* 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) 2011 Sergey Lisitsyn
* Copyright (C) 2011 Sergey Lisitsyn
*/

#include <shogun/converter/NeighborhoodPreservingEmbedding.h>
#include <shogun/lib/config.h>
#ifdef HAVE_LAPACK
#include <shogun/converter/EmbeddingConverter.h>
#include <shogun/mathematics/arpack.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/lib/FibonacciHeap.h>
#include <shogun/base/DynArray.h>
#include <shogun/mathematics/Math.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Time.h>
#include <shogun/distance/Distance.h>
#include <shogun/lib/Signal.h>

using namespace shogun;

CNeighborhoodPreservingEmbedding::CNeighborhoodPreservingEmbedding() :
CLocallyLinearEmbedding()
{
}

CNeighborhoodPreservingEmbedding::~CNeighborhoodPreservingEmbedding()
{
}

const char* CNeighborhoodPreservingEmbedding::get_name() const
{
return "NeighborhoodPreservingEmbedding";
}

SGMatrix<float64_t> CNeighborhoodPreservingEmbedding::construct_embedding(CFeatures* features, SGMatrix<float64_t> matrix, int dimension)
{
CSimpleFeatures<float64_t>* simple_features = (CSimpleFeatures<float64_t>*)features;
ASSERT(simple_features);
int i,j;
int N = simple_features->get_num_vectors();
int dim = simple_features->get_num_features();
ASSERT(dimension<=dim);

SGMatrix<float64_t> feature_matrix = simple_features->get_feature_matrix();
float64_t* XTM = SG_MALLOC(float64_t, dim*N);
float64_t* lhs_M = SG_MALLOC(float64_t, dim*dim);
float64_t* rhs_M = SG_MALLOC(float64_t, dim*dim);

cblas_dgemm(CblasColMajor,CblasNoTrans,CblasNoTrans,dim,N,N,1.0,feature_matrix.matrix,dim,matrix.matrix,N,0.0,XTM,dim);
cblas_dgemm(CblasColMajor,CblasNoTrans,CblasTrans,dim,dim,N,1.0,XTM,dim,feature_matrix.matrix,dim,0.0,lhs_M,dim);
cblas_dgemm(CblasColMajor,CblasNoTrans,CblasTrans,dim,dim,N,1.0,feature_matrix.matrix,dim,feature_matrix.matrix,dim,0.0,rhs_M,dim);

float64_t* evals = SG_MALLOC(float64_t, dim);
float64_t* evectors = SG_MALLOC(float64_t, dimension*dim);
int32_t info = 0;
wrap_dsygvx(1,'V','U',dim,lhs_M,dim,rhs_M,dim,dim-dimension+1,dim,evals,evectors,&info);
ASSERT(info==0);
SG_FREE(lhs_M);
SG_FREE(rhs_M);
SG_FREE(evals);

for (i=0; i<dimension/2; i++)
{
cblas_dswap(dim,evectors+i*dim,1,evectors+(dimension-i-1)*dim,1);
}

cblas_dgemm(CblasColMajor,CblasTrans,CblasNoTrans,N,dimension,dim,1.0,feature_matrix.matrix,dim,evectors,dim,0.0,XTM,N);
SG_FREE(evectors);

float64_t* new_features = SG_MALLOC(float64_t, dimension*N);
for (i=0; i<dimension; i++)
{
for (j=0; j<N; j++)
new_features[j*dimension+i] = XTM[i*N+j];
}
SG_FREE(XTM);
return SGMatrix<float64_t>(new_features,dimension,N);
}

#endif /* HAVE_LAPACK */
54 changes: 54 additions & 0 deletions src/shogun/converter/NeighborhoodPreservingEmbedding.h
@@ -0,0 +1,54 @@
/*
* 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) 2011 Sergey Lisitsyn
* Copyright (C) 2011 Sergey Lisitsyn
*/

#ifndef NEIGHBORHOODPRESERVINGEMBEDDING_H_
#define NEIGHBORHOODPRESERVINGEMBEDDING_H_
#include <shogun/lib/config.h>
#ifdef HAVE_LAPACK
#include <shogun/converter/LocallyLinearEmbedding.h>
#include <shogun/features/Features.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/distance/Distance.h>

namespace shogun
{

class CFeatures;
class CDistance;

/** @brief NeighborhoodPreservingEmbedding
*/
class CNeighborhoodPreservingEmbedding: public CLocallyLinearEmbedding
{
public:

/** constructor */
CNeighborhoodPreservingEmbedding();

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

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

protected:

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

#endif /* HAVE_LAPACK */
#endif /* NEIGHBORHOODPRESERVINGEMBEDDING_H_ */

0 comments on commit 99027e8

Please sign in to comment.