SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 #include <shogun/io/SGIO.h>
14 #include <shogun/lib/tapkee/tapkee_shogun.hpp>
15 
16 using namespace shogun;
17 
20 {
21  // Initialize to default values
22  m_k = 12;
23  m_nupdates = 100;
24  m_strategy = SPE_GLOBAL;
25  m_tolerance = 1e-5;
26  m_max_iteration = 0;
27 
28  init();
29 }
30 
31 void CStochasticProximityEmbedding::init()
32 {
33  SG_ADD(&m_k, "m_k", "Number of neighbors", MS_NOT_AVAILABLE);
34  SG_ADD((machine_int_t*) &m_strategy, "m_strategy", "SPE strategy",
36  SG_ADD(&m_tolerance, "m_tolerance", "Regularization parameter",
38  SG_ADD(&m_max_iteration, "max_iteration", "maximum number of iterations",
40 }
41 
43 {
44 }
45 
47 {
48  if ( k <= 0 )
49  SG_ERROR("Number of neighbors k must be greater than 0")
50 
51  m_k = k;
52 }
53 
55 {
56  return m_k;
57 }
58 
60 {
61  m_strategy = strategy;
62 }
63 
65 {
66  return m_strategy;
67 }
68 
70 {
71  if ( tolerance <= 0 )
72  SG_ERROR("Tolerance regularization parameter must be greater "
73  "than 0");
74 
75  m_tolerance = tolerance;
76 }
77 
79 {
80  return m_tolerance;
81 }
82 
84 {
85  if ( nupdates <= 0 )
86  SG_ERROR("The number of updates must be greater than 0")
87 
88  m_nupdates = nupdates;
89 }
90 
92 {
93  return m_nupdates;
94 }
95 
96 void CStochasticProximityEmbedding::set_max_iteration(const int32_t max_iteration)
97 {
98  m_max_iteration = max_iteration;
99 }
100 
102 {
103  return m_max_iteration;
104 }
105 
107 {
108  return "StochasticProximityEmbedding";
109 }
110 
112 {
113  if ( !features )
114  SG_ERROR("Features are required to apply SPE\n")
115 
116  // Shorthand for the DenseFeatures
117  CDenseFeatures< float64_t >* simple_features =
118  (CDenseFeatures< float64_t >*) features;
119  SG_REF(features);
120 
121  // Get and check the number of vectors
122  int32_t N = simple_features->get_num_vectors();
123  if ( m_strategy == SPE_LOCAL && m_k >= N )
124  SG_ERROR("The number of neighbors (%d) must be less than "
125  "the number of vectors (%d)\n", m_k, N);
126 
127  if ( 2*m_nupdates > N )
128  SG_ERROR("The number of vectors (%d) must be at least two times "
129  "the number of updates (%d)\n", N, m_nupdates);
130 
131  m_distance->init(simple_features, simple_features);
132  CDenseFeatures< float64_t >* embedding = embed_distance(m_distance);
134 
135  SG_UNREF(features);
136  return (CFeatures*)embedding;
137 }
138 
139 CDenseFeatures< float64_t >* CStochasticProximityEmbedding::embed_distance(CDistance* distance)
140 {
141  TAPKEE_PARAMETERS_FOR_SHOGUN parameters;
142  parameters.n_neighbors = m_k;
143  parameters.method = SHOGUN_STOCHASTIC_PROXIMITY_EMBEDDING;
144  parameters.target_dimension = m_target_dim;
145  parameters.spe_num_updates = m_nupdates;
146  parameters.spe_tolerance = m_tolerance;
147  parameters.distance = distance;
148  parameters.spe_global_strategy = (m_strategy==SPE_GLOBAL);
149  parameters.max_iteration = m_max_iteration;
150  CDenseFeatures<float64_t>* embedding = tapkee_embed(parameters);
151  return embedding;
152 }
153 
float distance(CJLCoverTreePoint p1, CJLCoverTreePoint p2, float64_t upper_bound)
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
virtual CFeatures * apply(CFeatures *features)
class EmbeddingConverter (part of the Efficient Dimensionality Reduction Toolkit) used to construct e...
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_REF(x)
Definition: SGObject.h:54
virtual int32_t get_num_vectors() const
virtual void remove_lhs_and_rhs()
Definition: Distance.cpp:144
void set_max_iteration(const int32_t max_iteration)
float float32_t
Definition: common.h:49
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
int machine_int_t
Definition: common.h:59
The class Features is the base class of all feature objects.
Definition: Features.h:68
#define SG_ADD(...)
Definition: SGObject.h:84
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Distance.cpp:78

SHOGUN Machine Learning Toolbox - Documentation