The Large Margin Nearest Neighbors function. More...
Public Member Functions | |
LMNNFunction (const arma::mat &dataset, const arma::Row< size_t > &labels, size_t k, double regularization, size_t range, MetricType metric=MetricType()) | |
Constructor for LMNNFunction class. More... | |
const arma::mat & | Dataset () const |
Return the dataset passed into the constructor. More... | |
double | Evaluate (const arma::mat &transformation) |
Evaluate the LMNN function for the given transformation matrix. More... | |
double | Evaluate (const arma::mat &transformation, const size_t begin, const size_t batchSize=1) |
Evaluate the LMNN objective function for the given transformation matrix on the given batch size from a given inital point of the dataset. More... | |
template < typename GradType > | |
double | EvaluateWithGradient (const arma::mat &transformation, GradType &gradient) |
Evaluate the LMNN objective function together with gradient for the given transformation matrix. More... | |
template < typename GradType > | |
double | EvaluateWithGradient (const arma::mat &transformation, const size_t begin, GradType &gradient, const size_t batchSize=1) |
Evaluate the LMNN objective function together with gradient for the given transformation matrix on the given batch size, from a given initial point of the dataset. More... | |
const arma::mat & | GetInitialPoint () const |
Return the initial point for the optimization. More... | |
template < typename GradType > | |
void | Gradient (const arma::mat &transformation, GradType &gradient) |
Evaluate the gradient of the LMNN function for the given transformation matrix. More... | |
template < typename GradType > | |
void | Gradient (const arma::mat &transformation, const size_t begin, GradType &gradient, const size_t batchSize=1) |
Evaluate the gradient of the LMNN function for the given transformation matrix on the given batch size, from a given initial point of the dataset. More... | |
const size_t & | K () const |
Access the value of k. More... | |
size_t & | K () |
Modify the value of k. More... | |
size_t | NumFunctions () const |
Get the number of functions the objective function can be decomposed into. More... | |
const size_t & | Range () const |
Access the value of range. More... | |
size_t & | Range () |
Modify the value of k. More... | |
const double & | Regularization () const |
Access the regularization value. More... | |
double & | Regularization () |
Modify the regularization value. More... | |
void | Shuffle () |
Shuffle the points in the dataset. More... | |
The Large Margin Nearest Neighbors function.
The actual function is
where x_n represents a point and A is the current scaling matrix.
This class is more flexible than the original paper, allowing an arbitrary metric function to be used in place of || A x_i - A x_j ||^2, meaning that the squared Euclidean distance is not the only allowed metric for LMNN. However, that is probably the best way to use this class.
In addition to the standard Evaluate() and Gradient() functions which mlpack optimizers use, overloads of Evaluate() and Gradient() are given which only operate on one point in the dataset. This is useful for optimizers like stochastic gradient descent (see ens::SGD).
Definition at line 46 of file lmnn_function.hpp.
LMNNFunction | ( | const arma::mat & | dataset, |
const arma::Row< size_t > & | labels, | ||
size_t | k, | ||
double | regularization, | ||
size_t | range, | ||
MetricType | metric = MetricType() |
||
) |
Constructor for LMNNFunction class.
dataset | Input dataset. |
labels | Input dataset labels. |
k | Number of target neighbors to be used. |
regularization | Regularization value. |
range | Range after which impostors need to be recalculated. |
metric | Type of metric used for computation. |
|
inline |
Return the dataset passed into the constructor.
Definition at line 175 of file lmnn_function.hpp.
double Evaluate | ( | const arma::mat & | transformation | ) |
Evaluate the LMNN function for the given transformation matrix.
This is the non-separable implementation, where the objective function is not decomposed into the sum of several objective functions.
transformation | Transformation matrix of Mahalanobis distance. |
double Evaluate | ( | const arma::mat & | transformation, |
const size_t | begin, | ||
const size_t | batchSize = 1 |
||
) |
Evaluate the LMNN objective function for the given transformation matrix on the given batch size from a given inital point of the dataset.
This is the separable implementation, where the objective function is decomposed into the sum of many objective functions, and here, only one of those constituent objective functions is returned.
transformation | Transformation matrix of Mahalanobis distance. |
begin | Index of the initial point to use for objective function. |
batchSize | Number of points to use for objective function. |
double EvaluateWithGradient | ( | const arma::mat & | transformation, |
GradType & | gradient | ||
) |
Evaluate the LMNN objective function together with gradient for the given transformation matrix.
This is the non-separable implementation, where the objective function is not decomposed into the sum of several objective functions.
GradType | The type of the gradient out-param. |
transformation | Transformation matrix of Mahalanobis distance. |
gradient | Matrix to store the calculated gradient in. |
double EvaluateWithGradient | ( | const arma::mat & | transformation, |
const size_t | begin, | ||
GradType & | gradient, | ||
const size_t | batchSize = 1 |
||
) |
Evaluate the LMNN objective function together with gradient for the given transformation matrix on the given batch size, from a given initial point of the dataset.
This is the separable implementation, where the objective function is decomposed into the sum of many objective functions, and here, only one of those constituent objective functions is returned. The type of the gradient parameter is a template argument to allow the computation of a sparse gradient.
GradType | The type of the gradient out-param. |
transformation | Transformation matrix of Mahalanobis distance. |
begin | Index of the initial point to use for objective function. |
gradient | Matrix to store the calculated gradient in. |
batchSize | Number of points to use for objective function. |
|
inline |
Return the initial point for the optimization.
Definition at line 166 of file lmnn_function.hpp.
void Gradient | ( | const arma::mat & | transformation, |
GradType & | gradient | ||
) |
Evaluate the gradient of the LMNN function for the given transformation matrix.
This is the non-separable implementation, where the objective function is not decomposed into the sum of several objective functions.
GradType | The type of the gradient out-param. |
transformation | Transformation matrix of Mahalanobis distance. |
gradient | Matrix to store the calculated gradient in. |
void Gradient | ( | const arma::mat & | transformation, |
const size_t | begin, | ||
GradType & | gradient, | ||
const size_t | batchSize = 1 |
||
) |
Evaluate the gradient of the LMNN function for the given transformation matrix on the given batch size, from a given initial point of the dataset.
This is the separable implementation, where the objective function is decomposed into the sum of many objective functions, and here, only one of those constituent objective functions is returned. The type of the gradient parameter is a template argument to allow the computation of a sparse gradient.
GradType | The type of the gradient out-param. |
transformation | Transformation matrix of Mahalanobis distance. |
begin | Index of the initial point to use for objective function. |
gradient | Matrix to store the calculated gradient in. |
batchSize | Number of points to use for objective function. |
|
inline |
Access the value of k.
Definition at line 183 of file lmnn_function.hpp.
|
inline |
Modify the value of k.
Definition at line 185 of file lmnn_function.hpp.
|
inline |
Get the number of functions the objective function can be decomposed into.
This is just the number of points in the dataset.
Definition at line 172 of file lmnn_function.hpp.
|
inline |
Access the value of range.
Definition at line 188 of file lmnn_function.hpp.
|
inline |
Modify the value of k.
Definition at line 190 of file lmnn_function.hpp.
|
inline |
Access the regularization value.
Definition at line 178 of file lmnn_function.hpp.
|
inline |
Modify the regularization value.
Definition at line 180 of file lmnn_function.hpp.
void Shuffle | ( | ) |
Shuffle the points in the dataset.
This may be used by optimizers.