Added a range parameter to calculate impostors after some particular iterations

This commit is contained in:
Manish
2018-06-13 18:33:02 +05:30
parent 059f3b1e71
commit 8a6709f089
4 changed files with 47 additions and 11 deletions
+5
View File
@@ -75,6 +75,11 @@ class LMNN
//! Modify the regularization value.
double& Regularization() { return objFunction.Regularization(); }
//! Access the range value.
const size_t& Range() const { return objFunction.Range(); }
//! Modify the range value.
size_t& Range() { return objFunction.Range(); }
//! Access the value of k.
const size_t& K() const { return k; }
@@ -157,6 +157,11 @@ class LMNNFunction
//! Modify the value of k.
size_t& K() { return k; }
//! Access the value of range.
const size_t& Range() const { return range; }
//! Modify the value of k.
size_t& Range() { return range; }
private:
//! data. This will be an alias until Shuffle() is called.
arma::mat dataset;
@@ -176,6 +181,10 @@ class LMNNFunction
MetricType metric;
//! Regularization value.
double regularization;
//! Keep iterations count.
size_t iteration;
//! Range after which impostors need to be recalculated.
size_t range;
//! Holds pre-calculated cij.
arma::mat p_cij;
//! False if nothing has ever been precalculated.
+26 -11
View File
@@ -32,6 +32,8 @@ LMNNFunction<MetricType>::LMNNFunction(const arma::mat& dataset,
k(k),
metric(metric),
regularization(regularization),
iteration(0),
range(1),
precalculated(false)
{
// Initialize the initial learning point.
@@ -74,9 +76,12 @@ double LMNNFunction<MetricType>::Evaluate(const arma::mat& coordinates)
// Apply metric over dataset.
transformedDataset = coordinates * dataset;
// Re-calculate impostors on transformed dataset.
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors);
if (iteration++ % range == 0)
{
// Re-calculate impostors on transformed dataset.
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors);
}
for (size_t i = 0; i < dataset.n_cols; i++)
{
@@ -128,8 +133,12 @@ double LMNNFunction<MetricType>::Evaluate(const arma::mat& coordinates,
// Apply metric over dataset.
transformedDataset = coordinates * dataset;
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, begin, batchSize);
if (iteration++ % range == 0)
{
// Re-calculate impostors on transformed dataset.
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, begin, batchSize);
}
for (size_t i = begin; i < begin + batchSize; i++)
{
@@ -286,9 +295,12 @@ double LMNNFunction<MetricType>::EvaluateWithGradient(
// Apply metric over dataset.
transformedDataset = coordinates * dataset;
// Calculate impostors.
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors);
if (iteration++ % range == 0)
{
// Re-calculate impostors on transformed dataset.
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors);
}
gradient.zeros(coordinates.n_rows, coordinates.n_cols);
@@ -361,9 +373,12 @@ double LMNNFunction<MetricType>::EvaluateWithGradient(
// Apply metric over dataset.
transformedDataset = coordinates * dataset;
// Calculate impostors.
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, begin, batchSize);
if (iteration++ % range == 0)
{
// Re-calculate impostors on transformed dataset.
Constraints constraint(transformedDataset, labels, k);
constraint.Impostors(impostors, begin, batchSize);
}
gradient.zeros(coordinates.n_rows, coordinates.n_cols);
+7
View File
@@ -134,6 +134,8 @@ PARAM_DOUBLE_IN("min_step", "Minimum step of line search for L-BFGS.", "m",
1e-20);
PARAM_DOUBLE_IN("max_step", "Maximum step of line search for L-BFGS.", "M",
1e20);
PARAM_INT_IN("range", "Number of iterations after which impostors needs to be "
"recalculated", "R", 1);
PARAM_INT_IN("seed", "Random seed. If 0, 'std::time(NULL)' is used.", "s", 0);
using namespace mlpack;
@@ -234,6 +236,7 @@ static void mlpackMain()
const double beta2 = CLI::GetParam<double>("beta2");
const double epsilon = CLI::GetParam<double>("epsilon");
const double batchDelta = CLI::GetParam<double>("batch_delta");
const size_t range = (size_t) CLI::GetParam<int>("range");
// Load data.
arma::mat data = std::move(CLI::GetParam<arma::mat>("input"));
@@ -283,6 +286,7 @@ static void mlpackMain()
{
LMNN<LMetric<2>> lmnn(data, labels, numTargets);
lmnn.Regularization() = regularization;
lmnn.Range() = range;
lmnn.Optimizer().StepSize() = stepSize;
lmnn.Optimizer().MaxIterations() = passes * data.n_cols;
lmnn.Optimizer().Beta1() = beta1;
@@ -298,6 +302,7 @@ static void mlpackMain()
{
LMNN<LMetric<2>, BBS_BB> lmnn(data, labels, numTargets);
lmnn.Regularization() = regularization;
lmnn.Range() = range;
lmnn.Optimizer().StepSize() = stepSize;
lmnn.Optimizer().BatchDelta() = batchDelta;
lmnn.Optimizer().MaxIterations() = passes * data.n_cols;
@@ -313,6 +318,7 @@ static void mlpackMain()
// diverge to inf causing serious memory problems.
LMNN<LMetric<2>, StandardSGD> lmnn(data, labels, numTargets);
lmnn.Regularization() = regularization;
lmnn.Range() = range;
lmnn.Optimizer().StepSize() = stepSize;
lmnn.Optimizer().MaxIterations() = passes * data.n_cols;
lmnn.Optimizer().Tolerance() = tolerance;
@@ -325,6 +331,7 @@ static void mlpackMain()
{
LMNN<LMetric<2>, L_BFGS> lmnn(data, labels, numTargets);
lmnn.Regularization() = regularization;
lmnn.Range() = range;
lmnn.Optimizer().NumBasis() = numBasis;
lmnn.Optimizer().MaxIterations() = maxIterations;
lmnn.Optimizer().ArmijoConstant() = armijoConstant;