SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StochasticProximityEmbedding.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  * Written (W) 2012-2013 Fernando José Iglesias García
8  * Copyright (C) 2012-2013 Fernando José Iglesias García
9  */
10 
12 #include <shogun/lib/config.h>
13 #ifdef HAVE_EIGEN3
14 #include <shogun/io/SGIO.h>
15 #include <shogun/lib/tapkee/tapkee_shogun.hpp>
16 
17 using namespace shogun;
18 
21 {
22  // Initialize to default values
23  m_k = 12;
24  m_nupdates = 100;
25  m_strategy = SPE_GLOBAL;
26  m_tolerance = 1e-5;
27  m_max_iteration = 0;
28 
29  init();
30 }
31 
32 void CStochasticProximityEmbedding::init()
33 {
34  SG_ADD(&m_k, "m_k", "Number of neighbors", MS_NOT_AVAILABLE);
35  SG_ADD((machine_int_t*) &m_strategy, "m_strategy", "SPE strategy",
37  SG_ADD(&m_tolerance, "m_tolerance", "Regularization parameter",
39  SG_ADD(&m_max_iteration, "max_iteration", "maximum number of iterations",
41 }
42 
44 {
45 }
46 
48 {
49  if ( k <= 0 )
50  SG_ERROR("Number of neighbors k must be greater than 0")
51 
52  m_k = k;
53 }
54 
56 {
57  return m_k;
58 }
59 
61 {
62  m_strategy = strategy;
63 }
64 
66 {
67  return m_strategy;
68 }
69 
71 {
72  if ( tolerance <= 0 )
73  SG_ERROR("Tolerance regularization parameter must be greater "
74  "than 0");
75 
76  m_tolerance = tolerance;
77 }
78 
80 {
81  return m_tolerance;
82 }
83 
85 {
86  if ( nupdates <= 0 )
87  SG_ERROR("The number of updates must be greater than 0")
88 
89  m_nupdates = nupdates;
90 }
91 
93 {
94  return m_nupdates;
95 }
96 
97 void CStochasticProximityEmbedding::set_max_iteration(const int32_t max_iteration)
98 {
99  m_max_iteration = max_iteration;
100 }
101 
103 {
104  return m_max_iteration;
105 }
106 
108 {
109  return "StochasticProximityEmbedding";
110 }
111 
113 {
114  if ( !features )
115  SG_ERROR("Features are required to apply SPE\n")
116 
117  // Shorthand for the DenseFeatures
118  CDenseFeatures< float64_t >* simple_features =
119  (CDenseFeatures< float64_t >*) features;
120  SG_REF(features);
121 
122  // Get and check the number of vectors
123  int32_t N = simple_features->get_num_vectors();
124  if ( m_strategy == SPE_LOCAL && m_k >= N )
125  SG_ERROR("The number of neighbors (%d) must be less than "
126  "the number of vectors (%d)\n", m_k, N);
127 
128  if ( 2*m_nupdates > N )
129  SG_ERROR("The number of vectors (%d) must be at least two times "
130  "the number of updates (%d)\n", N, m_nupdates);
131 
132  m_distance->init(simple_features, simple_features);
133  CDenseFeatures< float64_t >* embedding = embed_distance(m_distance);
135 
136  SG_UNREF(features);
137  return (CFeatures*)embedding;
138 }
139 
140 CDenseFeatures< float64_t >* CStochasticProximityEmbedding::embed_distance(CDistance* distance)
141 {
142  TAPKEE_PARAMETERS_FOR_SHOGUN parameters;
143  parameters.n_neighbors = m_k;
144  parameters.method = SHOGUN_STOCHASTIC_PROXIMITY_EMBEDDING;
145  parameters.target_dimension = m_target_dim;
146  parameters.spe_num_updates = m_nupdates;
147  parameters.spe_tolerance = m_tolerance;
148  parameters.distance = distance;
149  parameters.spe_global_strategy = (m_strategy==SPE_GLOBAL);
150  parameters.max_iteration = m_max_iteration;
151  CDenseFeatures<float64_t>* embedding = tapkee_embed(parameters);
152  return embedding;
153 }
154 
155 #endif /* HAVE_EIGEN3 */

SHOGUN Machine Learning Toolbox - Documentation