[mlpack-git] master: Add test for RMSprop. (b2d87c7)
gitdub at mlpack.org
gitdub at mlpack.org
Fri Feb 19 08:22:53 EST 2016
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/f6dd2f7a9752a7db8ec284a938b3e84a13d0bfb2...6205f3e0b62b56452b2a4afc4da24fce5b21e72f
>---------------------------------------------------------------
commit b2d87c715ccde9575247ec9e5f0a190061411555
Author: marcus <marcus.edel at fu-berlin.de>
Date: Tue Feb 16 21:55:23 2016 +0100
Add test for RMSprop.
>---------------------------------------------------------------
b2d87c715ccde9575247ec9e5f0a190061411555
src/mlpack/tests/rmsprop_test.cpp | 109 +++++++-------------------------------
1 file changed, 18 insertions(+), 91 deletions(-)
diff --git a/src/mlpack/tests/rmsprop_test.cpp b/src/mlpack/tests/rmsprop_test.cpp
index 13c4bdd..bb556dc 100644
--- a/src/mlpack/tests/rmsprop_test.cpp
+++ b/src/mlpack/tests/rmsprop_test.cpp
@@ -2,112 +2,39 @@
* @file rmsprop_test.cpp
* @author Marcus Edel
*
- * Tests the RMSProp optimizer on a couple test models.
+ * Tests the RMSProp optimizer.
*/
#include <mlpack/core.hpp>
-#include <mlpack/methods/ann/activation_functions/logistic_function.hpp>
-
-#include <mlpack/methods/ann/init_rules/random_init.hpp>
-
-#include <mlpack/methods/ann/layer/bias_layer.hpp>
-#include <mlpack/methods/ann/layer/linear_layer.hpp>
-#include <mlpack/methods/ann/layer/base_layer.hpp>
-#include <mlpack/methods/ann/layer/one_hot_layer.hpp>
-
-#include <mlpack/methods/ann/trainer/trainer.hpp>
-#include <mlpack/methods/ann/ffn.hpp>
-#include <mlpack/methods/ann/performance_functions/mse_function.hpp>
-#include <mlpack/methods/ann/optimizer/rmsprop.hpp>
+#include <mlpack/core/optimizers/rmsprop/rmsprop.hpp>
+#include <mlpack/core/optimizers/lbfgs/test_functions.hpp>
+#include <mlpack/core/optimizers/sgd/test_function.hpp>
#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"
+using namespace arma;
using namespace mlpack;
-using namespace mlpack::ann;
+using namespace mlpack::optimization;
+using namespace mlpack::optimization::test;
-BOOST_AUTO_TEST_SUITE(RMSPropTest);
+BOOST_AUTO_TEST_SUITE(RMSpropTest);
/**
- * Train and evaluate a vanilla network with the specified structure. Using the
- * iris data, the data set contains 3 classes. One class is linearly separable
- * from the other 2. The other two aren't linearly separable from each other.
+ * Tests the RMSprop optimizer using a simple test function.
*/
-BOOST_AUTO_TEST_CASE(SimpleRMSPropTestFunction)
+BOOST_AUTO_TEST_CASE(SimpleRMSpropTestFunction)
{
- const size_t hiddenLayerSize = 10;
- const size_t maxEpochs = 300;
-
- // Load the dataset.
- arma::mat dataset, labels, labelsIdx;
- data::Load("iris_train.csv", dataset, true);
- data::Load("iris_train_labels.csv", labelsIdx, true);
-
- // Create target matrix.
- labels = arma::zeros<arma::mat>(labelsIdx.max() + 1, labelsIdx.n_cols);
- for (size_t i = 0; i < labelsIdx.n_cols; i++)
- labels(labelsIdx(0, i), i) = 1;
-
- // Construct a feed forward network using the specified parameters.
- RandomInitialization randInit(0.1, 0.1);
-
- LinearLayer<RMSPROP, RandomInitialization> inputLayer(dataset.n_rows,
- hiddenLayerSize, randInit);
- BiasLayer<RMSPROP, RandomInitialization> inputBiasLayer(hiddenLayerSize,
- 1, randInit);
- BaseLayer<LogisticFunction> inputBaseLayer;
-
- LinearLayer<RMSPROP, RandomInitialization> hiddenLayer1(hiddenLayerSize,
- labels.n_rows, randInit);
- BiasLayer<RMSPROP, RandomInitialization> hiddenBiasLayer1(labels.n_rows,
- 1, randInit);
- BaseLayer<LogisticFunction> outputLayer;
-
- OneHotLayer classOutputLayer;
-
- auto modules = std::tie(inputLayer, inputBiasLayer, inputBaseLayer,
- hiddenLayer1, hiddenBiasLayer1, outputLayer);
-
- FFN<decltype(modules), OneHotLayer, MeanSquaredErrorFunction>
- net(modules, classOutputLayer);
-
- arma::mat prediction;
- size_t error = 0;
-
- // Evaluate the feed forward network.
- for (size_t i = 0; i < dataset.n_cols; i++)
- {
- arma::mat input = dataset.unsafe_col(i);
- net.Predict(input, prediction);
-
- if (arma::sum(arma::sum(arma::abs(
- prediction - labels.unsafe_col(i)))) == 0)
- error++;
- }
-
- // Check if the selected model isn't already optimized.
- double classificationError = 1 - double(error) / dataset.n_cols;
- BOOST_REQUIRE_GE(classificationError, 0.09);
-
- // Train the feed forward network.
- Trainer<decltype(net)> trainer(net, maxEpochs, 1, 0.01, false);
- trainer.Train(dataset, labels, dataset, labels);
-
- // Evaluate the feed forward network.
- error = 0;
- for (size_t i = 0; i < dataset.n_cols; i++)
- {
- arma::mat input = dataset.unsafe_col(i);
- net.Predict(input, prediction);
-
- if (arma::sum(arma::sum(arma::abs(
- prediction - labels.unsafe_col(i)))) == 0)
- error++;
- }
+ SGDTestFunction f;
+ RMSprop<SGDTestFunction> optimizer(f, 1e-3, 0.99, 1e-8, 5000000, 1e-9, true);
- classificationError = 1 - double(error) / dataset.n_cols;
+ arma::mat coordinates = f.GetInitialPoint();
+ double result = optimizer.Optimize(coordinates);
- BOOST_REQUIRE_LE(classificationError, 0.09);
+ BOOST_REQUIRE_CLOSE(result, -1.0, 0.05);
+ BOOST_REQUIRE_SMALL(coordinates[0], 1e-3);
+ BOOST_REQUIRE_SMALL(coordinates[1], 1e-3);
+ BOOST_REQUIRE_SMALL(coordinates[2], 1e-3);
}
BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-git
mailing list