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

SHOGUN Machine Learning Toolbox - Documentation