src/mlpack/tests/ann_layer_test.cpp
BOOST_AUTO_TEST_CASE(ForwardGRULayerTest)
{
  // This will make it easier to clean memory later.
  GRU<>* gruAlloc = new GRU<>(3, 3, 5);
  GRU<>& gru = *gruAlloc;

  // Initialize the weights to all ones.
  NetworkInitialization<ConstInitialization>
    networkInit(ConstInitialization(1));
  networkInit.Initialize(gru.Model(), gru.Parameters());

  // Provide input of all ones.
  arma::mat input = arma::ones(3, 1);
  arma::mat output;

  gru.Forward(input, output);

  // Compute the z_t gate output.
  arma::mat expectedOutput = arma::ones(3, 1);
  expectedOutput *= -4;
  expectedOutput = arma::exp(expectedOutput);
  expectedOutput = arma::ones(3, 1) / (arma::ones(3, 1) + expectedOutput);
  expectedOutput = (arma::ones(3, 1)  - expectedOutput) % expectedOutput;

  // For the first input the output should be equal to the output of
  // gate z_t as the previous output fed to the cell is all zeros.
  BOOST_REQUIRE_LE(arma::as_scalar(arma::trans(output) * expectedOutput), 1e-2);

  expectedOutput = output;

  gru.Forward(input, output);

  double s = arma::as_scalar(arma::sum(expectedOutput));

  // Compute the value of z_t gate for the second input.
  arma::mat z_t = arma::ones(3, 1);
  z_t *= -(s + 4);
  z_t = arma::exp(z_t);
  z_t = arma::ones(3, 1) / (arma::ones(3, 1) + z_t);

  // Compute the value of o_t gate for the second input.
  arma::mat o_t = arma::ones(3, 1);
  o_t *= -(arma::as_scalar(arma::sum(expectedOutput % z_t)) + 4);
  o_t = arma::exp(o_t);
  o_t = arma::ones(3, 1) / (arma::ones(3, 1) + o_t);

  // Expected output for the second input.
  expectedOutput = z_t % expectedOutput + (arma::ones(3, 1) - z_t) % o_t;

  BOOST_REQUIRE_LE(arma::as_scalar(arma::trans(output) * expectedOutput), 1e-2);

  LayerTypes<> layer(gruAlloc);
  boost::apply_visitor(DeleteVisitor(), layer);
}
/**
 * GRU layer manual forward test.
 */
GRU layer manual forward test.
GRU layer manual forward test .
BOOST_AUTO_TEST_CASE(ForwardGRULayerTest)
{
  GRU<>* gruAlloc = new GRU<>(3, 3, 5);
  GRU<>& gru = *gruAlloc;
  NetworkInitialization<ConstInitialization>
    networkInit(ConstInitialization(1));
  networkInit.Initialize(gru.Model(), gru.Parameters());
  arma::mat input = arma::ones(3, 1);
  arma::mat output;
  gru.Forward(input, output);
  arma::mat expectedOutput = arma::ones(3, 1);
  expectedOutput *= -4;
  expectedOutput = arma::exp(expectedOutput);
  expectedOutput = arma::ones(3, 1) / (arma::ones(3, 1) + expectedOutput);
  expectedOutput = (arma::ones(3, 1)  - expectedOutput) % expectedOutput;
  BOOST_REQUIRE_LE(arma::as_scalar(arma::trans(output) * expectedOutput), 1e-2);
  expectedOutput = output;
  gru.Forward(input, output);
  double s = arma::as_scalar(arma::sum(expectedOutput));
  arma::mat z_t = arma::ones(3, 1);
  z_t *= -(s + 4);
  z_t = arma::exp(z_t);
  z_t = arma::ones(3, 1) / (arma::ones(3, 1) + z_t);
  arma::mat o_t = arma::ones(3, 1);
  o_t *= -(arma::as_scalar(arma::sum(expectedOutput % z_t)) + 4);
  o_t = arma::exp(o_t);
  o_t = arma::ones(3, 1) / (arma::ones(3, 1) + o_t);
  expectedOutput = z_t % expectedOutput + (arma::ones(3, 1) - z_t) % o_t;
  BOOST_REQUIRE_LE(arma::as_scalar(arma::trans(output) * expectedOutput), 1e-2);
  LayerTypes<> layer(gruAlloc);
  boost::apply_visitor(DeleteVisitor(), layer);
}
BOOST_AUTO_TEST_CASE ( ForwardGRULayerTest ) { GRU < > * gruAlloc = new GRU < > ( 3 , 3 , 5 ) ; GRU < > & gru = * gruAlloc ; NetworkInitialization < ConstInitialization > networkInit ( ConstInitialization ( 1 ) ) ; networkInit . Initialize ( gru . Model ( ) , gru . Parameters ( ) ) ; arma :: mat input = arma :: ones ( 3 , 1 ) ; arma :: mat output ; gru . Forward ( input , output ) ; arma :: mat expectedOutput = arma :: ones ( 3 , 1 ) ; expectedOutput *= -4 ; expectedOutput = arma :: exp ( expectedOutput ) ; expectedOutput = arma :: ones ( 3 , 1 ) / ( arma :: ones ( 3 , 1 ) + expectedOutput ) ; expectedOutput = ( arma :: ones ( 3 , 1 ) - expectedOutput ) % expectedOutput ; BOOST_REQUIRE_LE ( arma :: as_scalar ( arma :: trans ( output ) * expectedOutput ) , 1e-2 ) ; expectedOutput = output ; gru . Forward ( input , output ) ; double s = arma :: as_scalar ( arma :: sum ( expectedOutput ) ) ; arma :: mat z_t = arma :: ones ( 3 , 1 ) ; z_t *= - ( s + 4 ) ; z_t = arma :: exp ( z_t ) ; z_t = arma :: ones ( 3 , 1 ) / ( arma :: ones ( 3 , 1 ) + z_t ) ; arma :: mat o_t = arma :: ones ( 3 , 1 ) ; o_t *= - ( arma :: as_scalar ( arma :: sum ( expectedOutput % z_t ) ) + 4 ) ; o_t = arma :: exp ( o_t ) ; o_t = arma :: ones ( 3 , 1 ) / ( arma :: ones ( 3 , 1 ) + o_t ) ; expectedOutput = z_t % expectedOutput + ( arma :: ones ( 3 , 1 ) - z_t ) % o_t ; BOOST_REQUIRE_LE ( arma :: as_scalar ( arma :: trans ( output ) * expectedOutput ) , 1e-2 ) ; LayerTypes < > layer ( gruAlloc ) ; boost :: apply_visitor ( DeleteVisitor ( ) , layer ) ; }
GRU layer manual forward test.
GRU layer manual forward test .
// This will make it easier to clean memory later. // Initialize the weights to all ones. // Provide input of all ones. // Compute the z_t gate output. // For the first input the output should be equal to the output of // gate z_t as the previous output fed to the cell is all zeros. // Compute the value of z_t gate for the second input. // Compute the value of o_t gate for the second input. // Expected output for the second input.