small update

This commit is contained in:
Manish
2018-06-22 15:33:49 +05:30
parent 1f0858230c
commit 104fb2aa1e
6 changed files with 20 additions and 27 deletions
-5
View File
@@ -118,11 +118,6 @@ class Constraints
*/
void Triplets(arma::Mat<size_t>& 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.
+2 -2
View File
@@ -23,8 +23,8 @@ Constraints<MetricType>::Constraints(
const arma::mat& dataset,
const arma::Row<size_t>& labels,
size_t k) :
dataset(dataset),
labels(labels),
dataset(math::MakeAlias(const_cast<arma::mat&>(dataset), false)),
labels(math::MakeAlias(const_cast<arma::Row<size_t>&>(labels), false)),
k(k)
{
// Ensure a valid k is passed.
@@ -15,7 +15,6 @@
#include <mlpack/prereqs.hpp>
#include <mlpack/core/metrics/lmetric.hpp>
#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<MetricType> constraint;
//! Holds pre-calculated cij.
arma::mat p_cij;
/**
@@ -13,6 +13,7 @@
#define MLPACK_METHODS_LMNN_FUNCTION_IMPL_HPP
#include "lmnn_function.hpp"
#include "constraints.hpp"
#include <mlpack/core/math/make_alias.hpp>
#include <mlpack/core/optimizers/function.hpp>
@@ -32,8 +33,7 @@ LMNNFunction<MetricType>::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<MetricType>::LMNNFunction(const arma::mat& dataset,
targetNeighbors = arma::Mat<size_t>(k, dataset.n_cols, arma::fill::zeros);
impostors = arma::Mat<size_t>(k, dataset.n_cols, arma::fill::zeros);
distance = arma::mat(k, dataset.n_cols, arma::fill::zeros);
Constraints<MetricType> constraint(dataset, labels, k);
constraint.TargetNeighbors(targetNeighbors);
constraint.Impostors(impostors);
@@ -68,7 +68,7 @@ void LMNNFunction<MetricType>::Shuffle()
labels = std::move(newLabels);
// Re-calculate target neighbors as indices changed.
constraint.Dataset() = dataset;
Constraints<MetricType> constraint(dataset, labels, k);
constraint.TargetNeighbors(targetNeighbors);
}
@@ -84,7 +84,7 @@ double LMNNFunction<MetricType>::Evaluate(const arma::mat& transformation)
if (iteration % range == 0)
{
// Re-calculate impostors on transformed dataset.
constraint.Dataset() = transformedDataset;
Constraints<MetricType> constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, distance);
}
@@ -151,7 +151,7 @@ double LMNNFunction<MetricType>::Evaluate(const arma::mat& transformation,
if (iteration % range == 0)
{
// Re-calculate impostors on transformed dataset.
constraint.Dataset() = transformedDataset;
Constraints<MetricType> constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, distance, begin, batchSize);
}
@@ -322,7 +322,7 @@ double LMNNFunction<MetricType>::EvaluateWithGradient(
if (iteration % range == 0)
{
// Re-calculate impostors on transformed dataset.
constraint.Dataset() = transformedDataset;
Constraints<MetricType> constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, distance);
}
@@ -409,7 +409,7 @@ double LMNNFunction<MetricType>::EvaluateWithGradient(
if (iteration % range == 0)
{
// Re-calculate impostors on transformed dataset.
constraint.Dataset() = transformedDataset;
Constraints<MetricType> constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, distance, begin, batchSize);
}
+2 -2
View File
@@ -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"
+8 -7
View File
@@ -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<size_t> 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<arma::vec>::from(arma::find(Map
size_t index = arma::conv_to<size_t>::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