Skip to content

Commit

Permalink
Added supervised KLTSA
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Mar 10, 2012
1 parent fa1f8cd commit cb7c871
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/interfaces/modular/Converter.i
Expand Up @@ -4,8 +4,8 @@
* 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
* Written (W) 2012 Sergey Lisitsyn
* Copyright (C) 2012 Sergey Lisitsyn
*/

%rename(EmbeddingConverter) CEmbeddingConverter;
Expand All @@ -16,13 +16,16 @@
%rename(HessianLocallyLinearEmbedding) CHessianLocallyLinearEmbedding;
%rename(KernelLocallyLinearEmbedding) CKernelLocallyLinearEmbedding;
%rename(KernelLocalTangentSpaceAlignment) CKernelLocalTangentSpaceAlignment;
%rename(SupervisedKernelLocalTangentSpaceAlignment) CSupervisedKernelLocalTangentSpaceAlignment;
%rename(DiffusionMaps) CDiffusionMaps;
%rename(LaplacianEigenmaps) CLaplacianEigenmaps;
%rename(LocalityPreservingProjections) CLocalityPreservingProjections;
%rename(MultidimensionalScaling) CMultidimensionalScaling;
%rename(Isomap) CIsomap;

%newobject shogun::CEmbeddingConverter::apply;
%newobject shogun::*::embed_kernel;
%newobject shogun::*::embed_distance;

%include <shogun/converter/Converter.h>
%include <shogun/converter/EmbeddingConverter.h>
Expand All @@ -33,6 +36,7 @@
%include <shogun/converter/HessianLocallyLinearEmbedding.h>
%include <shogun/converter/KernelLocallyLinearEmbedding.h>
%include <shogun/converter/KernelLocalTangentSpaceAlignment.h>
%include <shogun/converter/SupervisedKernelLocalTangentSpaceAlignment.h>
%include <shogun/converter/DiffusionMaps.h>
%include <shogun/converter/LaplacianEigenmaps.h>
%include <shogun/converter/LocalityPreservingProjections.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Converter_includes.i
Expand Up @@ -8,6 +8,7 @@
#include <shogun/converter/HessianLocallyLinearEmbedding.h>
#include <shogun/converter/KernelLocallyLinearEmbedding.h>
#include <shogun/converter/KernelLocalTangentSpaceAlignment.h>
#include <shogun/converter/SupervisedKernelLocalTangentSpaceAlignment.h>
#include <shogun/converter/DiffusionMaps.h>
#include <shogun/converter/LaplacianEigenmaps.h>
#include <shogun/converter/LocalityPreservingProjections.h>
Expand Down
124 changes: 124 additions & 0 deletions src/shogun/converter/SupervisedKernelLocalTangentSpaceAlignment.cpp
@@ -0,0 +1,124 @@
/*
* 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) 2012 Sergey Lisitsyn
* Copyright (C) 2012 Sergey Lisitsyn
*/

#include <shogun/converter/SupervisedKernelLocalTangentSpaceAlignment.h>
#ifdef HAVE_LAPACK
#include <shogun/converter/KernelLocallyLinearEmbedding.h>
#include <shogun/lib/Time.h>
#include <shogun/lib/common.h>
#include <shogun/mathematics/Math.h>
#include <shogun/io/SGIO.h>
#include <shogun/distance/Distance.h>
#include <shogun/lib/Signal.h>
#include <shogun/base/Parallel.h>

using namespace shogun;

class SKLTSA_COVERTREE_POINT
{
public:

SKLTSA_COVERTREE_POINT(int32_t index, const SGMatrix<float64_t>& dmatrix)
{
this->point_index = index;
this->kernel_matrix = dmatrix;
this->kii = dmatrix[index*dmatrix.num_rows+index];
}

inline double distance(const SKLTSA_COVERTREE_POINT& p) const
{
int32_t N = kernel_matrix.num_rows;
return kii+kernel_matrix[p.point_index*N+p.point_index]-2.0*kernel_matrix[point_index*N+p.point_index];
}

inline bool operator==(const SKLTSA_COVERTREE_POINT& p) const
{
return (p.point_index==this->point_index);
}

int32_t point_index;
float64_t kii;
SGMatrix<float64_t> kernel_matrix;
};

CSupervisedKernelLocalTangentSpaceAlignment::CSupervisedKernelLocalTangentSpaceAlignment() :
CKernelLocalTangentSpaceAlignment(), m_labels(NULL)
{
}

