[mlpack-git] master, mlpack-1.0.x: Change Sigmoid() function to avoid matrix copies via the return value. (848fdcf)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:46:53 EST 2015
Repository : https://github.com/mlpack/mlpack
On branches: master,mlpack-1.0.x
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 848fdcf93bda07a243d29e7a1ac6cb3a97a5bfcc
Author: Ryan Curtin <ryan at ratml.org>
Date: Wed Apr 16 19:36:06 2014 +0000
Change Sigmoid() function to avoid matrix copies via the return value.
>---------------------------------------------------------------
848fdcf93bda07a243d29e7a1ac6cb3a97a5bfcc
.../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
More information about the mlpack-git
mailing list