From 68f7762db36bc36252b8daf57ee2e7d21fd876ad Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Tue, 1 May 2018 19:14:59 +0200 Subject: [PATCH] Make sure we have all the methods that we need. --- .../bigbatch_sgd/bigbatch_sgd_impl.hpp | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/mlpack/core/optimizers/bigbatch_sgd/bigbatch_sgd_impl.hpp b/src/mlpack/core/optimizers/bigbatch_sgd/bigbatch_sgd_impl.hpp index b83d93e948..59db23532a 100644 --- a/src/mlpack/core/optimizers/bigbatch_sgd/bigbatch_sgd_impl.hpp +++ b/src/mlpack/core/optimizers/bigbatch_sgd/bigbatch_sgd_impl.hpp @@ -15,6 +15,8 @@ // In case it hasn't been included yet. #include "bigbatch_sgd.hpp" +#include + namespace mlpack { namespace optimization { @@ -41,8 +43,15 @@ template double BigBatchSGD::Optimize( DecomposableFunctionType& function, arma::mat& iterate) { + typedef Function FullFunctionType; + FullFunctionType& f(static_cast(function)); + + // Make sure we have all the methods that we need. + traits::CheckDecomposableFunctionTypeAPI(); + + // Find the number of functions to use. - const size_t numFunctions = function.NumFunctions(); + const size_t numFunctions = f.NumFunctions(); // To keep track of where we are and how things are going. size_t currentFunction = 0; @@ -53,7 +62,7 @@ double BigBatchSGD::Optimize( // Calculate the first objective function. for (size_t i = 0; i < numFunctions; ++i) - overallObjective += function.Evaluate(iterate, i); + overallObjective += f.Evaluate(iterate, i); // Now iterate! arma::mat gradient(iterate.n_rows, iterate.n_cols); @@ -90,7 +99,7 @@ double BigBatchSGD::Optimize( currentFunction = 0; if (shuffle) // Determine order of visitation. - function.Shuffle(); + f.Shuffle(); } // Find the effective batch size; we have to take the minimum of three @@ -107,12 +116,12 @@ double BigBatchSGD::Optimize( double vB = 0; // Compute the stochastic gradient estimation. - function.Gradient(iterate, currentFunction, gradient, 1); + f.Gradient(iterate, currentFunction, gradient, 1); delta1 = gradient; for (size_t j = 1; j < effectiveBatchSize; ++j, ++k) { - function.Gradient(iterate, currentFunction + j, functionGradient, 1); + f.Gradient(iterate, currentFunction + j, functionGradient, 1); delta0 = delta1 + (functionGradient - delta1) / k; // Compute sample variance. @@ -146,7 +155,7 @@ double BigBatchSGD::Optimize( - 1) < numFunctions ? currentFunction + batchSize - 1 : 0; for (size_t j = 0; j < batchOffset; ++j, ++k) { - function.Gradient(iterate, batchStart + j, functionGradient, 1); + f.Gradient(iterate, batchStart + j, functionGradient, 1); delta0 = delta1 + (functionGradient - delta1) / (k + 1); // Compute sample variance. @@ -167,13 +176,13 @@ double BigBatchSGD::Optimize( } } - updatePolicy.Update(function, stepSize, iterate, gradient, gB, vB, + updatePolicy.Update(f, stepSize, iterate, gradient, gB, vB, currentFunction, batchSize, effectiveBatchSize, reset); // Update the iterate. iterate -= stepSize * gradient; - overallObjective += function.Evaluate(iterate, currentFunction, + overallObjective += f.Evaluate(iterate, currentFunction, effectiveBatchSize); i += effectiveBatchSize; @@ -188,7 +197,7 @@ double BigBatchSGD::Optimize( for (size_t i = 0; i < numFunctions; i += batchSize) { const size_t effectiveBatchSize = std::min(batchSize, numFunctions - i); - overallObjective += function.Evaluate(iterate, i, effectiveBatchSize); + overallObjective += f.Evaluate(iterate, i, effectiveBatchSize); } return overallObjective; }