mlpack
Public Member Functions | List of all members
mlpack::nca::SoftmaxErrorFunction< MetricType > Class Template Reference

The "softmax" stochastic neighbor assignment probability function. More...

#include <nca_softmax_error_function.hpp>

Public Member Functions

 SoftmaxErrorFunction (const arma::mat &dataset, const arma::Row< size_t > &labels, MetricType metric=MetricType())
 Initialize with the given kernel; useful when the kernel has some state to store, which is set elsewhere. More...
 
void Shuffle ()
 Shuffle the dataset.
 
double Evaluate (const arma::mat &covariance)
 Evaluate the softmax function for the given covariance matrix. More...
 
double Evaluate (const arma::mat &covariance, const size_t begin, const size_t batchSize=1)
 Evaluate the softmax objective function for the given covariance matrix on the given batch size from a given inital point of the dataset. More...
 
void Gradient (const arma::mat &covariance, arma::mat &gradient)
 Evaluate the gradient of the softmax function for the given covariance matrix. More...
 
template<typename GradType >
void Gradient (const arma::mat &covariance, const size_t begin, GradType &gradient, const size_t batchSize=1)
 Evaluate the gradient of the softmax function for the given covariance matrix on the given batch size, from a given initial point of the dataset. More...
 
const arma::mat GetInitialPoint () const
 Get the initial point.
 
size_t NumFunctions () const
 Get the number of functions the objective function can be decomposed into. More...
 

Detailed Description

template<typename MetricType = metric::SquaredEuclideanDistance>
class mlpack::nca::SoftmaxErrorFunction< MetricType >

The "softmax" stochastic neighbor assignment probability function.

The actual function is

p_ij = (exp(-|| A x_i - A x_j || ^ 2)) / (sum_{k != i} (exp(-|| A x_i - A x_k || ^ 2)))

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 NCA. 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 mlpack::optimization::SGD).

Constructor & Destructor Documentation

◆ SoftmaxErrorFunction()

template<typename MetricType >
mlpack::nca::SoftmaxErrorFunction< MetricType >::SoftmaxErrorFunction ( const arma::mat &  dataset,
const arma::Row< size_t > &  labels,
MetricType  metric = MetricType() 
)

Initialize with the given kernel; useful when the kernel has some state to store, which is set elsewhere.

If no kernel is given, an empty kernel is used; this way, you can call the constructor with no arguments. A reference to the dataset we will be optimizing over is also required.

Parameters
datasetMatrix containing the dataset.
labelsVector of class labels for each point in the dataset.
metricInstantiated metric (optional).

Member Function Documentation

◆ Evaluate() [1/2]

template<typename MetricType >
double mlpack::nca::SoftmaxErrorFunction< MetricType >::Evaluate ( const arma::mat &  covariance)

Evaluate the softmax function for the given covariance matrix.

The non-separable implementation, which uses Precalculate() to save time.

This is the non-separable implementation, where the objective function is not decomposed into the sum of several objective functions.

Parameters
covarianceCovariance matrix of Mahalanobis distance.

◆ Evaluate() [2/2]

template<typename MetricType >
double mlpack::nca::SoftmaxErrorFunction< MetricType >::Evaluate ( const arma::mat &  coordinates,
const size_t  begin,
const size_t  batchSize = 1 
)

Evaluate the softmax objective function for the given covariance matrix on the given batch size from a given inital point of the dataset.

The separated objective function, which does not use Precalculate(), for a given batch size and from an initial index.

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.

Parameters
covarianceCovariance matrix of Mahalanobis distance.
beginIndex of the initial point to use for objective function.
batchSizeNumber of points to use for objective function.

◆ Gradient() [1/2]

template<typename MetricType >
void mlpack::nca::SoftmaxErrorFunction< MetricType >::Gradient ( const arma::mat &  covariance,
arma::mat &  gradient 
)

Evaluate the gradient of the softmax function for the given covariance matrix.

The non-separable implementation, where Precalculate() is used.

This is the non-separable implementation, where the objective function is not decomposed into the sum of several objective functions.

Parameters
covarianceCovariance matrix of Mahalanobis distance.
gradientMatrix to store the calculated gradient in.

◆ Gradient() [2/2]

template<typename MetricType >
template<typename GradType >
void mlpack::nca::SoftmaxErrorFunction< MetricType >::Gradient ( const arma::mat &  covariance,
const size_t  begin,
GradType &  gradient,
const size_t  batchSize = 1 
)

Evaluate the gradient of the softmax function for the given covariance matrix on the given batch size, from a given initial point of the dataset.

The separable implementation for a given batch size and an initial index.

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.

Template Parameters
GradTypeThe type of the gradient out-param.
Parameters
covarianceCovariance matrix of Mahalanobis distance.
beginIndex of the initial point to use for objective function.
batchSizeNumber of points to use for objective function.
gradientMatrix to store the calculated gradient in.

◆ NumFunctions()

template<typename MetricType = metric::SquaredEuclideanDistance>
size_t mlpack::nca::SoftmaxErrorFunction< MetricType >::NumFunctions ( ) const
inline

Get the number of functions the objective function can be decomposed into.

This is just the number of points in the dataset.


The documentation for this class was generated from the following files: