From 104fb2aa1edeaefef02ad8dd04a7fe181f56384e Mon Sep 17 00:00:00 2001 From: Manish Date: Fri, 22 Jun 2018 15:33:49 +0530 Subject: [PATCH] small update --- src/mlpack/methods/lmnn/constraints.hpp | 5 ----- src/mlpack/methods/lmnn/constraints_impl.hpp | 4 ++-- src/mlpack/methods/lmnn/lmnn_function.hpp | 3 --- src/mlpack/methods/lmnn/lmnn_function_impl.hpp | 16 ++++++++-------- src/mlpack/methods/lmnn/lmnn_main.cpp | 4 ++-- src/mlpack/tests/lmnn_test.cpp | 15 ++++++++------- 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/mlpack/methods/lmnn/constraints.hpp b/src/mlpack/methods/lmnn/constraints.hpp index 7479083e81..3f19ca1ae2 100644 --- a/src/mlpack/methods/lmnn/constraints.hpp +++ b/src/mlpack/methods/lmnn/constraints.hpp @@ -118,11 +118,6 @@ class Constraints */ void Triplets(arma::Mat& outputMatrix); - //! Get the dataset reference. - const arma::mat& Dataset() const { return dataset; } - //! Modify the dataset reference. - arma::mat& Dataset() { return dataset; } - //! Access the value of k. const size_t& K() const { return k; } //! Modify the value of k. diff --git a/src/mlpack/methods/lmnn/constraints_impl.hpp b/src/mlpack/methods/lmnn/constraints_impl.hpp index 951012467d..a44207425a 100644 --- a/src/mlpack/methods/lmnn/constraints_impl.hpp +++ b/src/mlpack/methods/lmnn/constraints_impl.hpp @@ -23,8 +23,8 @@ Constraints::Constraints( const arma::mat& dataset, const arma::Row& labels, size_t k) : - dataset(dataset), - labels(labels), + dataset(math::MakeAlias(const_cast(dataset), false)), + labels(math::MakeAlias(const_cast&>(labels), false)), k(k) { // Ensure a valid k is passed. diff --git a/src/mlpack/methods/lmnn/lmnn_function.hpp b/src/mlpack/methods/lmnn/lmnn_function.hpp index 6b0d9f498c..fa6b1a3873 100644 --- a/src/mlpack/methods/lmnn/lmnn_function.hpp +++ b/src/mlpack/methods/lmnn/lmnn_function.hpp @@ -15,7 +15,6 @@ #include #include -#include "constraints.hpp" namespace mlpack { namespace lmnn { @@ -188,8 +187,6 @@ class LMNNFunction size_t iteration; //! Range after which impostors need to be recalculated. size_t range; - // Constraints object. - Constraints constraint; //! Holds pre-calculated cij. arma::mat p_cij; /** diff --git a/src/mlpack/methods/lmnn/lmnn_function_impl.hpp b/src/mlpack/methods/lmnn/lmnn_function_impl.hpp index 55a1fac571..b6acd11a6f 100644 --- a/src/mlpack/methods/lmnn/lmnn_function_impl.hpp +++ b/src/mlpack/methods/lmnn/lmnn_function_impl.hpp @@ -13,6 +13,7 @@ #define MLPACK_METHODS_LMNN_FUNCTION_IMPL_HPP #include "lmnn_function.hpp" +#include "constraints.hpp" #include #include @@ -32,8 +33,7 @@ LMNNFunction::LMNNFunction(const arma::mat& dataset, metric(metric), regularization(regularization), iteration(0), - range(1), - constraint(dataset, labels, k) + range(1) { // Initialize the initial learning point. initialPoint.eye(dataset.n_rows, dataset.n_rows); @@ -44,7 +44,7 @@ LMNNFunction::LMNNFunction(const arma::mat& dataset, targetNeighbors = arma::Mat(k, dataset.n_cols, arma::fill::zeros); impostors = arma::Mat(k, dataset.n_cols, arma::fill::zeros); distance = arma::mat(k, dataset.n_cols, arma::fill::zeros); - + Constraints constraint(dataset, labels, k); constraint.TargetNeighbors(targetNeighbors); constraint.Impostors(impostors); @@ -68,7 +68,7 @@ void LMNNFunction::Shuffle() labels = std::move(newLabels); // Re-calculate target neighbors as indices changed. - constraint.Dataset() = dataset; + Constraints constraint(dataset, labels, k); constraint.TargetNeighbors(targetNeighbors); } @@ -84,7 +84,7 @@ double LMNNFunction::Evaluate(const arma::mat& transformation) if (iteration % range == 0) { // Re-calculate impostors on transformed dataset. - constraint.Dataset() = transformedDataset; + Constraints constraint(transformedDataset, labels, k); constraint.Impostors(impostors, distance); } @@ -151,7 +151,7 @@ double LMNNFunction::Evaluate(const arma::mat& transformation, if (iteration % range == 0) { // Re-calculate impostors on transformed dataset. - constraint.Dataset() = transformedDataset; + Constraints constraint(transformedDataset, labels, k); constraint.Impostors(impostors, distance, begin, batchSize); } @@ -322,7 +322,7 @@ double LMNNFunction::EvaluateWithGradient( if (iteration % range == 0) { // Re-calculate impostors on transformed dataset. - constraint.Dataset() = transformedDataset; + Constraints constraint(transformedDataset, labels, k); constraint.Impostors(impostors, distance); } @@ -409,7 +409,7 @@ double LMNNFunction::EvaluateWithGradient( if (iteration % range == 0) { // Re-calculate impostors on transformed dataset. - constraint.Dataset() = transformedDataset; + Constraints constraint(transformedDataset, labels, k); constraint.Impostors(impostors, distance, begin, batchSize); } diff --git a/src/mlpack/methods/lmnn/lmnn_main.cpp b/src/mlpack/methods/lmnn/lmnn_main.cpp index 1ae93ebef8..601918886b 100644 --- a/src/mlpack/methods/lmnn/lmnn_main.cpp +++ b/src/mlpack/methods/lmnn/lmnn_main.cpp @@ -100,7 +100,7 @@ PROGRAM_INFO("Large Margin Nearest Neighbors (LMNN)", "literature on L-BFGS. In addition, a normalized starting point can be " "used by specifying the " + PRINT_PARAM_STRING("normalize") + " parameter." "\n\n" - "By default, the L-BFGS optimizer is used." + "By default, the AMSGrad optimizer is used." "\n\n" "Example - Let's say we want to learn distance on iris dataset with " "number of targets as 3 using BigBatch_SGD optimizer. A simple call for " @@ -121,7 +121,7 @@ PARAM_INT_IN("num_targets", "Number of target neighbors to use for each " "datapoint.", "k", 1); PARAM_MATRIX_OUT("output", "Output matrix for learned distance matrix.", "o"); PARAM_STRING_IN("optimizer", "Optimizer to use; 'amsgrad', 'bbsgd', 'sgd', or " - "'lbfgs'.", "O", "lbfgs"); + "'lbfgs'.", "O", "amsgrad"); PARAM_DOUBLE_IN("regularization", "Regularization for LMNN objective function ", "r", 0.5); PARAM_FLAG("normalize", "Use a normalized starting point for optimization. It" diff --git a/src/mlpack/tests/lmnn_test.cpp b/src/mlpack/tests/lmnn_test.cpp index f3fa7c4f2d..bd958c0238 100644 --- a/src/mlpack/tests/lmnn_test.cpp +++ b/src/mlpack/tests/lmnn_test.cpp @@ -392,26 +392,27 @@ double KnnAccuracy(const arma::mat& dataset, knn.Search(k, neighbors, distances); // Keep count. - double count = 0.0; + size_t count = 0.0; for (size_t i = 0; i < dataset.n_cols; i++) { - arma::Row Map; + arma::vec Map; Map.zeros(uniqueLabels.n_cols); - for (size_t j=0; j < k; j++) - Map(labels(neighbors(j, i))) += std::exp(1 / (j + 1)); + for (size_t j = 0; j < k; j++) + Map(labels(neighbors(j, i))) += + 1 / std::pow(distances(j, i) + 1, 2); - arma::vec index = arma::conv_to::from(arma::find(Map + size_t index = arma::conv_to::from(arma::find(Map == arma::max(Map))); // Increase count if labels match. - if (index(0) == labels(i)) + if (index == labels(i)) count++; } // return accuracy. - return (count / dataset.n_cols) * 100; + return ((double) count / dataset.n_cols) * 100; } // Check that final accuracy is greater than initial accuracy on