CSupervisedKernelLocalTangentSpaceAlignment::CSupervisedKernelLocalTangentSpaceAlignment(CKernel* kernel, CLabels* labels) :
CKernelLocalTangentSpaceAlignment(kernel), m_labels(NULL)
{
set_labels(labels);
}

CSupervisedKernelLocalTangentSpaceAlignment::~CSupervisedKernelLocalTangentSpaceAlignment()
{
SG_UNREF(m_labels);
}

const char* CSupervisedKernelLocalTangentSpaceAlignment::get_name() const
{
return "SupervisedKernelLocalTangentSpaceAlignment";
};

SGMatrix<int32_t> CSupervisedKernelLocalTangentSpaceAlignment::get_neighborhood_matrix(SGMatrix<float64_t> kernel_matrix, int32_t k)
{
int32_t i;
int32_t N = kernel_matrix.num_cols;
ASSERT(m_labels);
ASSERT(m_labels->get_num_labels()==N);

int32_t* neighborhood_matrix = SG_MALLOC(int32_t, N*k);

float64_t max_dist=0.0;
for (i=0; i<N; i++)
max_dist = CMath::max(max_dist,kernel_matrix[i*N+i]);

std::vector<SKLTSA_COVERTREE_POINT> vectors;
vectors.reserve(N);
for (i=0; i<N; i++)
vectors.push_back(SKLTSA_COVERTREE_POINT(i,kernel_matrix));

CoverTree<SKLTSA_COVERTREE_POINT>* coverTree = new CoverTree<SKLTSA_COVERTREE_POINT>(2.0*max_dist,vectors);

for (i=0; i<N; i++)
{
std::vector<SKLTSA_COVERTREE_POINT> neighbors =
coverTree->kNearestNeighbors(vectors[i],k+1);

ASSERT(neighbors.size()>=unsigned(k+1));

int32_t c = 0;
for (std::size_t m=1; m<unsigned(k+1); m++)
{
if (m_labels->get_int_label(i)==m_labels->get_int_label(neighbors[m].point_index))
{
neighborhood_matrix[i*k+c] = neighbors[m].point_index;
c++;
}
}
SG_PRINT("c=%d",c);
for (std::size_t m=1; c<k && m<unsigned(k+1); m++)
{
if (m_labels->get_int_label(i)!=m_labels->get_int_label(neighbors[m].point_index))
{
neighborhood_matrix[i*k+c] = neighbors[m].point_index;
c++;
}
}
}

delete coverTree;

return SGMatrix<int32_t>(neighborhood_matrix,k,N);
}

#endif /* HAVE_LAPACK */
82 changes: 82 additions & 0 deletions src/shogun/converter/SupervisedKernelLocalTangentSpaceAlignment.h
@@ -0,0 +1,82 @@
/*
* 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) 2012 Sergey Lisitsyn
* Copyright (C) 2012 Sergey Lisitsyn Berlin Institute of Technology and Max-Planck-Society
*/

#ifndef SUPERVISEDKERNELLOCALTANGENTSPACEALIGNMENT_H_
#define SUPERVISEDKERNELLOCALTANGENTSPACEALIGNMENT_H_
#include <shogun/lib/config.h>
#ifdef HAVE_LAPACK
#include <shogun/converter/KernelLocalTangentSpaceAlignment.h>
#include <shogun/features/Features.h>
#include <shogun/features/Labels.h>
#include <shogun/kernel/Kernel.h>

namespace shogun
{

class CFeatures;
class CKernel;

/** @brief class LocalTangentSpaceAlignment (part of the
* Efficient Dimensionality Reduction Toolkit) used to embed
* data using supervised kernel extension of the Local Tangent Space
* Alignment (LTSA) algorithm.
*/
class CSupervisedKernelLocalTangentSpaceAlignment: public CKernelLocalTangentSpaceAlignment
{
public:

/** constructor */
CSupervisedKernelLocalTangentSpaceAlignment();

/** constructor with kernel parameter
* @param kernel kernel to be used
*/
CSupervisedKernelLocalTangentSpaceAlignment(CKernel* kernel, CLabels* labels);

/** set labels */
void set_labels(CLabels* labels)
{
SG_REF(labels);
SG_UNREF(m_labels);
m_labels = labels;
}
/** get labels */
CLabels* get_labels() const
{
SG_REF(m_labels);
return m_labels;
}

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

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

protected:

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

protected:

/** labels */
CLabels* m_labels;

};
}

#endif /* HAVE_LAPACK */
#endif /* SUPERVISEDKERNELLOCALTANGENTSPACEALINGMENT_H_ */

0 comments on commit cb7c871

Please sign in to comment.