From f34ae33e2ccdaca68dc796a8384d409b586dd183 Mon Sep 17 00:00:00 2001 From: marcus Date: Sun, 31 Jan 2016 23:35:00 +0100 Subject: [PATCH] Remove unnecessary element function for the convolutional network and add sparse autoencoder training routine that makes use of the batch size. --- src/mlpack/methods/ann/trainer/trainer.hpp | 79 +++++++++++++++------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/src/mlpack/methods/ann/trainer/trainer.hpp b/src/mlpack/methods/ann/trainer/trainer.hpp index 1cc0b72c4e..b951d01e47 100644 --- a/src/mlpack/methods/ann/trainer/trainer.hpp +++ b/src/mlpack/methods/ann/trainer/trainer.hpp @@ -88,8 +88,10 @@ class Trainer if (shuffle) index = arma::shuffle(index); - Train(trainingData, trainingLabels); - Evaluate(validationData, validationLabels); + Train( + trainingData, trainingLabels); + Evaluate( + validationData, validationLabels); if (validationError <= tolerance) break; @@ -132,8 +134,9 @@ class Trainer * @param data Data used to train the network. * @param target Labels used to train the network. */ - template - void Train(InputType& data, OutputType& target) + template + typename std::enable_if::IsSAE, void>::type + Train(InputType& data, OutputType& target) { // Reset the training error. trainingError = 0; @@ -156,14 +159,55 @@ class Trainer trainingError /= index.n_elem; } + /** + * Train the sparse autoencoder on the given dataset. + * + * @param data Data used to train the network. + */ + template + typename std::enable_if::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 - void Evaluate(InputType& data, OutputType& target) + template + typename std::enable_if::IsSAE, void>::type + Evaluate(InputType& data, OutputType& target) { // Reset the validation error. validationError = 0; @@ -177,6 +221,10 @@ class Trainer validationError /= ElementCount(data); } + template + typename std::enable_if::IsSAE, void>::type + Evaluate(InputType& data, OutputType& target) { /* Nothing to do here */ } + /* * Create a Col object which uses memory from an existing matrix object. * (This approach is currently not alias safe) @@ -185,24 +233,7 @@ class Trainer * @param sliceNum Provide a Col object of the specified index. */ template - typename std::enable_if::IsCNN, - arma::Mat >::type - Element(arma::Mat& input, const size_t colNum) - { - return arma::Mat(input.colptr(colNum), input.n_rows, 1, false, true); - } - - /* - * Create a Mat 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 Mat object of the specified index. - */ - template - typename std::enable_if::IsCNN, - arma::Mat >::type - Element(arma::Mat& input, const size_t colNum) + arma::Mat Element(arma::Mat& input, const size_t colNum) { return arma::Mat(input.colptr(colNum), input.n_rows, 1, false, true); }