[mlpack-git] master: Adds inplace transpose method and some tests. This method used in Load() now. (7881c3e)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:16:55 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 7881c3e3e69b74b902e370a9ccd8348ffb9ff3c5
Author: Vladimir Glazachev <glazachev.vladimir at gmail.com>
Date: Sun Feb 22 04:51:43 2015 +0400
Adds inplace transpose method and some tests. This method used in Load() now.
>---------------------------------------------------------------
7881c3e3e69b74b902e370a9ccd8348ffb9ff3c5
src/mlpack/core/arma_extend/CMakeLists.txt | 1 +
src/mlpack/core/arma_extend/arma_extend.hpp | 3 ++
.../core/arma_extend/fn_inplace_transpose.hpp | 21 +++++++++
src/mlpack/core/data/load_impl.hpp | 4 +-
src/mlpack/tests/arma_extend_test.cpp | 52 ++++++++++++++++++++++
5 files changed, 80 insertions(+), 1 deletion(-)
diff --git a/src/mlpack/core/arma_extend/CMakeLists.txt b/src/mlpack/core/arma_extend/CMakeLists.txt
index 43f6ee1..36b8593 100644
--- a/src/mlpack/core/arma_extend/CMakeLists.txt
+++ b/src/mlpack/core/arma_extend/CMakeLists.txt
@@ -3,6 +3,7 @@
set(SOURCES
arma_extend.hpp
fn_ccov.hpp
+ fn_inplace_transpose.hpp
glue_ccov_meat.hpp
glue_ccov_proto.hpp
hdf5_misc.hpp
diff --git a/src/mlpack/core/arma_extend/arma_extend.hpp b/src/mlpack/core/arma_extend/arma_extend.hpp
index ec50735..c3c2d98 100644
--- a/src/mlpack/core/arma_extend/arma_extend.hpp
+++ b/src/mlpack/core/arma_extend/arma_extend.hpp
@@ -47,6 +47,9 @@ namespace arma {
// unary minus for sparse matrices
#include "operator_minus.hpp"
+
+ // inplace_transpose()
+ #include "fn_inplace_transpose.hpp"
};
#endif
diff --git a/src/mlpack/core/arma_extend/fn_inplace_transpose.hpp b/src/mlpack/core/arma_extend/fn_inplace_transpose.hpp
new file mode 100644
index 0000000..83e8bec
--- /dev/null
+++ b/src/mlpack/core/arma_extend/fn_inplace_transpose.hpp
@@ -0,0 +1,21 @@
+
+/**
+ * This method automatically selects the transpose method,
+ * depending on available memory.
+ */
+template<typename eT>
+bool
+inline
+inplace_transpose(Mat<eT>& X)
+ {
+ try
+ {
+ X = trans(X);
+ return false;
+ }
+ catch (std::bad_alloc& exception)
+ {
+ inplace_trans(X, "lowmem");
+ return true;
+ }
+ }
diff --git a/src/mlpack/core/data/load_impl.hpp b/src/mlpack/core/data/load_impl.hpp
index c1fe470..fee0e65 100644
--- a/src/mlpack/core/data/load_impl.hpp
+++ b/src/mlpack/core/data/load_impl.hpp
@@ -220,7 +220,9 @@ bool Load(const std::string& filename,
// Now transpose the matrix, if necessary.
if (transpose)
- matrix = trans(matrix);
+ {
+ arma::inplace_transpose(matrix);
+ }
Timer::Stop("loading_data");
diff --git a/src/mlpack/tests/arma_extend_test.cpp b/src/mlpack/tests/arma_extend_test.cpp
index 13c7246..10d5af6 100644
--- a/src/mlpack/tests/arma_extend_test.cpp
+++ b/src/mlpack/tests/arma_extend_test.cpp
@@ -49,6 +49,58 @@ BOOST_AUTO_TEST_CASE(InplaceReshapeMatrixTest)
}
/**
+ * Make sure we can still transpose correct.
+ */
+BOOST_AUTO_TEST_CASE(InplaceTransposeTest)
+{
+ mat X;
+ X.randu(2, 4);
+
+ mat Y = X;
+
+ BOOST_REQUIRE(arma::inplace_transpose(X) == false);
+ BOOST_REQUIRE_EQUAL(X.n_rows, 4);
+ BOOST_REQUIRE_EQUAL(X.n_cols, 2);
+
+ for (size_t i = 0; i < X.n_rows; ++i)
+ for (size_t j = 0; j < X.n_cols; ++j)
+ BOOST_REQUIRE_CLOSE(X.at(i, j), Y.at(j, i), 1e-5);
+}
+
+/**
+ * Try to transpose using low-mem method.
+ */
+BOOST_AUTO_TEST_CASE(InplaceTransposeLowMemTest)
+{
+ mat X;
+
+ int width = 1000;
+ int height = 50000;
+
+ size_t n_iters = 50;
+
+ for (size_t i = 0; i < n_iters; ++i)
+ {
+ try
+ {
+ X.zeros(height, width);
+ if (inplace_transpose(X))
+ {
+ break;
+ }
+ }
+ catch (std::bad_alloc& exception)
+ {
+ BOOST_FAIL("bad_alloc exception.");
+ }
+ height *= 2;
+ }
+
+ BOOST_REQUIRE_EQUAL(X.n_rows, width);
+ BOOST_REQUIRE_EQUAL(X.n_cols, height);
+}
+
+/**
* Test const_row_col_iterator for basic functionality.
*/
BOOST_AUTO_TEST_CASE(ConstRowColIteratorTest)
More information about the mlpack-git
mailing list