SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
tapkee_shogun.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Copyright (c) 2012-2013 Sergey Lisitsyn
8  */
9 
10 #include <shogun/lib/tapkee/tapkee_shogun.hpp>
11 
12 
13 #define CUSTOM_UNIFORM_RANDOM_INDEX_FUNCTION shogun::CMath::random()
14 #define CUSTOM_UNIFORM_RANDOM_FUNCTION shogun::CMath::random(static_cast<tapkee::ScalarType>(0),static_cast<tapkee::ScalarType>(1))
15 #define CUSTOM_GAUSSIAN_RANDOM_FUNCTION shogun::CMath::normal_random(static_cast<tapkee::ScalarType>(0),static_cast<tapkee::ScalarType>(1))
16 #define TAPKEE_EIGEN_INCLUDE_FILE <shogun/mathematics/eigen3.h>
17 
18 #ifdef HAVE_ARPACK
19  #define TAPKEE_WITH_ARPACK
20 #endif
21 #define TAPKEE_USE_LGPL_COVERTREE
22 #include <shogun/lib/tapkee/tapkee.hpp>
23 #include <shogun/lib/tapkee/callbacks/pimpl_callbacks.hpp>
24 
25 using namespace shogun;
26 
27 class ShogunLoggerImplementation : public tapkee::LoggerImplementation
28 {
29  virtual void message_info(const std::string& msg)
30  {
31  SG_SINFO((msg+"\n").c_str())
32  }
33  virtual void message_warning(const std::string& msg)
34  {
35  SG_SWARNING((msg+"\n").c_str())
36  }
37  virtual void message_error(const std::string& msg)
38  {
39  SG_SERROR((msg+"\n").c_str())
40  }
41  virtual void message_debug(const std::string& msg)
42  {
43  SG_SDEBUG((msg+"\n").c_str())
44  }
45  virtual void message_benchmark(const std::string& msg)
46  {
47  SG_SINFO((msg+"\n").c_str())
48  }
49 };
50 
52 {
53  ShogunFeatureVectorCallback(CDotFeatures* f) : dim(0), features(f) { }
54  inline tapkee::IndexType dimension() const
55  {
56  if (features)
57  return (dim = features->get_dim_feature_space());
58 
59  return 0;
60  }
61  inline void vector(int i, tapkee::DenseVector& v) const
62  {
63  v.setZero();
64  features->add_to_dense_vec(1.0,i,v.data(),dim);
65  }
66  mutable int32_t dim;
68 };
69 
70 
71 CDenseFeatures<float64_t>* shogun::tapkee_embed(const shogun::TAPKEE_PARAMETERS_FOR_SHOGUN& parameters)
72 {
73  tapkee::LoggingSingleton::instance().set_logger_impl(new ShogunLoggerImplementation);
74  tapkee::LoggingSingleton::instance().enable_benchmark();
75  tapkee::LoggingSingleton::instance().enable_info();
76 
77  pimpl_kernel_callback<CKernel> kernel_callback(parameters.kernel);
78  pimpl_distance_callback<CDistance> distance_callback(parameters.distance);
79  ShogunFeatureVectorCallback features_callback(parameters.features);
80 
81  tapkee::DimensionReductionMethod method;
82 #ifdef HAVE_ARPACK
83  tapkee::EigenMethod eigen_method = tapkee::Arpack;
84 #else
85  tapkee::EigenMethod eigen_method = tapkee::Dense;
86 #endif
87  tapkee::NeighborsMethod neighbors_method = tapkee::CoverTree;
88  size_t N = 0;
89 
90  switch (parameters.method)
91  {
92  case SHOGUN_KERNEL_LOCALLY_LINEAR_EMBEDDING:
93  case SHOGUN_LOCALLY_LINEAR_EMBEDDING:
94  method = tapkee::KernelLocallyLinearEmbedding;
95  N = parameters.kernel->get_num_vec_lhs();
96  break;
97  case SHOGUN_NEIGHBORHOOD_PRESERVING_EMBEDDING:
98  method = tapkee::NeighborhoodPreservingEmbedding;
99  N = parameters.kernel->get_num_vec_lhs();
100  break;
101  case SHOGUN_LOCAL_TANGENT_SPACE_ALIGNMENT:
102  method = tapkee::KernelLocalTangentSpaceAlignment;
103  N = parameters.kernel->get_num_vec_lhs();
104  break;
105  case SHOGUN_LINEAR_LOCAL_TANGENT_SPACE_ALIGNMENT:
106  method = tapkee::LinearLocalTangentSpaceAlignment;
107  N = parameters.kernel->get_num_vec_lhs();
108  break;
109  case SHOGUN_HESSIAN_LOCALLY_LINEAR_EMBEDDING:
110  method = tapkee::HessianLocallyLinearEmbedding;
111  N = parameters.kernel->get_num_vec_lhs();
112  break;
113  case SHOGUN_DIFFUSION_MAPS:
114  method = tapkee::DiffusionMap;
115  N = parameters.distance->get_num_vec_lhs();
116  break;
117  case SHOGUN_LAPLACIAN_EIGENMAPS:
118  method = tapkee::LaplacianEigenmaps;
119  N = parameters.distance->get_num_vec_lhs();
120  break;
121  case SHOGUN_LOCALITY_PRESERVING_PROJECTIONS:
122  method = tapkee::LocalityPreservingProjections;
123  N = parameters.distance->get_num_vec_lhs();
124  break;
125  case SHOGUN_MULTIDIMENSIONAL_SCALING:
126  method = tapkee::MultidimensionalScaling;
127  N = parameters.distance->get_num_vec_lhs();
128  break;
129  case SHOGUN_LANDMARK_MULTIDIMENSIONAL_SCALING:
130  method = tapkee::LandmarkMultidimensionalScaling;
131  N = parameters.distance->get_num_vec_lhs();
132  break;
133  case SHOGUN_ISOMAP:
134  method = tapkee::Isomap;
135  N = parameters.distance->get_num_vec_lhs();
136  break;
137  case SHOGUN_LANDMARK_ISOMAP:
138  method = tapkee::LandmarkIsomap;
139  N = parameters.distance->get_num_vec_lhs();
140  break;
141  case SHOGUN_STOCHASTIC_PROXIMITY_EMBEDDING:
142  method = tapkee::StochasticProximityEmbedding;
143  N = parameters.distance->get_num_vec_lhs();
144  break;
145  case SHOGUN_FACTOR_ANALYSIS:
146  method = tapkee::FactorAnalysis;
147  N = parameters.features->get_num_vectors();
148  break;
149  case SHOGUN_TDISTRIBUTED_STOCHASTIC_NEIGHBOR_EMBEDDING:
150  method = tapkee::tDistributedStochasticNeighborEmbedding;
151  N = parameters.features->get_num_vectors();
152  break;
153  case SHOGUN_MANIFOLD_SCULPTING:
154  method = tapkee::ManifoldSculpting;
155  N = parameters.features->get_num_vectors();
156  break;
157  }
158 
159  std::vector<int32_t> indices(N);
160  for (size_t i=0; i<N; i++)
161  indices[i] = i;
162 
163  tapkee::ParametersSet parameters_set =
164  (tapkee::keywords::method=method,
165  tapkee::keywords::eigen_method=eigen_method,
166  tapkee::keywords::neighbors_method=neighbors_method,
167  tapkee::keywords::num_neighbors=parameters.n_neighbors,
168  tapkee::keywords::diffusion_map_timesteps = parameters.n_timesteps,
169  tapkee::keywords::target_dimension = parameters.target_dimension,
170  tapkee::keywords::spe_num_updates = parameters.spe_num_updates,
171  tapkee::keywords::nullspace_shift = parameters.eigenshift,
172  tapkee::keywords::landmark_ratio = parameters.landmark_ratio,
173  tapkee::keywords::gaussian_kernel_width = parameters.gaussian_kernel_width,
174  tapkee::keywords::spe_tolerance = parameters.spe_tolerance,
175  tapkee::keywords::spe_global_strategy = parameters.spe_global_strategy,
176  tapkee::keywords::max_iteration = parameters.max_iteration,
177  tapkee::keywords::fa_epsilon = parameters.fa_epsilon,
178  tapkee::keywords::sne_perplexity = parameters.sne_perplexity,
179  tapkee::keywords::sne_theta = parameters.sne_theta,
180  tapkee::keywords::squishing_rate = parameters.squishing_rate
181  );
182 
183  tapkee::TapkeeOutput output = tapkee::embed(indices.begin(),indices.end(),
184  kernel_callback,distance_callback,features_callback,parameters_set);
185  tapkee::DenseMatrix result_embedding = output.embedding;
186  // destroy projecting function
187  output.projection.clear();
188 
189  SGMatrix<float64_t> feature_matrix(parameters.target_dimension,N);
190  // TODO avoid copying
191  for (uint32_t i=0; i<N; i++)
192  {
193  for (uint32_t j=0; j<parameters.target_dimension; j++)
194  {
195  feature_matrix(j,i) = result_embedding(i,j);
196  }
197  }
198  return new CDenseFeatures<float64_t>(feature_matrix);
199 }
200 
void vector(int i, tapkee::DenseVector &v) const
#define SG_SWARNING(...)
Definition: SGIO.h:178
Features that support dot products among other operations.
Definition: DotFeatures.h:44
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SDEBUG(...)
Definition: SGIO.h:168
#define SG_SERROR(...)
Definition: SGIO.h:179
#define SG_SINFO(...)
Definition: SGIO.h:173
ShogunFeatureVectorCallback(CDotFeatures *f)
tapkee::IndexType dimension() const

SHOGUN Machine Learning Toolbox - Documentation