Skip to content

Commit

Permalink
Merge branch 'master' of github.com:shogun-toolbox/shogun
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Jul 26, 2011
2 parents 8f5a9eb + b61710c commit 9bb26bc
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 12 deletions.
Expand Up @@ -5,7 +5,7 @@

parameter_list = [[data,10],[data,20]]

def preprocessor_locallylinearembedding_modular(data,k):
def preprocessor_hessianlocallylinearembedding_modular(data,k):
from shogun.Features import RealFeatures
from shogun.Preprocessor import HessianLocallyLinearEmbedding

Expand All @@ -21,5 +21,5 @@ def preprocessor_locallylinearembedding_modular(data,k):

if __name__=='__main__':
print 'HessianLocallyLinearEmbedding'
preprocessor_locallylinearembedding_modular(*parameter_list[0])
preprocessor_hessianlocallylinearembedding_modular(*parameter_list[0])

@@ -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])

4 changes: 3 additions & 1 deletion src/interfaces/modular/Preprocessor.i
Expand Up @@ -18,10 +18,11 @@
%rename(PruneVarSubMean) CPruneVarSubMean;
%rename(RandomFourierGaussPreproc) CRandomFourierGaussPreproc;

%rename(BaseDimensionReductionPreprocessor) CDimensionReductionPreprocessor;
%rename(DimensionReductionPreprocessor) CDimensionReductionPreprocessor;
%rename(ClassicMDS) CClassicMDS;
%rename(LocallyLinearEmbedding) CLocallyLinearEmbedding;
%rename(HessianLocallyLinearEmbedding) CHessianLocallyLinearEmbedding;
%rename(LocalTangentSpaceAlignment) CLocalTangentSpaceAlignment;
%rename(BaseIsomap) CIsomap;
%rename(LandmarkIsomap) CLandmarkIsomap;
%rename(ClassicIsomap) CClassicIsomap;
Expand Down Expand Up @@ -77,6 +78,7 @@ namespace shogun
%include <shogun/preprocessor/ClassicMDS.h>
%include <shogun/preprocessor/LocallyLinearEmbedding.h>
%include <shogun/preprocessor/HessianLocallyLinearEmbedding.h>
%include <shogun/preprocessor/LocalTangentSpaceAlignment.h>
%include <shogun/preprocessor/Isomap.h>
%include <shogun/preprocessor/LandmarkIsomap.h>
%include <shogun/preprocessor/ClassicIsomap.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Preprocessor_includes.i
Expand Up @@ -16,6 +16,7 @@
#include <shogun/preprocessor/ClassicMDS.h>
#include <shogun/preprocessor/LocallyLinearEmbedding.h>
#include <shogun/preprocessor/HessianLocallyLinearEmbedding.h>
#include <shogun/preprocessor/LocalTangentSpaceAlignment.h>
#include <shogun/preprocessor/Isomap.h>
#include <shogun/preprocessor/LandmarkIsomap.h>
#include <shogun/preprocessor/ClassicIsomap.h>
Expand Down
5 changes: 1 addition & 4 deletions src/shogun/preprocessor/HessianLocallyLinearEmbedding.cpp
Expand Up @@ -84,10 +84,7 @@ SGMatrix<float64_t> CHessianLocallyLinearEmbedding::apply_to_feature_matrix(CFea
SG_FREE(local_neighbors_idxs);

// init W (weight) matrix
float64_t* W_matrix = SG_MALLOC(float64_t, N*N);
for (i=0; i<N; i++)
for (j=0; j<N; j++)
W_matrix[i*N+j]=0.0;
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);
Expand Down
179 changes: 179 additions & 0 deletions src/shogun/preprocessor/LocalTangentSpaceAlignment.cpp
@@ -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 */
73 changes: 73 additions & 0 deletions src/shogun/preprocessor/LocalTangentSpaceAlignment.h
@@ -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_ */
5 changes: 1 addition & 4 deletions src/shogun/preprocessor/LocallyLinearEmbedding.cpp
Expand Up @@ -82,10 +82,7 @@ SGMatrix<float64_t> CLocallyLinearEmbedding::apply_to_feature_matrix(CFeatures*
SG_FREE(local_neighbors_idxs);

// init W (weight) matrix
float64_t* W_matrix = SG_MALLOC(float64_t, N*N);
for (i=0; i<N; i++)
for (j=0; j<N; j++)
W_matrix[i*N+j]=0.0;
float64_t* W_matrix = SG_CALLOC(float64_t, N*N);

// init matrices and norm factor to be used
float64_t* z_matrix = SG_MALLOC(float64_t, m_k*dim);
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/preprocessor/Preprocessor.h
Expand Up @@ -51,7 +51,8 @@ enum EPreprocessorType
P_LANDMARKISOMAP=200,
P_CLASSICISOMAP=210,
P_LANDMARKMDS=220,
P_HESSIANLOCALLYLINEAREMBEDDING=230
P_HESSIANLOCALLYLINEAREMBEDDING=230,
P_LOCALTANGENTSPACEALIGNMENT=240
};

class CFeatures;
Expand Down

0 comments on commit 9bb26bc

Please sign in to comment.