/usr/include/shogun/metric/LMNN.h is in libshogun-dev 3.2.0-7.3build4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | /*
* 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) 2013 Fernando J. Iglesias Garcia
* Copyright (C) 2013 Fernando J. Iglesias Garcia
*/
#ifndef LMNN_H_
#define LMNN_H_
#include <shogun/lib/config.h>
#ifdef HAVE_EIGEN3
#ifdef HAVE_LAPACK
#include <shogun/base/SGObject.h>
#include <shogun/distance/CustomMahalanobisDistance.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/lib/SGMatrix.h>
namespace shogun
{
// Forward declaration
class CLMNNStatistics;
/**
* @brief Class LMNN that implements the distance metric learning technique
* Large Margin Nearest Neighbour (LMNN) described in
*
* Weinberger, K. Q., Saul, L. K.
* Distance Metric Learning for Large Margin Nearest Neighbor Classification.
*/
class CLMNN : public CSGObject
{
public:
/** default constructor */
CLMNN();
/** standard constructor
*
* @param features feature vectors
* @param labels labels of the features
* @param k number of target neighbours per example
*/
CLMNN(CDenseFeatures<float64_t>* features, CMulticlassLabels* labels, int32_t k);
/** destructor */
virtual ~CLMNN();
/** @return name of SGSerializable */
virtual const char* get_name() const;
/**
* LMNN algorithm to learn a linear transformation of the original feature
* space (or, equivalently, a Mahalanobis distance) such that kNN
* classification performance is maximized
*
* @param init_transform initial linear transform
*/
void train(SGMatrix<float64_t> init_transform=SGMatrix<float64_t>());
/** get the learnt linear transform (denoted L in LMNN literature typically)
*
* @return the linear transform L
*/
SGMatrix<float64_t> get_linear_transform() const;
/**
* get the learnt Mahalanobis distance (typically denoted M in LMNN literature)
* encapsulated in a CCustomMahalanobisDistance object, suitable to be used in kNN
*
* @return the distance M
*/
CCustomMahalanobisDistance* get_distance() const;
/** get the number of target neighbours per example
*
* @return number of neighbours per example
*/
int32_t get_k() const;
/** set the number of target neighbours per example
*
* @param k the number of target neighbours per example
*/
void set_k(const int32_t k);
/** get regularization
*
* @return regularization strength
*/
float64_t get_regularization() const;
/** set regularization
*
* @param regularization regularization strength to set
*/
void set_regularization(const float64_t regularization);
/** get step size
*
* @return step size
*/
float64_t get_stepsize() const;
/** set step size
*
* @param stepsize step size to set
*/
void set_stepsize(const float64_t stepsize);
/** get step size threshold
*
* @return step size threshold
*/
float64_t get_stepsize_threshold() const;
/** set step size threshold
*
* @param stepsize_threshold step size threshold to set
*/
void set_stepsize_threshold(const float64_t stepsize_threshold);
/** get maximum number of iterations
*
* @return maximum number of iterations
*/
uint32_t get_maxiter() const;
/** set maximum number of iterations
*
* @param maxiter maximum number of iterations to set
*/
void set_maxiter(const uint32_t maxiter);
/** get number of iterations between exact impostors search
*
* @return iterations between exact impostors search
*/
uint32_t get_correction() const;
/** set number of iterations between exact impostors search
*
* @param correction iterations between exact impostors search
*/
void set_correction(const uint32_t correction);
/** get objective threshold
*
* @return objective threshold
*/
float64_t get_obj_threshold() const;
/** set objective threshold
*
* @param obj_threshold objective threshold to set
*/
void set_obj_threshold(const float64_t obj_threshold);
/** get whether the linear transform will be diagonal
*
* @return whether the linear transform will be diagonal
*/
bool get_diagonal() const;
/** set whether the linear transform will be diagonal
*
* @param diagonal whether the linear transform will be diagonal
*/
void set_diagonal(const bool diagonal);
/** get LMNN training statistics
*
* @return LMNN training statistics
*/
CLMNNStatistics* get_statistics() const;
private:
/** register parameters */
void init();
private:
/** the linear transform learnt by LMNN once train has been called */
SGMatrix<float64_t> m_linear_transform;
/** training features */
CFeatures* m_features;
/** training labels */
CLabels* m_labels;
/**
* trade-off between pull and push forces in the objective.
* Its default value is 0.5
*/
float64_t m_regularization;
/** number of target neighbours to use per training example */
int32_t m_k;
/**
* learning rate or step size used in gradient descent.
* Its deafult value is 1e-07.
*/
float64_t m_stepsize;
/**
* step size threshold; during training the step size is modified
* internally, stop training if the step size is below this threshold.
* Its default value is 1e-22.
*/
float64_t m_stepsize_threshold;
/** maximum number of iterations. Its default value is 1000. */
uint32_t m_maxiter;
/**
* number of iterations between exact computation of impostors.
* Its default value is 15
*/
uint32_t m_correction;
/**
* objective threshold; stop training if the first order difference in
* absolute value of the objective function in the last three iterations
* is below (element-wise) this threshold times the current objective.
* Its default value is 1e-9.
*/
float64_t m_obj_threshold;
/**
* whether m_linear_transform is forced to be diagonal (useful to
* perform feature selection). Its default value is false.
*/
bool m_diagonal;
/** training statistics, @see CLMNNStatistics */
CLMNNStatistics* m_statistics;
}; /* class CLMNN */
/**
* @brief Class LMNNStatistics used to give access to intermediate results
* obtained training LMNN.
*/
class CLMNNStatistics : public CSGObject
{
public:
/** default constructor */
CLMNNStatistics();
/** destructor */
virtual ~CLMNNStatistics();
/** @return name of SGSerializable */
virtual const char* get_name() const;
/**
* resize CLMNNStatistics::obj, CLMNNStatistics::stepsize and
* CLMNNStatistics::num_impostors to fit the specified number of elements
*
* @param size number of elements
*/
void resize(int32_t size);
/**
* set objective, step size and number of impostors computed at the
* specified iteration
*
* @param iter index to store the parameters, must be greater or equal to zero,
* and less than the size
* @param obj_iter objective to set
* @param stepsize_iter stepsize to set
* @param num_impostors_iter number of impostors to set
*/
void set(index_t iter, float64_t obj_iter, float64_t stepsize_iter, uint32_t num_impostors_iter);
private:
/** register parameters */
void init();
public:
/** objective function at each iteration */
SGVector<float64_t> obj;
/** step size at each iteration */
SGVector<float64_t> stepsize;
/** number of impostors at each iteration */
SGVector<uint32_t> num_impostors;
};
} /* namespace shogun */
#endif /* HAVE_LAPACK */
#endif /* HAVE_EIGEN3 */
#endif /* LMNN_H_ */
|