[mlpack] 22/22: Change Sigmoid() function to avoid matrix copies via the return value.

Barak A. Pearlmutter barak+git at cs.nuim.ie
Thu Apr 17 12:23:03 UTC 2014


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch svn-trunk
in repository mlpack.

commit c80e63ecc072608b2b9594482c93d7ca46a3da32
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Wed Apr 16 19:36:06 2014 +0000

    Change Sigmoid() function to avoid matrix copies via the return value.
    
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@16443 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 .../sparse_autoencoder/sparse_autoencoder.hpp      |  4 ++--
 .../sparse_autoencoder_function.cpp                | 22 ++++++++++++----------
 .../sparse_autoencoder_function.hpp                |  4 ++--
 .../sparse_autoencoder/sparse_autoencoder_impl.hpp |  5 +++--
 4 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp
index 2baaf94..1c1f896 100644
--- a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp
+++ b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp
@@ -110,9 +110,9 @@ class SparseAutoencoder
    *
    * @param x Matrix of real values for which we require the sigmoid activation.
    */
-  arma::mat Sigmoid(const arma::mat& x) const
+  void Sigmoid(const arma::mat& x, arma::mat& output) const
   {
-    return (1.0 / (1 + arma::exp(-x)));
+    output = (1.0 / (1 + arma::exp(-x)));
   }
 
   //! Sets size of the visible layer.
diff --git a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.cpp b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.cpp
index b7ad71f..09505ae 100644
--- a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.cpp
+++ b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.cpp
@@ -93,12 +93,13 @@ double SparseAutoencoderFunction::Evaluate(const arma::mat& parameters) const
   arma::mat hiddenLayer, outputLayer;
 
   // Compute activations of the hidden and output layers.
-  hiddenLayer = Sigmoid(parameters.submat(0, 0, l1 - 1, l2 - 1) * data +
-      arma::repmat(parameters.submat(0, l2, l1 - 1, l2), 1, data.n_cols));
+  Sigmoid(parameters.submat(0, 0, l1 - 1, l2 - 1) * data +
+      arma::repmat(parameters.submat(0, l2, l1 - 1, l2), 1, data.n_cols),
+      hiddenLayer);
 
-  outputLayer = Sigmoid(
-      parameters.submat(l1, 0, l3 - 1, l2 - 1).t() * hiddenLayer +
-      arma::repmat(parameters.submat(l3, 0, l3, l2 - 1).t(), 1, data.n_cols));
+  Sigmoid(parameters.submat(l1, 0, l3 - 1, l2 - 1).t() * hiddenLayer +
+      arma::repmat(parameters.submat(l3, 0, l3, l2 - 1).t(), 1, data.n_cols),
+      outputLayer);
 
   arma::mat rhoCap, diff;
 
@@ -159,12 +160,13 @@ void SparseAutoencoderFunction::Gradient(const arma::mat& parameters,
   arma::mat hiddenLayer, outputLayer;
 
   // Compute activations of the hidden and output layers.
-  hiddenLayer = Sigmoid(parameters.submat(0, 0, l1 - 1, l2 - 1) * data +
-      arma::repmat(parameters.submat(0, l2, l1 - 1, l2), 1, data.n_cols));
+  Sigmoid(parameters.submat(0, 0, l1 - 1, l2 - 1) * data +
+      arma::repmat(parameters.submat(0, l2, l1 - 1, l2), 1, data.n_cols),
+      hiddenLayer);
 
-  outputLayer = Sigmoid(
-      parameters.submat(l1, 0, l3 - 1, l2 - 1).t() * hiddenLayer +
-      arma::repmat(parameters.submat(l3, 0, l3, l2 - 1).t(), 1, data.n_cols));
+  Sigmoid(parameters.submat(l1, 0, l3 - 1, l2 - 1).t() * hiddenLayer +
+      arma::repmat(parameters.submat(l3, 0, l3, l2 - 1).t(), 1, data.n_cols),
+      outputLayer);
 
   arma::mat rhoCap, diff;
 
diff --git a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.hpp b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.hpp
index 7a9b0f4..331e346 100644
--- a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.hpp
+++ b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.hpp
@@ -71,9 +71,9 @@ class SparseAutoencoderFunction
    *
    * @param x Matrix of real values for which we require the sigmoid activation.
    */
-  arma::mat Sigmoid(const arma::mat& x) const
+  void Sigmoid(const arma::mat& x, arma::mat& output) const
   {
-    return (1.0 / (1 + arma::exp(-x)));
+    output = (1.0 / (1 + arma::exp(-x)));
   }
 
   //! Return the initial point for the optimization.
diff --git a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp
index 16115da..1d786c4 100644
--- a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp
+++ b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp
@@ -66,8 +66,9 @@ void SparseAutoencoder<OptimizerType>::GetNewFeatures(arma::mat& data,
   const size_t l1 = hiddenSize;
   const size_t l2 = visibleSize;
 
-  features = Sigmoid(parameters.submat(0, 0, l1 - 1, l2 - 1) * data +
-      arma::repmat(parameters.submat(0, l2, l1 - 1, l2), 1, data.n_cols));
+  Sigmoid(parameters.submat(0, 0, l1 - 1, l2 - 1) * data +
+      arma::repmat(parameters.submat(0, l2, l1 - 1, l2), 1, data.n_cols),
+      features);
 }
 
 }; // namespace nn

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list