Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced NeighborhoodPreservingEmbedding (NPE)
- Loading branch information
Showing
6 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
examples/undocumented/libshogun/converter_neighborhoodpreservingembedding.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |