From c60f3af3aabc252a5b576da73f28330b0bbc8309 Mon Sep 17 00:00:00 2001 From: Shikhar Bhardwaj Date: Mon, 24 Jul 2017 14:03:22 +0530 Subject: [PATCH] Use existing random number generator for std::shuffle --- src/mlpack/core/optimizers/parallel_sgd/parallel_sgd.hpp | 1 + .../core/optimizers/parallel_sgd/parallel_sgd_impl.hpp | 7 ++----- .../regularized_svd/regularized_svd_function_impl.hpp | 7 ++----- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd.hpp b/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd.hpp index 3975a2d69d..7e789b8473 100644 --- a/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd.hpp +++ b/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd.hpp @@ -13,6 +13,7 @@ #define MLPACK_CORE_OPTIMIZERS_PARALLEL_SGD_HPP #include +#include #include "decay_policies/constant_step.hpp" namespace mlpack { diff --git a/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd_impl.hpp b/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd_impl.hpp index 2081556bc1..97cc630942 100644 --- a/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd_impl.hpp +++ b/src/mlpack/core/optimizers/parallel_sgd/parallel_sgd_impl.hpp @@ -45,10 +45,6 @@ double ParallelSGD::Optimize( arma::Col visitationOrder = arma::linspace>(0, (function.NumFunctions() - 1), function.NumFunctions()); - // A random number generator instance to be used for shuffling the order of - // visitation. - std::mt19937 gen{ std::random_device()() }; - // Iterate till the objective is within tolerance or the maximum number of // allowed iterations is reached. If maxIterations is 0, this will iterate // till convergence. @@ -88,7 +84,8 @@ double ParallelSGD::Optimize( // Shuffle for uniform sampling of functions by each thread. if (shuffle) // Determine order of visitation. - std::shuffle(visitationOrder.begin(), visitationOrder.end(), gen); + std::shuffle(visitationOrder.begin(), visitationOrder.end(), + mlpack::math::randGen); #pragma omp parallel { diff --git a/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp b/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp index 2b790045fc..7a46959049 100644 --- a/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp +++ b/src/mlpack/methods/regularized_svd/regularized_svd_function_impl.hpp @@ -232,10 +232,6 @@ inline double ParallelSGD::Optimize( arma::Col visitationOrder = arma::linspace>(0, (function.NumFunctions() - 1), function.NumFunctions()); - // A random number generator instance to be used for shuffling the order of - // visitation. - std::mt19937 gen{ std::random_device()() }; - const arma::mat data = function.Dataset(); // Iterate till the objective is within tolerance or the maximum number of @@ -276,7 +272,8 @@ inline double ParallelSGD::Optimize( double stepSize = decayPolicy.StepSize(i); if (shuffle) // Determine order of visitation. - std::shuffle(visitationOrder.begin(), visitationOrder.end(), gen); + std::shuffle(visitationOrder.begin(), visitationOrder.end(), + mlpack::math::randGen); #pragma omp parallel {