[mlpack-git] master: able to set block width and block height (8ad13ee)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Mon Nov 30 10:40:36 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/5aaf0e441dd64a5de9a0210aa7a837eecf162d12...e4519fc42a2a340cf0387ab082bf49b9715c871b
>---------------------------------------------------------------
commit 8ad13ee340a1b0678f94a1cee278ff9b2b4fa08d
Author: stereomatchingkiss <stereomatchingkiss at gmail.com>
Date: Thu Nov 26 18:43:49 2015 +0800
able to set block width and block height
>---------------------------------------------------------------
8ad13ee340a1b0678f94a1cee278ff9b2b4fa08d
src/mlpack/core/math/columns_to_blocks.cpp | 53 ++++++++++++------
src/mlpack/core/math/columns_to_blocks.hpp | 87 ++++++++++++++++++++++++++----
2 files changed, 112 insertions(+), 28 deletions(-)
diff --git a/src/mlpack/core/math/columns_to_blocks.cpp b/src/mlpack/core/math/columns_to_blocks.cpp
index 9a188f9..fa5e0bb 100644
--- a/src/mlpack/core/math/columns_to_blocks.cpp
+++ b/src/mlpack/core/math/columns_to_blocks.cpp
@@ -3,7 +3,13 @@
namespace mlpack {
namespace math {
-ColumnsToBlocks::ColumnsToBlocks(arma::uword rows, arma::uword cols) :
+ColumnsToBlocks::ColumnsToBlocks(size_t rows, size_t cols,
+ size_t blockHeight,
+ size_t blockWidth) :
+ blockHeight(blockHeight),
+ blockWidth(blockWidth),
+ bufSize(1),
+ bufValue(-1),
minRange(0),
maxRange(255),
scale(false),
@@ -12,7 +18,7 @@ ColumnsToBlocks::ColumnsToBlocks(arma::uword rows, arma::uword cols) :
{
}
-bool ColumnsToBlocks::IsPerfectSquare(arma::uword value) const
+bool ColumnsToBlocks::IsPerfectSquare(size_t value) const
{
if (value < 0)
{
@@ -30,26 +36,39 @@ void ColumnsToBlocks::Transform(const arma::mat &maximalInputs, arma::mat &outpu
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;
+ if(blockHeight == 0 || blockWidth == 0)
+ {
+ size_t const squareRows = static_cast<size_t>(std::sqrt(maximalInputs.n_rows));
+ blockHeight = squareRows;
+ blockWidth = squareRows;
+ }
+ if(blockHeight * blockWidth != maximalInputs.n_rows)
+ {
+ throw std::runtime_error("blockHeight * blockWidth should "
+ "equal to maximalInputs.n_rows");
+ }
- arma::uword const offset = squareRows+buf;
- output.ones(buf+ rows*(offset),
- buf+ cols*(offset));
- output *= -1;
+ const size_t rowOffset = blockHeight+bufSize;
+ const size_t colOffset = blockWidth+bufSize;
+ output.ones(bufSize+rows*rowOffset,
+ bufSize+cols*colOffset);
+ output *= bufValue;
- 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) {
+ size_t k = 0;
+ const size_t maxSize = std::min(rows * cols, maximalInputs.n_cols);
+ for(size_t i = 0; i != rows; ++i)
+ {
+ for(size_t 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;
+ const size_t minRow = bufSize + i * rowOffset;
+ const size_t minCol = bufSize + j * colOffset;
+ const size_t maxRow = i * rowOffset + blockHeight;
+ const size_t maxCol = j * colOffset + blockWidth;
output.submat(minRow, minCol, maxRow, maxCol) =
- arma::reshape(maximalInputs.col(k++), squareRows, squareRows);
+ arma::reshape(maximalInputs.col(k++),
+ blockHeight, blockWidth);
if(k >= maxSize) {
break;
}
diff --git a/src/mlpack/core/math/columns_to_blocks.hpp b/src/mlpack/core/math/columns_to_blocks.hpp
index 4ec3286..816d1b8 100644
--- a/src/mlpack/core/math/columns_to_blocks.hpp
+++ b/src/mlpack/core/math/columns_to_blocks.hpp
@@ -35,8 +35,41 @@ class ColumnsToBlocks
* Constructor of ColumnsToBlocks
* @param rows number of blocks per cols
* @param cols number of blocks per rows
+ * @param blockHeight height of block
+ * @param blockWidth width of block
+ * @warning blockHeight * blockWidth must equal to maximalInputs.n_rows
+ * By default the blockHeight and blockWidth will equal to
+ * std::sqrt(maximalInputs.n_rows)
+ * @code
+ * arma::mat maximalInputs;
+ * maximalInputs<<-1.0000<<0.1429<<arma::endr
+ * <<-0.7143<<0.4286<<arma::endr
+ * <<-0.4286<<0.7143<<arma::endr
+ * <<-0.1429<<1.0000<<arma::endr;
+ * arma::mat output;
+ * mlpack::math::ColumnsToBlocks ctb(1, 2);
+ * ctb.Transform(maximalInputs, output);
+ * //The cols of the maximalInputs output will reshape as a square which
+ * //surrounded by padding value -1(this value could be set by BufValue)
+ * //-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
+ * //-1.0000 -1.0000 -0.4286 -1.0000 0.1429 0.7143 -1.0000
+ * //-1.0000 -0.7143 -0.1429 -1.0000 0.4286 1.0000 -1.0000
+ * //-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
+ *
+ * ctb.BlockWidth(4);
+ * ctb.BlockHeight(1);
+ * ctb.Transform(maxinalInputs, output);
+ * //The cols of the maximalInputs output will reshape as a square which
+ * //surrounded by padding value -1(this value could be set by BufValue)
+ * //-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
+ * //-1.0000 -1.0000 -0.7143 -0.4286 -0.1429 -1.0000 0.1429 0.4286 0.7143 1.0000 -1.0000
+ * //-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
+ *
+ * @endcode
*/
- ColumnsToBlocks(arma::uword rows, arma::uword cols);
+ ColumnsToBlocks(size_t rows, size_t cols,
+ size_t blockHeight = 0,
+ size_t blockWidth = 0);
/**
* Transform the columns of maximalInputs into blocks
@@ -47,18 +80,48 @@ class ColumnsToBlocks
void Transform(const arma::mat &maximalInputs, arma::mat &output);
/**
+ * Height of the block, please refer to the comments of
+ * constructor for details
+ */
+ void BlockHeight(size_t value) {
+ blockHeight = value;
+ }
+
+ /**
+ * Return block height
+ */
+ size_t BlockHeight() const {
+ return blockHeight;
+ }
+
+ /**
+ * Width of the blcok, , please refer to the comments of
+ * constructor for details
+ */
+ void BlockWidth(size_t value) {
+ blockWidth = value;
+ }
+
+ /**
+ * Return block width
+ */
+ size_t BlockWidth() const {
+ return blockWidth;
+ }
+
+ /**
* 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) {
+ void BufSize(size_t value) {
bufSize = value;
}
/**
* Return buffer size
*/
- arma::uword BufSize() const {
+ size_t BufSize() const {
return bufSize;
}
@@ -77,14 +140,14 @@ class ColumnsToBlocks
* set number of blocks per rows
* @param value number of blocks per rows, default value is 0
*/
- void Cols(arma::uword value) {
+ void Cols(size_t value) {
cols = value;
}
/**
* Return number of blocks per rows
*/
- arma::uword Cols() const {
+ size_t Cols() const {
return cols;
}
@@ -122,14 +185,14 @@ class ColumnsToBlocks
* @brief Set number of blocks per rows
* @param cols number of blocks per rows, default value is 0
*/
- void Rows(arma::uword value) {
+ void Rows(size_t value) {
rows = value;
}
/**
* Return number of blocks per rows
*/
- arma::uword Rows() const {
+ size_t Rows() const {
return rows;
}
@@ -150,15 +213,17 @@ class ColumnsToBlocks
}
private:
- bool IsPerfectSquare(arma::uword value) const;
+ bool IsPerfectSquare(size_t value) const;
- arma::uword bufSize;
+ size_t blockHeight;
+ size_t blockWidth;
+ size_t bufSize;
double bufValue;
- arma::uword cols;
+ size_t cols;
double maxRange;
double minRange;
bool scale;
- arma::uword rows;
+ size_t rows;
};
} // namespace math
More information about the mlpack-git
mailing list