[mlpack-git] master: Remove old unused trainer ann class; use mlpack optimizer instead. (cf2e18c)

gitdub at mlpack.org gitdub at mlpack.org
Fri Jul 8 18:51:09 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/aaefadd7cfd1f0f7d4ad0118d7e23cea76383397...cf2e18cfe61a475d6b9ca1a0676fa675c5fb70b6

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

commit cf2e18cfe61a475d6b9ca1a0676fa675c5fb70b6
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Sat Jul 9 00:50:43 2016 +0200

    Remove old unused trainer ann class; use mlpack optimizer instead.


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

cf2e18cfe61a475d6b9ca1a0676fa675c5fb70b6
 src/mlpack/methods/ann/CMakeLists.txt         |   1 -
 src/mlpack/methods/ann/trainer/CMakeLists.txt |  14 --
 src/mlpack/methods/ann/trainer/trainer.hpp    | 310 --------------------------
 3 files changed, 325 deletions(-)

diff --git a/src/mlpack/methods/ann/CMakeLists.txt b/src/mlpack/methods/ann/CMakeLists.txt
index c62459a..6ff7011 100644
--- a/src/mlpack/methods/ann/CMakeLists.txt
+++ b/src/mlpack/methods/ann/CMakeLists.txt
@@ -24,6 +24,5 @@ add_subdirectory(activation_functions)
 add_subdirectory(init_rules)
 add_subdirectory(layer)
 add_subdirectory(performance_functions)
-add_subdirectory(trainer)
 add_subdirectory(pooling_rules)
 add_subdirectory(convolution_rules)
