[mlpack-svn] r13966 - mlpack/trunk/src/mlpack/methods/nca
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Sun Dec 2 23:55:19 EST 2012
Author: rcurtin
Date: 2012-12-02 23:55:19 -0500 (Sun, 02 Dec 2012)
New Revision: 13966
Modified:
mlpack/trunk/src/mlpack/methods/nca/nca.hpp
mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp
mlpack/trunk/src/mlpack/methods/nca/nca_main.cpp
Log:
Refactor NCA to take an optimizer as a template parameter, and default to SGD.
Modified: mlpack/trunk/src/mlpack/methods/nca/nca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca.hpp 2012-12-01 22:59:57 UTC (rev 13965)
+++ mlpack/trunk/src/mlpack/methods/nca/nca.hpp 2012-12-03 04:55:19 UTC (rev 13966)
@@ -9,7 +9,10 @@
#include <mlpack/core.hpp>
#include <mlpack/core/metrics/lmetric.hpp>
+#include <mlpack/core/optimizers/sgd/sgd.hpp>
+#include "nca_softmax_error_function.hpp"
+
namespace mlpack {
namespace nca /** Neighborhood Components Analysis. */ {
@@ -36,7 +39,8 @@
* }
* @endcode
*/
-template<typename MetricType>
+template<typename MetricType = metric::SquaredEuclideanDistance,
+ template<typename> class OptimizerType = optimization::SGD>
class NCA
{
public:
@@ -55,10 +59,6 @@
*/
NCA(const arma::mat& dataset,
const arma::uvec& labels,
- const double stepSize = 0.01,
- const size_t maxIterations = 500000,
- const double tolerance = 1e-10,
- const bool shuffle = true,
MetricType metric = MetricType());
/**
@@ -77,21 +77,12 @@
//! Get the labels reference.
const arma::uvec& Labels() const { return labels; }
- //! Get the step size for stochastic gradient descent.
- double StepSize() const { return stepSize; }
- //! Modify the step size for stochastic gradient descent.
- double& StepSize() { return stepSize; }
+ //! Get the optimizer.
+ const OptimizerType<SoftmaxErrorFunction<MetricType> >& Optimizer() const
+ { return optimizer; }
+ OptimizerType<SoftmaxErrorFunction<MetricType> >& Optimizer()
+ { return optimizer; }
- //! Get the maximum number of iterations for stochastic gradient descent.
- size_t MaxIterations() const { return maxIterations; }
- //! Modify the maximum number of iterations for stochastic gradient descent.
- size_t& MaxIterations() { return maxIterations; }
-
- //! Get the tolerance for the termination of stochastic gradient descent.
- double Tolerance() const { return tolerance; }
- //! Modify the tolerance for the termination of stochastic gradient descent.
- double& Tolerance() { return tolerance; }
-
private:
//! Dataset reference.
const arma::mat& dataset;
@@ -101,14 +92,11 @@
//! Metric to be used.
MetricType metric;
- //! Step size for stochastic gradient descent.
- double stepSize;
- //! Maximum iterations for stochastic gradient descent.
- size_t maxIterations;
- //! Tolerance for termination of stochastic gradient descent.
- double tolerance;
- //! Whether or not to shuffle the dataset for SGD.
- bool shuffle;
+ //! The function to optimize.
+ SoftmaxErrorFunction<MetricType> errorFunction;
+
+ //! The optimizer to use.
+ OptimizerType<SoftmaxErrorFunction<MetricType> > optimizer;
};
}; // namespace nca
Modified: mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp 2012-12-01 22:59:57 UTC (rev 13965)
+++ mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp 2012-12-03 04:55:19 UTC (rev 13966)
@@ -10,48 +10,32 @@
// In case it was not already included.
#include "nca.hpp"
-#include <mlpack/core/optimizers/sgd/sgd.hpp>
-
-#include "nca_softmax_error_function.hpp"
-
namespace mlpack {
namespace nca {
// Just set the internal matrix reference.
-template<typename MetricType>
-NCA<MetricType>::NCA(const arma::mat& dataset,
- const arma::uvec& labels,
- const double stepSize,
- const size_t maxIterations,
- const double tolerance,
- const bool shuffle,
- MetricType metric) :
+template<typename MetricType, template<typename> class OptimizerType>
+NCA<MetricType, OptimizerType>::NCA(const arma::mat& dataset,
+ const arma::uvec& labels,
+ MetricType metric) :
dataset(dataset),
labels(labels),
metric(metric),
- stepSize(stepSize),
- maxIterations(maxIterations),
- tolerance(tolerance),
- shuffle(shuffle)
+ errorFunction(dataset, labels, metric),
+ optimizer(OptimizerType<SoftmaxErrorFunction<MetricType> >(errorFunction))
{ /* Nothing to do. */ }
-template<typename MetricType>
-void NCA<MetricType>::LearnDistance(arma::mat& outputMatrix)
+template<typename MetricType, template<typename> class OptimizerType>
+void NCA<MetricType, OptimizerType>::LearnDistance(arma::mat& outputMatrix)
{
// See if we were passed an initialized matrix.
if ((outputMatrix.n_rows != dataset.n_rows) ||
(outputMatrix.n_cols != dataset.n_rows))
outputMatrix.eye(dataset.n_rows, dataset.n_rows);
- SoftmaxErrorFunction<MetricType> errorFunc(dataset, labels, metric);
-
- // We will use stochastic gradient descent to optimize the NCA error function.
- optimization::SGD<SoftmaxErrorFunction<MetricType> > sgd(errorFunc, stepSize,
- maxIterations, tolerance, shuffle);
-
Timer::Start("nca_sgd_optimization");
- sgd.Optimize(outputMatrix);
+ optimizer.Optimize(outputMatrix);
Timer::Stop("nca_sgd_optimization");
}
Modified: mlpack/trunk/src/mlpack/methods/nca/nca_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca_main.cpp 2012-12-01 22:59:57 UTC (rev 13965)
+++ mlpack/trunk/src/mlpack/methods/nca/nca_main.cpp 2012-12-03 04:55:19 UTC (rev 13966)
@@ -126,8 +126,11 @@
}
// Now create the NCA object and run the optimization.
- NCA<LMetric<2> > nca(data, labels.unsafe_col(0), stepSize, maxIterations,
- tolerance, shuffle);
+ NCA<LMetric<2> > nca(data, labels.unsafe_col(0));
+ nca.Optimizer().StepSize() = stepSize;
+ nca.Optimizer().MaxIterations() = maxIterations;
+ nca.Optimizer().Tolerance() = tolerance;
+ nca.Optimizer().Shuffle() = shuffle;
nca.LearnDistance(distance);
More information about the mlpack-svn
mailing list