diff --git a/src/mlpack/methods/ann/layer/CMakeLists.txt b/src/mlpack/methods/ann/layer/CMakeLists.txt index 8c851e0d85..2d15b23661 100644 --- a/src/mlpack/methods/ann/layer/CMakeLists.txt +++ b/src/mlpack/methods/ann/layer/CMakeLists.txt @@ -9,8 +9,8 @@ set(SOURCES adaptive_max_pooling_impl.hpp adaptive_mean_pooling.hpp adaptive_mean_pooling_impl.hpp - # alpha_dropout.hpp - # alpha_dropout_impl.hpp + alpha_dropout.hpp + alpha_dropout_impl.hpp # atrous_convolution.hpp # atrous_convolution_impl.hpp # base_layer.hpp diff --git a/src/mlpack/methods/ann/layer/alpha_dropout.hpp b/src/mlpack/methods/ann/layer/alpha_dropout.hpp index a38e30edea..4b62662b7c 100644 --- a/src/mlpack/methods/ann/layer/alpha_dropout.hpp +++ b/src/mlpack/methods/ann/layer/alpha_dropout.hpp @@ -59,6 +59,11 @@ class AlphaDropout : public Layer AlphaDropout(const double ratio = 0.5, const double alphaDash = -alpha * lambda); + /** + * Clone the DropoutType object. This handles polymorphism correctly. + */ + AlphaDropout* Clone() const { return new AlphaDropout(*this); } + /** * Ordinary feed forward pass of the alpha_dropout layer. * diff --git a/src/mlpack/methods/ann/layer/layer_types.hpp b/src/mlpack/methods/ann/layer/layer_types.hpp index c7f9fd5b1f..04bf79e40c 100644 --- a/src/mlpack/methods/ann/layer/layer_types.hpp +++ b/src/mlpack/methods/ann/layer/layer_types.hpp @@ -20,7 +20,7 @@ //#include #include //#include -//#include +#include #include //#include //#include diff --git a/src/mlpack/tests/ann_layer_test.cpp b/src/mlpack/tests/ann_layer_test.cpp index a2dcb7cd50..89180483f2 100644 --- a/src/mlpack/tests/ann_layer_test.cpp +++ b/src/mlpack/tests/ann_layer_test.cpp @@ -321,96 +321,96 @@ TEST_CASE("NoDropoutTest", "[ANNLayerTest]") REQUIRE(arma::accu(output) == arma::accu(input)); } -// /* -// * Perform test to check whether mean and variance remain nearly same -// * after AlphaDropout. -// */ -// TEST_CASE("SimpleAlphaDropoutLayerTest", "[ANNLayerTest]") -// { -// // Initialize the probability of setting a value to alphaDash. -// const double p = 0.2; +/* + * Perform test to check whether mean and variance remain nearly same + * after AlphaDropout. + */ +TEST_CASE("SimpleAlphaDropoutLayerTest", "[ANNLayerTest]") +{ + // Initialize the probability of setting a value to alphaDash. + const double p = 0.2; -// // Initialize the input parameter having a mean nearabout 0 -// // and variance nearabout 1. -// arma::mat input = arma::randn(1000, 1); + // Initialize the input parameter having a mean nearabout 0 + // and variance nearabout 1. + arma::mat input = arma::randn(1000, 1); -// AlphaDropout<> module(p); -// module.Deterministic() = false; + AlphaDropout<> module(p); + module.Training() = true; -// // Test the Forward function when training phase. -// arma::mat output; -// module.Forward(input, output); -// // Check whether mean remains nearly same. -// REQUIRE(arma::as_scalar(arma::abs(arma::mean(input) - arma::mean(output))) <= -// 0.1); + // Test the Forward function when training phase. + arma::mat output (arma::size(input)); + module.Forward(input, output); + // Check whether mean remains nearly same. + REQUIRE(arma::as_scalar(arma::abs(arma::mean(input) - arma::mean(output))) <= + 0.1); -// // Check whether variance remains nearly same. -// REQUIRE(arma::as_scalar(arma::abs(arma::var(input) - arma::var(output))) <= -// 0.1); + // Check whether variance remains nearly same. + REQUIRE(arma::as_scalar(arma::abs(arma::var(input) - arma::var(output))) <= + 0.1); -// // Test the Backward function when training phase. -// arma::mat delta; -// module.Backward(input, input, delta); -// REQUIRE(arma::as_scalar(arma::abs(arma::mean(delta) - 0)) <= 0.05); + // Test the Backward function when training phase. + arma::mat delta; + module.Backward(input, input, delta); + REQUIRE(arma::as_scalar(arma::abs(arma::mean(delta) - 0)) <= 0.05); -// // Test the Forward function when testing phase. -// module.Deterministic() = true; -// module.Forward(input, output); -// REQUIRE(arma::accu(input) == arma::accu(output)); -// } + // Test the Forward function when testing phase. + module.Training() = false; + module.Forward(input, output); + REQUIRE(arma::accu(input) == arma::accu(output)); +} -// /** -// * Perform AlphaDropout x times using ones as input, sum the number of ones -// * and validate that the layer is producing approximately the correct number -// * of ones. -// */ -// TEST_CASE("AlphaDropoutProbabilityTest", "[ANNLayerTest]") -// { -// arma::mat input = arma::ones(1500, 1); -// const size_t iterations = 10; +/** + * Perform AlphaDropout x times using ones as input, sum the number of ones + * and validate that the layer is producing approximately the correct number + * of ones. + */ +TEST_CASE("AlphaDropoutProbabilityTest", "[ANNLayerTest]") +{ + arma::mat input = arma::ones(1500, 1); + const size_t iterations = 10; -// double probability[5] = { 0.1, 0.3, 0.4, 0.7, 0.8 }; -// for (size_t trial = 0; trial < 5; ++trial) -// { -// double nonzeroCount = 0; -// for (size_t i = 0; i < iterations; ++i) -// { -// AlphaDropout<> module(probability[trial]); -// module.Deterministic() = false; + double probability[5] = { 0.1, 0.3, 0.4, 0.7, 0.8 }; + for (size_t trial = 0; trial < 5; ++trial) + { + double nonzeroCount = 0; + for (size_t i = 0; i < iterations; ++i) + { + AlphaDropout<> module(probability[trial]); + module.Training() = true; -// arma::mat output; -// module.Forward(input, output); + arma::mat output(arma::size(input)); + module.Forward(input, output); -// // Return a column vector containing the indices of elements of X -// // that are not alphaDash, we just need the number of -// // nonAlphaDash values. -// arma::uvec nonAlphaDash = arma::find(module.Mask()); -// nonzeroCount += nonAlphaDash.n_elem; -// } + // Return a column vector containing the indices of elements of X + // that are not alphaDash, we just need the number of + // nonAlphaDash values. + arma::uvec nonAlphaDash = arma::find(module.Mask()); + nonzeroCount += nonAlphaDash.n_elem; + } -// const double expected = input.n_elem * (1-probability[trial]) * iterations; + const double expected = input.n_elem * (1-probability[trial]) * iterations; -// const double error = fabs(nonzeroCount - expected) / expected; + const double error = fabs(nonzeroCount - expected) / expected; -// REQUIRE(error <= 0.15); -// } -// } + REQUIRE(error <= 0.15); + } +} -// /** -// * Perform AlphaDropout with probability 1 - p where p = 0, -// * means no AlphaDropout. -// */ -// TEST_CASE("NoAlphaDropoutTest", "[ANNLayerTest]") -// { -// arma::mat input = arma::ones(1500, 1); -// AlphaDropout<> module(0); -// module.Deterministic() = false; +/** + * Perform AlphaDropout with probability 1 - p where p = 0, + * means no AlphaDropout. + */ +TEST_CASE("NoAlphaDropoutTest", "[ANNLayerTest]") +{ + arma::mat input = arma::ones(1500, 1); + AlphaDropout<> module(0); + module.Training() = false; -// arma::mat output; -// module.Forward(input, output); + arma::mat output; + module.Forward(input, output); -// REQUIRE(arma::accu(output) == arma::accu(input)); -// } + REQUIRE(arma::accu(output) == arma::accu(input)); +} // /** // * Simple linear module test.