From a4bfb4f09689b2a7fb3bd091ea8d13935de08210 Mon Sep 17 00:00:00 2001 From: Shikhar Bhardwaj Date: Wed, 16 Aug 2017 00:43:32 +0530 Subject: [PATCH] Update documentation with the changes. Unify changes with test functions --- doc/policies/functiontype.hpp | 8 +++++--- .../core/optimizers/parallel_sgd/sparse_test_function.hpp | 6 +++--- src/mlpack/core/optimizers/scd/scd_impl.hpp | 2 +- src/mlpack/tests/scd_test.cpp | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/doc/policies/functiontype.hpp b/doc/policies/functiontype.hpp index a0139f4c1d..81e0545a93 100644 --- a/doc/policies/functiontype.hpp +++ b/doc/policies/functiontype.hpp @@ -82,10 +82,12 @@ To evaluate the loss function at the given coordinates, same as the \c FunctionType interface. @code -void FeatureGradient(const arma::mat& coordinates, const size_t j, double& gradient); +void FeatureGradient(const arma::mat& coordinates, const size_t j, arma::sp_mat& gradient); @endcode To evaluate the gradient at the given coordinates, where \c gradient is an -out-param for the required gradient. The out-param is a scalar value, for -storing the gradient of the jth feature. +out-param for the required gradient. The out-param is a sparse matrix(with +dimensions equal to the decision variable), for storing the gradient of the +jth feature. The \c gradient matrix is supposed to be non-zero in the jth +column, which contains the relavant partial gradient. */ diff --git a/src/mlpack/core/optimizers/parallel_sgd/sparse_test_function.hpp b/src/mlpack/core/optimizers/parallel_sgd/sparse_test_function.hpp index e76d55eff5..5f8b7ad04b 100644 --- a/src/mlpack/core/optimizers/parallel_sgd/sparse_test_function.hpp +++ b/src/mlpack/core/optimizers/parallel_sgd/sparse_test_function.hpp @@ -67,7 +67,7 @@ class SparseTestFunction const size_t i, arma::sp_mat& gradient) const { - gradient = arma::sp_mat(1, coordinates.n_cols); + gradient.zeros(coordinates.size()); gradient[i] = 2 * coordinates[i] + bi[i]; } @@ -76,8 +76,8 @@ class SparseTestFunction const size_t j, arma::sp_mat& gradient) const { - gradient.set_size(1); - gradient[0] = 2 * coordinates[j] + bi[j]; + gradient.zeros(coordinates.size()); + gradient[j] = 2 * coordinates[j] + bi[j]; } private: diff --git a/src/mlpack/core/optimizers/scd/scd_impl.hpp b/src/mlpack/core/optimizers/scd/scd_impl.hpp index e5b046231d..472a99fc74 100644 --- a/src/mlpack/core/optimizers/scd/scd_impl.hpp +++ b/src/mlpack/core/optimizers/scd/scd_impl.hpp @@ -53,7 +53,7 @@ double SCD::Optimize(ResolvableFunctionType& function, function.FeatureGradient(iterate, featureIdx, gradient); // Update the decision variable with the partial gradient. - iterate.col(featureIdx) -= stepSize * gradient; + iterate -= stepSize * gradient; // Check for convergence. if (i % updateInterval == 0) diff --git a/src/mlpack/tests/scd_test.cpp b/src/mlpack/tests/scd_test.cpp index 8af39753ca..f1fbb92757 100644 --- a/src/mlpack/tests/scd_test.cpp +++ b/src/mlpack/tests/scd_test.cpp @@ -20,8 +20,8 @@ #include "test_tools.hpp" using namespace std; -using namespace arma; using namespace mlpack; +using namespace mlpack::math; using namespace mlpack::optimization; using namespace mlpack::optimization::test; using namespace mlpack::regression; @@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(SoftmaxRegressionFeatureGradientTest) // Create random class labels. arma::Row labels(points); for (size_t i = 0; i < points; i++) - labels(i) = math::RandInt(0, numClasses); + labels(i) = RandInt(0, numClasses); // 2 objects for 2 terms in the cost function. Each term contributes towards // the gradient and thus need to be checked independently.