Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
213 additions
and
2 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
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
124 changes: 124 additions & 0 deletions
124
src/shogun/converter/SupervisedKernelLocalTangentSpaceAlignment.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,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
82
src/shogun/converter/SupervisedKernelLocalTangentSpaceAlignment.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |