Finishing the Alphadroput layer, tests are passing

Signed-off-by: Omar Shrit <omar@shrit.me>
This commit is contained in:
Omar Shrit
2021-11-16 17:43:09 +00:00
parent c19e39a152
commit eae19f7c28
4 changed files with 82 additions and 77 deletions
+2 -2
View File
@@ -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
@@ -59,6 +59,11 @@ class AlphaDropout : public Layer<InputType, OutputType>
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.
*
+1 -1
View File
@@ -20,7 +20,7 @@
//#include <mlpack/methods/ann/layer/adaptive_mean_pooling.hpp>
#include <mlpack/methods/ann/layer/add.hpp>
//#include <mlpack/methods/ann/layer/add_merge.hpp>
//#include <mlpack/methods/ann/layer/alpha_dropout.hpp>
#include <mlpack/methods/ann/layer/alpha_dropout.hpp>
#include <mlpack/methods/ann/layer/base_layer.hpp>
//#include <mlpack/methods/ann/layer/batch_norm.hpp>
//#include <mlpack/methods/ann/layer/bilinear_interpolation.hpp>
+74 -74
View File
@@ -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<arma::mat>(1000, 1);
// Initialize the input parameter having a mean nearabout 0
// and variance nearabout 1.
arma::mat input = arma::randn<arma::mat>(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.