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

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


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

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

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

commit e0f22256d2e1ce47b3e30c0fa36d6609f77fe430
Author: stereomatchingkiss <stereomatchingkiss at gmail.com>
Date:   Thu Nov 12 15:56:57 2015 +0800

    first commit


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

e0f22256d2e1ce47b3e30c0fa36d6609f77fe430
 src/mlpack/core/math/CMakeLists.txt        |   2 +
 src/mlpack/core/math/columns_to_blocks.cpp |  71 ++++++++++++
 src/mlpack/core/math/columns_to_blocks.hpp | 167 +++++++++++++++++++++++++++++
 3 files changed, 240 insertions(+)

diff --git a/src/mlpack/core/math/CMakeLists.txt b/src/mlpack/core/math/CMakeLists.txt
index 88e294d..f6521cc 100644
--- a/src/mlpack/core/math/CMakeLists.txt
+++ b/src/mlpack/core/math/CMakeLists.txt
@@ -2,6 +2,8 @@
 # Anything not in this list will not be compiled into MLPACK.
 set(SOURCES
   clamp.hpp
+  columns_to_blocks.hpp
+  columns_to_blocks.cpp
   lin_alg.hpp
   lin_alg_impl.hpp
   lin_alg.cpp
diff --git a/src/mlpack/core/math/columns_to_blocks.cpp b/src/mlpack/core/math/columns_to_blocks.cpp
new file mode 100644
index 0000000..9a188f9
--- /dev/null
+++ b/src/mlpack/core/math/columns_to_blocks.cpp
@@ -0,0 +1,71 @@
+#include "columns_to_blocks.hpp"
+
+namespace mlpack {
+namespace math {
+
+ColumnsToBlocks::ColumnsToBlocks(arma::uword rows, arma::uword cols) :
+    minRange(0),
+    maxRange(255),
+    scale(false),
+    rows(rows),
+    cols(cols)
+{
+}
+
+bool ColumnsToBlocks::IsPerfectSquare(arma::uword value) const
+{
+  if (value < 0)
+  {
+    return false;
+  }
+
+  const int root = std::round(std::sqrt(value));
+  return value == root * root;
+}
+
+void ColumnsToBlocks::Transform(const arma::mat &maximalInputs, arma::mat &output)
+{
+  if(!IsPerfectSquare(maximalInputs.n_rows))
+  {
+    throw std::runtime_error("maximalInputs.n_rows should be perfect square");
+  }
+
+  arma::uword const squareRows = static_cast<arma::uword>(std::sqrt(maximalInputs.n_rows));
+  arma::uword const buf = 1;
+
+  arma::uword const offset = squareRows+buf;
+  output.ones(buf+ rows*(offset),
+              buf+ cols*(offset));
+  output *= -1;
+
+  arma::uword k = 0;
+  const arma::uword maxSize = rows * cols;
+  for(arma::uword i = 0; i != rows; ++i) {
+    for(arma::uword j = 0; j != cols; ++j) {
+      // Now, copy the elements of the row to the output submatrix.
+      const arma::uword minRow = buf + i * offset;
+      const arma::uword minCol = buf + j * offset;
+      const arma::uword maxRow = i * offset + squareRows;
+      const arma::uword maxCol = j * offset + squareRows;
+
+      output.submat(minRow, minCol, maxRow, maxCol) =
+        arma::reshape(maximalInputs.col(k++), squareRows, squareRows);
+      if(k >= maxSize) {
+        break;
+      }
+    }
+  }
+
+  if(scale)
+  {
+    const double max = output.max();
+    const double min = output.min();
+    if((max - min) != 0)
+    {
+      output = (output - min) / (max - min) * (maxRange - minRange) + minRange;
+    }
+  }
+}
+
+} // namespace math
+} // namespace mlpack
diff --git a/src/mlpack/core/math/columns_to_blocks.hpp b/src/mlpack/core/math/columns_to_blocks.hpp
new file mode 100644
index 0000000..4ec3286
--- /dev/null
+++ b/src/mlpack/core/math/columns_to_blocks.hpp
@@ -0,0 +1,167 @@
+#ifndef __MLPACK_METHODS_NN_COLUMNS_TO_BLOCKS_HPP
+#define __MLPACK_METHODS_NN_COLUMNS_TO_BLOCKS_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace math {
+
+/**
+ * Transform the output of "MaximalInputs" to blocks, if your training samples are images,
+ * this function could help you visualize your training results
+ * @param maximalInputs Parameters after maximize by "MaximalInputs", each col assiociate to one sample
+ * @param output Maximal inputs regrouped to blocks
+ * @param scale False, the output will not be scaled and vice versa
+ * @param minRange minimum range of the output
+ * @param maxRange maximum range of the output
+ * @code
+ * arma::mat maximalInput; //store the features learned by sparse autoencoder
+ * mlpack::nn::MaximalInputs(encoder2.Parameters(), maximalInput);
+ *
+ * arma::mat outputs;
+ * const bool scale = true;
+ *
+ * ColumnsToBlocks ctb(5,5);
+ * arma::mat output;
+ * ctb.Transform(maximalInput, output);
+ * //you can save the output as a pgm, this may help you visualize the training results
+ * output.save(fileName, arma::pgm_binary);
+ * @endcode
+ */
+class ColumnsToBlocks
+{
+ public:
+  /**
+   * Constructor of ColumnsToBlocks
+   * @param rows number of blocks per cols
+   * @param cols number of blocks per rows
+   */
+  ColumnsToBlocks(arma::uword rows, arma::uword cols);
+
+  /**
+   * Transform the columns of maximalInputs into blocks
+   * @param maximalInputs input value intent to transform to block, the rows
+   * of this input must be a perfect square
+   * @param output input transformed to block
+   */
+  void Transform(const arma::mat &maximalInputs, arma::mat &output);
+
+  /**
+   * Get the size of the buffer, this size determine the how many cells surround
+   * each column of the maximalInputs.
+   * @param value buffer size, default value is 1
+   */
+  void BufSize(arma::uword value) {
+    bufSize = value;
+  }
+
+  /**
+   * Return buffer size
+   */
+  arma::uword BufSize() const {
+    return bufSize;
+  }
+
+  /**
+   * Set the buffer value surround the cells
+   * @param value The value of the buffer, default value is -1
+   */
+  void BufValue(double value) {
+    bufValue = value;
+  }
+  double BufValue() const {
+    return bufValue;
+  }
+
+  /**
+   * set number of blocks per rows
+   * @param value number of blocks per rows, default value is 0
+   */
+  void Cols(arma::uword value) {
+    cols = value;
+  }
+
+  /**
+   * Return number of blocks per rows
+   */
+  arma::uword Cols() const {
+    return cols;
+  }
+
+  /**
+   * Set maximum range for scaling
+   * @param value maximum range, default value is 255
+   */
+  void MaxRange(double value) {
+    maxRange = value;
+  }
+
+  /**
+   * Return maximum range
+   */
+  double MaxRange() const {
+    return maxRange;
+  }
+
+  /**
+   * Set minimum range for scaling
+   * @param value minimum range, default value is 0
+   */
+  void MinRange(double value) {
+    minRange = value;
+  }
+
+  /**
+   * Return minimum range
+   */
+  double MinRange() const {
+    return minRange;
+  }
+
+  /**
+   * @brief Set number of blocks per rows
+   * @param cols number of blocks per rows, default value is 0
+   */
+  void Rows(arma::uword value) {
+    rows = value;
+  }
+
+  /**
+   * Return number of blocks per rows
+   */
+  arma::uword Rows() const {
+    return rows;
+  }
+
+  /**
+   * Disable or enable scale
+   * @param value True, scale the output range and vice versa.Default
+   * value is false
+   */
+  void Scale(bool value) {
+    scale = value;
+  }
+
+  /**
+   * Return scale value
+   */
+  bool Scale() const {
+    return scale;
+  }
+
+ private:
+  bool IsPerfectSquare(arma::uword value) const;
+
+  arma::uword bufSize;
+  double bufValue;
+  arma::uword cols;
+  double maxRange;
+  double minRange;
+  bool scale;
+  arma::uword rows;
+};
+
+} // namespace math
+} // namespace mlpack
+
+#endif



More information about the mlpack-git mailing list