[mlpack-git] master: 1 : Transpose the input 2 : Reduce copy (779437e)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Mon Nov 30 10:40:07 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/5aaf0e441dd64a5de9a0210aa7a837eecf162d12...e4519fc42a2a340cf0387ab082bf49b9715c871b

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

commit 779437efcb2cd7cb944165b0f22eb6a472116ab4
Author: stereomatchingkiss <stereomatchingkiss at gmail.com>
Date:   Wed Oct 28 15:20:48 2015 +0800

    1 : Transpose the input
    2 : Reduce copy


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

779437efcb2cd7cb944165b0f22eb6a472116ab4
 .../methods/sparse_autoencoder/maximal_inputs.cpp  | 79 ++++++++++------------
 1 file changed, 36 insertions(+), 43 deletions(-)

diff --git a/src/mlpack/methods/sparse_autoencoder/maximal_inputs.cpp b/src/mlpack/methods/sparse_autoencoder/maximal_inputs.cpp
index f6e19eb..b3217e4 100644
--- a/src/mlpack/methods/sparse_autoencoder/maximal_inputs.cpp
+++ b/src/mlpack/methods/sparse_autoencoder/maximal_inputs.cpp
@@ -6,33 +6,37 @@ namespace nn {
 namespace {
 
 void VisualizeHiddenUnit(size_t rows, size_t cols,
-                         int squareRows,
-                         int offset,
                          arma::mat const &input,
                          arma::mat &output)
 {
+  int const squareRows = static_cast<int>(std::sqrt(input.n_rows));
+  int const buf = 1;
+
+  int const offset = squareRows+buf;
+  output.ones(buf+rows*(offset),
+              buf+cols*(offset));
+
   int k = 0;
-  for(int i = 0; i != cols; ++i)
-  {
-    for(int j = 0; j != rows; ++j)
-    {
-      if(k >= input.n_cols)
-      {
+  for(int i = 0; i != rows; ++i) {
+    for(int j = 0; j != cols; ++j) {
+      if(k >= input.n_cols) {
         continue;
       }
-      arma::mat reshapeMat(squareRows, squareRows);
-      arma::mat const weights = input.row(k);
-      std::copy(std::begin(weights),
-                std::end(weights),
-                std::begin(reshapeMat));
-      double const max = arma::abs(input.row(k)).max();
-      if(max != 0.0)
-      {
-        reshapeMat /= max;
-      }
-      output.submat(i*(offset), j*(offset),
-                    i*(offset) + squareRows - 1,
-                    j*(offset) + squareRows - 1) = reshapeMat;
+      // Find the maximum element in this row.
+      const double max  = arma::max(arma::abs(input.col(k)));
+      // Now, copy the elements of the row to the output submatrix.
+      const arma::uword minRow = i * offset;
+      const arma::uword minCol = j * offset;
+      const arma::uword maxRow = i * offset + squareRows - 1;
+      const arma::uword maxCol = j * offset + squareRows - 1;
+      // Only divide by the max if it's not 0.
+      if (max != 0.0)
+        output.submat(minRow, minCol, maxRow, maxCol) =
+          arma::reshape(input.col(k), squareRows, squareRows) / max;
+      else
+        output.submat(minRow, minCol, maxRow, maxCol) =
+          arma::reshape(input.col(k), squareRows, squareRows);
+
       ++k;
     }
   }
@@ -42,39 +46,28 @@ void VisualizeHiddenUnit(size_t rows, size_t cols,
 
 void MaximalInputs(arma::mat const &parameters, arma::mat &output)
 {
-  //take the encoder part of the paramters
-  arma::mat paramTemp = parameters.submat(0, 0, (parameters.n_rows-1)/2-1, parameters.n_cols-2);
+  arma::mat paramTemp(parameters.t());
   double const mean = arma::mean(arma::mean(paramTemp));
   paramTemp -= mean;
 
-  int rows = 0, cols = (int)std::ceil(std::sqrt(paramTemp.n_rows));
-  if(std::pow(std::floor(std::sqrt(paramTemp.n_rows)), 2) != paramTemp.n_rows)
-  {
-    while(paramTemp.n_rows % cols != 0 && cols < 1.2*std::sqrt(paramTemp.n_rows))
-    {
+  int rows = 0, cols = 0;
+  if(std::pow(std::floor(std::sqrt(paramTemp.n_cols)), 2) != paramTemp.n_cols) {
+    cols = (int)std::ceil(std::sqrt(paramTemp.n_cols));
+    while(paramTemp.n_cols % cols != 0 && cols < 1.2*std::sqrt(paramTemp.n_cols)) {
       ++cols;
     }
-    rows = (int)std::ceil(paramTemp.n_rows/cols);
-  }else
-  {
-    cols = (int)std::sqrt(paramTemp.n_rows);
+    rows = static_cast<int>
+           (std::ceil(paramTemp.n_cols/static_cast<double>(cols)));
+  }else{
+    cols = static_cast<int>(std::sqrt(paramTemp.n_cols));
     rows = cols;
   }
 
-  int const squareRows = (int)std::sqrt(paramTemp.n_cols);
-  int const buf = 1;
-
-  int const offset = squareRows+buf;
-  output.ones(buf+rows*(offset),
-              buf+cols*(offset));
-
-  VisualizeHiddenUnit(rows, cols, squareRows,
-                      offset, paramTemp, output);
+  VisualizeHiddenUnit(rows, cols, paramTemp, output);
 
   double const max = output.max();
   double const min = output.min();
-  if((max - min) != 0)
-  {
+  if((max - min) != 0) {
     output = (output - min) / (max - min) * 255;
   }
 }



More information about the mlpack-git mailing list