[mlpack-git] master: Add a test for RMSProp. (54f77d3)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Fri Oct 16 08:18:05 EDT 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/68157b9f819db2636ace8348840e8588e7bc7bc2...54f77d383ddb8546c6615d0c4aca29f18758ded2

>---------------------------------------------------------------

commit 54f77d383ddb8546c6615d0c4aca29f18758ded2
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Fri Oct 16 14:17:57 2015 +0200

    Add a test for RMSProp.


>---------------------------------------------------------------

54f77d383ddb8546c6615d0c4aca29f18758ded2
 src/mlpack/tests/CMakeLists.txt   |   1 +
 src/mlpack/tests/rmsprop_test.cpp | 113 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+)

diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index c4d2c4f..43e7dd0 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -49,6 +49,7 @@ add_executable(mlpack_test
   range_search_test.cpp
   rectangle_tree_test.cpp
   regularized_svd_test.cpp
+  rmsprop_test.cpp
   sa_test.cpp
   sdp_primal_dual_test.cpp
   sgd_test.cpp
diff --git a/src/mlpack/tests/rmsprop_test.cpp b/src/mlpack/tests/rmsprop_test.cpp
new file mode 100644
index 0000000..0ae76ba
--- /dev/null
+++ b/src/mlpack/tests/rmsprop_test.cpp
@@ -0,0 +1,113 @@
+/**
+ * @file rmsprop_test.cpp
+ * @author Marcus Edel
+ *
+ * Tests the RMSProp optimizer on a couple test models.
+ */
+#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 <boost/test/unit_test.hpp>
+#include "old_boost_test_definitions.hpp"
+
+using namespace mlpack;
+using namespace mlpack::ann;
+
+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.
+ */
+BOOST_AUTO_TEST_CASE(SimpleRMSPropTestFunction)
+{
+  const size_t hiddenLayerSize = 10;
+  const size_t maxEpochs = 100;
+
+  // 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.5, 0.5);
+
+  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.05);
+
+  // Train the feed forward network.
+  Trainer<decltype(net)> trainer(net, maxEpochs, 1, 0.01);
+  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++;
+  }
+
+  classificationError = 1 - double(error) / dataset.n_cols;
+
+  BOOST_REQUIRE_LE(classificationError, 0.05);
+}
+
+BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-git mailing list