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