Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:shogun-toolbox/shogun
- Loading branch information
Showing
9 changed files
with
287 additions
and
12 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
25 changes: 25 additions & 0 deletions
25
examples/undocumented/python_modular/preprocessor_localtangentspacealignment_modular.py
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,25 @@ | ||
from tools.load import LoadMatrix | ||
|
||
lm=LoadMatrix() | ||
data = lm.load_numbers('../data/fm_train_real.dat') | ||
|
||
parameter_list = [[data,10],[data,20]] | ||
|
||
def preprocessor_localtangentspacealignment_modular(data,k): | ||
from shogun.Features import RealFeatures | ||
from shogun.Preprocessor import LocalTangentSpaceAlignment | ||
|
||
features = RealFeatures(data) | ||
|
||
preprocessor = LocalTangentSpaceAlignment() | ||
preprocessor.set_target_dim(1) | ||
preprocessor.set_k(k) | ||
preprocessor.apply_to_feature_matrix(features) | ||
|
||
return features | ||
|
||
|
||
if __name__=='__main__': | ||
print 'LocalTangentSpaceAlignment' | ||
preprocessor_localtangentspacealignment_modular(*parameter_list[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
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,179 @@ | ||
/* | ||
* 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/preprocessor/LocalTangentSpaceAlignment.h> | ||
#ifdef HAVE_LAPACK | ||
#include <shogun/mathematics/arpack.h> | ||
#include <shogun/mathematics/lapack.h> | ||
#include <shogun/lib/common.h> | ||
#include <shogun/mathematics/Math.h> | ||
#include <shogun/io/SGIO.h> | ||
#include <shogun/distance/EuclidianDistance.h> | ||
#include <shogun/lib/Signal.h> | ||
|
||
using namespace shogun; | ||
|
||
CLocalTangentSpaceAlignment::CLocalTangentSpaceAlignment() : | ||
CLocallyLinearEmbedding() | ||
{ | ||
} | ||
|
||
CLocalTangentSpaceAlignment::~CLocalTangentSpaceAlignment() | ||
{ | ||
} | ||
|
||
bool CLocalTangentSpaceAlignment::init(CFeatures* features) | ||
{ | ||
return true; | ||
} | ||
|
||
void CLocalTangentSpaceAlignment::cleanup() | ||
{ | ||
} | ||
|
||
SGMatrix<float64_t> CLocalTangentSpaceAlignment::apply_to_feature_matrix(CFeatures* features) | ||
{ | ||
// shorthand for simplefeatures | ||
CSimpleFeatures<float64_t>* simple_features = (CSimpleFeatures<float64_t>*) features; | ||
ASSERT(simple_features); | ||
|
||
// get dimensionality and number of vectors of data | ||
int32_t dim = simple_features->get_num_features(); | ||
ASSERT(m_target_dim<=dim); | ||
int32_t N = simple_features->get_num_vectors(); | ||
ASSERT(m_k<N); | ||
|
||
// loop variables | ||
int32_t i,j,k; | ||
|
||
// compute distance matrix | ||
CDistance* distance = new CEuclidianDistance(simple_features,simple_features); | ||
SGMatrix<float64_t> distance_matrix = distance->get_distance_matrix(); | ||
delete distance; | ||
|
||
// init matrices to be used | ||
int32_t* neighborhood_matrix = SG_MALLOC(int32_t, N*m_k); | ||
int32_t* local_neighbors_idxs = SG_MALLOC(int32_t, N); | ||
|
||
// construct neighborhood matrix (contains idxs of neighbors for | ||
// i-th object in i-th column) | ||
for (i=0; i<N; i++) | ||
{ | ||
for (j=0; j<N; j++) | ||
{ | ||
local_neighbors_idxs[j] = j; | ||
} | ||
|
||
CMath::qsort_index(distance_matrix.matrix+(i*N),local_neighbors_idxs,N); | ||
|
||
for (j=0; j<m_k; j++) | ||
neighborhood_matrix[j*N+i] = local_neighbors_idxs[j+1]; | ||
} | ||
|
||
delete[] distance_matrix.matrix; | ||
delete[] local_neighbors_idxs; | ||
|
||
// init W (weight) matrix | ||
float64_t* W_matrix = SG_CALLOC(float64_t, N*N); | ||
|
||
// init matrices and norm factor to be used | ||
float64_t* local_feature_matrix = SG_MALLOC(float64_t, m_k*dim); | ||
float64_t* mean_vector = SG_MALLOC(float64_t, dim); | ||
float64_t* q_matrix = SG_MALLOC(float64_t, m_k*m_k); | ||
float64_t* s_values_vector = SG_MALLOC(float64_t, dim); | ||
|
||
// G | ||
float64_t* G_matrix = SG_MALLOC(float64_t, m_k*(1+m_target_dim)); | ||
// get feature matrix | ||
SGMatrix<float64_t> feature_matrix = simple_features->get_feature_matrix(); | ||
|
||
for (i=0; i<N && !(CSignal::cancel_computations()); i++) | ||
{ | ||
// Yi(:,0) = 1 | ||
for (j=0; j<m_k; j++) | ||
G_matrix[j] = 1.0/CMath::sqrt((float64_t)m_k); | ||
|
||
// fill mean vector with zeros | ||
for (j=0; j<dim; j++) | ||
mean_vector[j] = 0.0; | ||
|
||
// compute local feature matrix containing neighbors of i-th vector | ||
for (j=0; j<m_k; j++) | ||
{ | ||
for (k=0; k<dim; k++) | ||
{ | ||
local_feature_matrix[j*dim+k] = feature_matrix.matrix[neighborhood_matrix[j*N+i]*dim+k]; | ||
mean_vector[k] += local_feature_matrix[j*dim+k]; | ||
} | ||
} | ||
|
||
// compute mean | ||
for (j=0; j<dim; j++) | ||
mean_vector[j] /= m_k; | ||
|
||
// center feature vectors by mean | ||
for (j=0; j<m_k; j++) | ||
{ | ||
for (k=0; k<dim; k++) | ||
local_feature_matrix[j*dim+k] -= mean_vector[k]; | ||
} | ||
|
||
int32_t info = 0; | ||
// find right eigenvectors of local_feature_matrix | ||
wrap_dgesvd('N','O', dim,m_k,local_feature_matrix,dim, | ||
s_values_vector, | ||
NULL,1, NULL,1, &info); | ||
ASSERT(info==0); | ||
|
||
for (j=0; j<m_target_dim; j++) | ||
{ | ||
for (k=0; k<m_k; k++) | ||
G_matrix[(j+1)*m_k+k] = local_feature_matrix[k*dim+j]; | ||
} | ||
|
||
// compute GG' | ||
cblas_dgemm(CblasColMajor,CblasNoTrans,CblasTrans, | ||
m_k,m_k,1+m_target_dim, | ||
1.0,G_matrix,m_k, | ||
G_matrix,m_k, | ||
0.0,q_matrix,m_k); | ||
|
||
// W[neighbors of i, neighbors of i] = I - GG' | ||
for (j=0; j<m_k; j++) | ||
{ | ||
W_matrix[N*neighborhood_matrix[j*N+i]+neighborhood_matrix[j*N+i]] += 1.0; | ||
for (k=0; k<m_k; k++) | ||
W_matrix[N*neighborhood_matrix[k*N+i]+neighborhood_matrix[j*N+i]] -= q_matrix[j*m_k+k]; | ||
} | ||
} | ||
|
||
// clean | ||
SG_FREE(G_matrix); | ||
SG_FREE(s_values_vector); | ||
SG_FREE(mean_vector); | ||
SG_FREE(neighborhood_matrix); | ||
SG_FREE(local_feature_matrix); | ||
SG_FREE(q_matrix); | ||
|
||
// finally construct embedding | ||
SGMatrix<float64_t> W_sgmatrix(W_matrix,N,N,true); | ||
simple_features->set_feature_matrix(find_null_space(W_sgmatrix,m_target_dim,false)); | ||
W_sgmatrix.free_matrix(); | ||
|
||
return simple_features->get_feature_matrix(); | ||
} | ||
|
||
SGVector<float64_t> CLocalTangentSpaceAlignment::apply_to_feature_vector(SGVector<float64_t> vector) | ||
{ | ||
SG_NOTIMPLEMENTED; | ||
return vector; | ||
} | ||
|
||
#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,73 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
#ifndef LOCALTANGENTSPACEALIGNMENT_H_ | ||
#define LOCALTANGENTSPACEALIGNMENT_H_ | ||
#ifdef HAVE_LAPACK | ||
#include <shogun/preprocessor/LocallyLinearEmbedding.h> | ||
#include <shogun/features/Features.h> | ||
#include <shogun/distance/Distance.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
class CFeatures; | ||
|
||
class CDistance; | ||
|
||
/** @brief the class LocalTangentSpaceAlignment used to preprocess | ||
* data using Local Tangent Space Alignment (LTSA) algorithm described in | ||
* | ||
* Z. Zhang, H. Zha, Principal manifolds and nonlinear dimensionality | ||
* reduction via tangent space alignment, SIAM J. Sci. Comput. 26 (1) | ||
* (2004) 313–338. | ||
* | ||
*/ | ||
class CLocalTangentSpaceAlignment: public CLocallyLinearEmbedding | ||
{ | ||
public: | ||
|
||
/** constructor */ | ||
CLocalTangentSpaceAlignment(); | ||
|
||
/** destructor */ | ||
virtual ~CLocalTangentSpaceAlignment(); | ||
|
||
/** init | ||
* @param data feature vectors for preproc | ||
*/ | ||
virtual bool init(CFeatures* features); | ||
|
||
/** cleanup | ||
* | ||
*/ | ||
virtual void cleanup(); | ||
|
||
/** apply preproc to feature matrix | ||
* | ||
*/ | ||
virtual SGMatrix<float64_t> apply_to_feature_matrix(CFeatures* features); | ||
|
||
/** apply preproc to feature vector | ||
* | ||
*/ | ||
virtual SGVector<float64_t> apply_to_feature_vector(SGVector<float64_t> vector); | ||
|
||
/** get name */ | ||
virtual inline const char* get_name() const { return "LocalTangentSpaceAlignment"; }; | ||
|
||
/** get type */ | ||
virtual inline EPreprocessorType get_type() const { return P_LOCALTANGENTSPACEALIGNMENT; }; | ||
|
||
}; | ||
} | ||
|
||
#endif /* HAVE_LAPACK */ | ||
#endif /* LOCALTANGENTSPACEALINGMENT_H_ */ |
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