[mlpack-git] master: first commit (a484241)

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


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

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

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

commit a484241158f708cca4a4c4f5642a96e1671171ba
Author: stereomatchingkiss <stereomatchingkiss at gmail.com>
Date:   Tue Oct 27 17:19:44 2015 +0800

    first commit


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

a484241158f708cca4a4c4f5642a96e1671171ba
 .../methods/sparse_autoencoder/CMakeLists.txt      |  2 +
 .../methods/sparse_autoencoder/maximal_inputs.cpp  | 83 ++++++++++++++++++++++
 .../methods/sparse_autoencoder/maximal_inputs.hpp  | 20 ++++++
 3 files changed, 105 insertions(+)

diff --git a/src/mlpack/methods/sparse_autoencoder/CMakeLists.txt b/src/mlpack/methods/sparse_autoencoder/CMakeLists.txt
index b47e851..c14a5f2 100644
--- a/src/mlpack/methods/sparse_autoencoder/CMakeLists.txt
+++ b/src/mlpack/methods/sparse_autoencoder/CMakeLists.txt
@@ -5,6 +5,8 @@ set(SOURCES
   sparse_autoencoder_impl.hpp
   sparse_autoencoder_function.hpp
   sparse_autoencoder_function.cpp
+  maximal_inputs.hpp
+  maximal_inputs.cpp
 )
 
 # Add directory name to sources.
diff --git a/src/mlpack/methods/sparse_autoencoder/maximal_inputs.cpp b/src/mlpack/methods/sparse_autoencoder/maximal_inputs.cpp
new file mode 100644
index 0000000..f6e19eb
--- /dev/null
+++ b/src/mlpack/methods/sparse_autoencoder/maximal_inputs.cpp
@@ -0,0 +1,83 @@
+#include "maximal_inputs.hpp"
+
+namespace mlpack {
+namespace nn {
+
+namespace {
+
+void VisualizeHiddenUnit(size_t rows, size_t cols,
+                         int squareRows,
+                         int offset,
+                         arma::mat const &input,
+                         arma::mat &output)
+{
+  int k = 0;
+  for(int i = 0; i != cols; ++i)
+  {
+    for(int j = 0; j != rows; ++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;
+      ++k;
+    }
+  }
+}
+
+}
+
+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);
+  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))
+    {
+      ++cols;
+    }
+    rows = (int)std::ceil(paramTemp.n_rows/cols);
+  }else
+  {
+    cols = (int)std::sqrt(paramTemp.n_rows);
+    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);
+
+  double const max = output.max();
+  double const min = output.min();
+  if((max - min) != 0)
+  {
+    output = (output - min) / (max - min) * 255;
+  }
+}
+
+} // namespace nn
+} // namespace mlpack
diff --git a/src/mlpack/methods/sparse_autoencoder/maximal_inputs.hpp b/src/mlpack/methods/sparse_autoencoder/maximal_inputs.hpp
new file mode 100644
index 0000000..87a6e8f
--- /dev/null
+++ b/src/mlpack/methods/sparse_autoencoder/maximal_inputs.hpp
@@ -0,0 +1,20 @@
+#ifndef __MLPACK_METHODS_NN_MAXIMAL_INPUTS_HPP
+#define __MLPACK_METHODS_NN_MAXIMAL_INPUTS_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace nn {
+
+
+/**
+ * Maximize the hidden units of the parameters
+ * @param params The parameters want to maximize
+ * @param output Parameters after maximize
+ */
+void MaximalInputs(arma::mat const &parameters, arma::mat &output);
+
+} // namespace nn
+} // namespace mlpack
+
+#endif



More information about the mlpack-git mailing list