From e2ae4f984eb485083c68ecca3edc26e2aba10189 Mon Sep 17 00:00:00 2001 From: Haritha Date: Wed, 13 Jun 2018 00:27:30 +0530 Subject: [PATCH] tests edit --- src/mlpack/methods/ann/layer/subview.hpp | 14 +++------- src/mlpack/tests/ann_layer_test.cpp | 34 ++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/mlpack/methods/ann/layer/subview.hpp b/src/mlpack/methods/ann/layer/subview.hpp index 2f74fb40a5..82aa346bce 100644 --- a/src/mlpack/methods/ann/layer/subview.hpp +++ b/src/mlpack/methods/ann/layer/subview.hpp @@ -41,7 +41,7 @@ class Subview * @param begin Start index. * @param end End index. */ - Subview(const arma::uword begin = 0, const arma::uword end = 0): + Subview(const size_t begin = 0, const size_t end = 0): begin(begin), end(end) { @@ -58,11 +58,11 @@ class Subview template void Forward(InputType&& input, OutputType&& output) { + // Check if input has been selected as required. if ((input.n_rows != (end-begin+1)) && (end != 0)) { - arma::uword cols = input.n_cols; - output = arma::mat(&input(begin), end - begin + 1, cols, false); + output = arma::mat(&input(begin), end - begin + 1, input.n_cols, false); } else { @@ -80,16 +80,10 @@ class Subview * @param g The calculated gradient. */ template - void Backward(arma::Mat&& input, + void Backward(arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) { - // Check if input has been selected as required. - if ((input.n_rows != (end-begin+1)) && (end != 0)) - { - arma::uword cols = input.n_cols; - input = arma::mat(&input(begin), end - begin + 1, cols, false); - } g = gy; } diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index 40e7758b0b..aaae439aec 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -1813,8 +1813,38 @@ BOOST_AUTO_TEST_CASE(SimpleSubviewLayerTest) // Test the Backward function. module.Backward(std::move(input), std::move(input), std::move(delta)); - BOOST_REQUIRE_EQUAL(accu(delta), 10); - BOOST_REQUIRE_EQUAL(delta.n_rows, 10); + BOOST_REQUIRE_EQUAL(accu(delta), 20); + BOOST_REQUIRE_EQUAL(delta.n_rows, 20); +} + +/** + * Subview index test. + */ +BOOST_AUTO_TEST_CASE(SubviewIndexTest) +{ + arma::mat outputEnd, outputMid, outputStart, input, delta; + input = arma::linspace(1, 20, 20); + + // Slicing from the initial indices. + Subview<> moduleStart(0, 9); + arma::mat subStart = arma::linspace(1, 10, 10); + + moduleStart.Forward(std::move(input), std::move(outputStart)); + CheckMatrices(outputStart, subStart); + + // Slicing from the mid indices. + Subview<> moduleMid(6, 15); + arma::mat subMid = arma::linspace(7, 16, 10); + + moduleMid.Forward(std::move(input), std::move(outputMid)); + CheckMatrices(outputMid, subMid); + + // Slicing from the end indices. + Subview<> moduleEnd(10, 19); + arma::mat subEnd = arma::linspace(11, 20, 10); + + moduleEnd.Forward(std::move(input), std::move(outputEnd)); + CheckMatrices(outputEnd, subEnd); } BOOST_AUTO_TEST_SUITE_END();