Change Sigmoid() function to avoid matrix copies via the return value.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user