diff --git a/src/mlpack/methods/ann/trainer/CMakeLists.txt b/src/mlpack/methods/ann/trainer/CMakeLists.txt
deleted file mode 100644
index 95c2192..0000000
--- a/src/mlpack/methods/ann/trainer/CMakeLists.txt
+++ /dev/null
@@ -1,14 +0,0 @@
-# Define the files we need to compile
-# Anything not in this list will not be compiled into mlpack.
-set(SOURCES
-  trainer.hpp
-)
-
-# Add directory name to sources.
-set(DIR_SRCS)
-foreach(file ${SOURCES})
-  set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
-endforeach()
-# Append sources (with directory name) to list of all mlpack sources (used at
-# the parent scope).
-set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
diff --git a/src/mlpack/methods/ann/trainer/trainer.hpp b/src/mlpack/methods/ann/trainer/trainer.hpp
deleted file mode 100644
index a42cd8a..0000000
--- a/src/mlpack/methods/ann/trainer/trainer.hpp
+++ /dev/null
@@ -1,310 +0,0 @@
-/**
- * @file trainer.hpp
- * @author Marcus Edel
- *
- * Definition and implementation of a trainer that trains the parameters of a
- * neural network according to a supervised dataset.
- */
-#ifndef MLPACK_METHODS_ANN_TRAINER_TRAINER_HPP
-#define MLPACK_METHODS_ANN_TRAINER_TRAINER_HPP
-
-#include <mlpack/core.hpp>
-
-#include <mlpack/methods/ann/network_traits.hpp>
-#include <mlpack/methods/ann/layer/layer_traits.hpp>
-
-namespace mlpack {
-namespace ann /** Artificial Neural Network. */ {
-
-/**
- * Trainer that trains the parameters of a neural network according to a
- * supervised dataset.
- *
- * @tparam NetworkType The type of network which should be trained and
- * evaluated.
- * @tparam MaType Type of the error type (arma::mat or arma::sp_mat).
- */
-template<
-  typename NetworkType,
-  typename MatType = arma::mat
->
-class Trainer
-{
-  public:
-    /**
-     * Construct the Trainer object, which will be used to train a neural
-     * network according to a supervised dataset by backpropagating the errors.
-     *
-     * If batchSize is greater 1 the trainer will take a mean gradient step over
-     * this many samples and will update the parameters only at the end of
-     * each epoch (Default 1).
-     *
-     * @param net The network that should be trained.
-     * @param maxEpochs The number of maximal trained iterations (0 means no
-     * limit).
-     * @param batchSize The batch size used to train the network.
-     * @param tolerance Train the network until it converges against
-     * the specified threshold.
-     * @param shuffle If true, the order of the training set is shuffled;
-     * otherwise, each data is visited in linear order.
-     */
-    Trainer(NetworkType& net,
-            const size_t maxEpochs = 0,
-            const size_t batchSize = 1,
-            const double tolerance = 0.0001,
-            const bool shuffle = true) :
-        net(net),
-        maxEpochs(maxEpochs),
-        batchSize(batchSize),
-        tolerance(tolerance),
-        shuffle(shuffle)
-    {
-      // Nothing to do here.
-    }
-
-    /**
-     * Train the network on the given datasets until the network converges. If
-     * maxEpochs is greater than zero that many epochs are maximal trained.
-     *
-     * @param trainingData Data used to train the network.
-     * @param trainingLabels Labels used to train the network.
-     * @param validationData Data used to evaluate the network.
-     * @tparam validationLabels Labels used to evaluate the network.
-     */
-    template<typename InputType, typename OutputType>
-    void Train(InputType& trainingData,
-               OutputType& trainingLabels,
-               InputType& validationData,
-               OutputType& validationLabels)
-    {
-      // This generates [0 1 2 3 ... (ElementCount(trainingData) - 1)]. The
-      // sequence will be used to iterate through the training data.
-      index = arma::linspace<arma::Col<size_t> >(0,
-          ElementCount(trainingData) - 1, ElementCount(trainingData));
-      epoch = 0;
-
-      while(true)
-      {
-        if (shuffle)
-          index = arma::shuffle(index);
-
-        Train<InputType, OutputType, NetworkType>(
-            trainingData, trainingLabels);
-        Evaluate<InputType, OutputType, NetworkType>(
-            validationData, validationLabels);
-
-        if (validationError <= tolerance)
-          break;
-
-        if (maxEpochs > 0 && ++epoch >= maxEpochs)
-          break;
-      }
-    }
-
-    //! Get the training error.
-    double TrainingError() const { return trainingError; }
-
-    //! Get the validation error.
-    double ValidationError() const { return validationError; }
-
-    //! Get whether or not the individual inputs are shuffled.
-    bool Shuffle() const { return shuffle; }
-    //! Modify whether or not the individual inputs are shuffled.
-    bool& Shuffle() { return shuffle; }
-
-    //! Get the batch size.
-    size_t StepSize() const { return batchSize; }
-    //! Modify the batch size.
-    size_t& StepSize() { return batchSize; }
-
-    //! Get the maximum number of iterations (0 indicates no limit).
-    size_t MaxEpochs() const { return maxEpochs; }
-    //! Modify the maximum number of iterations (0 indicates no limit).
-    size_t& MaxEpochs() { return maxEpochs; }
-
-    //! Get the tolerance for termination.
-    double Tolerance() const { return tolerance; }
-    //! Modify the tolerance for termination.
-    double& Tolerance() { return tolerance; }
-
-  private:
-    /**
-     * Train the network on the given dataset.
-     *
-     * @param data Data used to train the network.
-     * @param target Labels used to train the network.
-     */
-    template<typename InputType, typename OutputType, typename ModelType>
-    typename std::enable_if<!NetworkTraits<ModelType>::IsSAE, void>::type
-    Train(InputType& data, OutputType& target)
-    {
-      // Reset the training error.
-      trainingError = 0;
-
-      for (size_t i = 0; i < index.n_elem; i++)
-      {
-        net.FeedForward(Element(data, index(i)),
-            Element(target, index(i)), error);
-
-        trainingError += net.Error();
-        net.FeedBackward(Element(data, index(i)), error);
-
-        if (((i + 1) % batchSize) == 0)
-          net.ApplyGradients();
-      }
-
-      if ((index.n_elem % batchSize) != 0)
-        net.ApplyGradients();
-
-      trainingError /= index.n_elem;
-    }
-
-    /**
-     * Train the sparse autoencoder on the given dataset.
-     *
-     * @param data Data used to train the network.
-     */
-    template<typename InputType, typename OutputType, typename ModelType>
-    typename std::enable_if<NetworkTraits<ModelType>::IsSAE, void>::type
-    Train(InputType& data, OutputType& /* unused */)
-    {
-      // Reset the training error.
-      trainingError = 0;
-
-      arma::uvec indices(batchSize);
-
-      if (index.n_elem > batchSize)
-      {
-        for (size_t i = 0; i < index.n_elem; i += batchSize)
-        {
-          for (size_t j = 0; j < batchSize; j++)
-            indices(j) = index(j + i);
-
-          MatType input = data.rows(indices);
-          net.FeedForward(input, input, error);
-
-          trainingError += net.Error();
-          net.FeedBackward(input, error);
-          net.ApplyGradients();
-        }
-
-        trainingError /= (index.n_elem / batchSize);
-      }
-      else
-      {
-        net.FeedForward(data, data, error);
-        trainingError += net.Error();
-        net.FeedBackward(data, error);
-        net.ApplyGradients();
-      }
-    }
-
-    /**
-     * Evaluate the network on the given dataset.
-     *
-     * @param data Data used to train the network.
-     * @param target Labels used to train the network.
-     */
-    template<typename InputType, typename OutputType, typename ModelType>
-    typename std::enable_if<!NetworkTraits<ModelType>::IsSAE, void>::type
-    Evaluate(InputType& data, OutputType& target)
-    {
-      // Reset the validation error.
-      validationError = 0;
-
-      for (size_t i = 0; i < ElementCount(data); i++)
-      {
-         validationError += net.Evaluate(Element(data, i),
-            Element(target, i), error);
-      }
-
-      validationError /= ElementCount(data);
-    }
-
-    template<typename InputType, typename OutputType, typename ModelType>
-    typename std::enable_if<NetworkTraits<ModelType>::IsSAE, void>::type
-    Evaluate(InputType& /* unused */, OutputType& /* unused */) { /* Nothing to do here */ }
-
-    /*
-     * Create a Col object which uses memory from an existing matrix object.
-     * (This approach is currently not alias safe)
-     *
-     * @param data The reference data.
-     * @param sliceNum Provide a Col object of the specified index.
-     */
-    template<typename eT>
-    arma::Mat<eT> Element(arma::Mat<eT>& input, const size_t colNum)
-    {
-      return arma::Mat<eT>(input.colptr(colNum), input.n_rows, 1, false, true);
-    }
-
-    /*
-     * Provide the reference to the matrix representing a single slice.
-     *
-     * @param data The reference data.
-     * @param sliceNum Provide a single slice of the specified index.
-     */
-    template<typename eT>
-    const arma::Cube<eT> Element(arma::Cube<eT>& input, const size_t sliceNum)
-    {
-      return input.slices(sliceNum, sliceNum);
-    }
-
-    /*
-     * Get the number of elements.
-     *
-     * @param data The reference data.
-     */
-    template<typename eT>
-    size_t ElementCount(const arma::Mat<eT>& data) const
-    {
-      return data.n_cols;
-    }
-
-    /*
-     * Get the number of elements.
-     *
-     * @param data The reference data.
-     */
-    template<typename eT>
-    size_t ElementCount(const arma::Cube<eT>& data) const
-    {
-      return data.n_slices;
-    }
-
-    //! The network which should be trained and evaluated.
-    NetworkType& net;
-
-    //! The current network error of a single input.
-    MatType error;
-
-    //! The current epoch if maxEpochs is set.
-    size_t epoch;
-
-    //! The maximal epochs that should be used.
-    size_t maxEpochs;
-
-    //! The size until a update is performed.
-    size_t batchSize;
-
-    //! The shuffel sequence index used to train the network.
-    arma::Col<size_t> index;
-
-    //! The overall traing error.
-    double trainingError;
-
-    //! The overall validation error.
-    double validationError;
-
-    //! The tolerance for termination.
-    double tolerance;
-
-    //! Controls whether or not the individual inputs are shuffled when
-    //! iterating.
-    bool shuffle;
-}; // class Trainer
-
-} // namespace ann
-} // namespace mlpack
-
-#endif




More information about the mlpack-git mailing list