diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index 47aba100da..57b3fe96da 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -1,6 +1,7 @@ /** * @file feedforward_network_test.cpp * @author Marcus Edel + * @author Palash Ahuja * * Tests the feed forward network. */ @@ -288,7 +289,8 @@ BOOST_AUTO_TEST_CASE(DropoutNetworkTest) } /** - * Train and evaluate a DropConnect network(with a baselayer) with the specified structure. + * Train and evaluate a DropConnect network(with a baselayer) with the + * specified structure. */ template< typename PerformanceFunction, @@ -304,66 +306,71 @@ void BuildDropConnectNetwork(MatType& trainData, const size_t maxEpochs, const double classificationErrorThreshold) { -/* -* Construct a feed forward network with trainData.n_rows input nodes, -* hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The -* network struct that looks like: -* -* Input Hidden DropConnect Output -* Layer Layer Layer Layer -* +-----+ +-----+ +-----+ +-----+ -* | | | | | | | | -* | +------>| +------>| +------>| | -* | | +>| | | | | | -* +-----+ | +--+--+ +-----+ +-----+ -* | -* Bias | -* Layer | -* +-----+ | -* | | | -* | +-----+ -* | | -* +-----+ -* -* -*/ -LinearLayer<> inputLayer(trainData.n_rows, hiddenLayerSize); -BiasLayer<> biasLayer(hiddenLayerSize); -BaseLayer hiddenLayer0; + /* + * Construct a feed forward network with trainData.n_rows input nodes, + * hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The + * network struct that looks like: + * + * Input Hidden DropConnect Output + * Layer Layer Layer Layer + * +-----+ +-----+ +-----+ +-----+ + * | | | | | | | | + * | +------>| +------>| +------>| | + * | | +>| | | | | | + * +-----+ | +--+--+ +-----+ +-----+ + * | + * Bias | + * Layer | + * +-----+ | + * | | | + * | +-----+ + * | | + * +-----+ + * + * + */ + LinearLayer<> inputLayer(trainData.n_rows, hiddenLayerSize); + BiasLayer<> biasLayer(hiddenLayerSize); + BaseLayer hiddenLayer0; -LinearLayer<> hiddenLayer1(hiddenLayerSize, trainLabels.n_rows); -DropConnectLayer dropConnectLayer0(hiddenLayer1); + LinearLayer<> hiddenLayer1(hiddenLayerSize, trainLabels.n_rows); + DropConnectLayer dropConnectLayer0(hiddenLayer1); -BaseLayer outputLayer; + BaseLayer outputLayer; -OutputLayerType classOutputLayer; + OutputLayerType classOutputLayer; -auto modules = std::tie(inputLayer, biasLayer, hiddenLayer0, - dropConnectLayer0, outputLayer); + auto modules = std::tie(inputLayer, biasLayer, hiddenLayer0, + dropConnectLayer0, outputLayer); -FFN net(modules, classOutputLayer); -RMSprop opt(net, 0.01, 0.88, 1e-8, - maxEpochs * trainData.n_cols, 1e-18); -net.Train(trainData, trainLabels, opt); -MatType prediction; -net.Predict(testData, prediction); + FFN net(modules, classOutputLayer); -size_t error = 0; -for (size_t i = 0; i < testData.n_cols; i++) -{ - if (arma::sum(arma::sum( - arma::abs(prediction.col(i) - testLabels.col(i)))) == 0) - { - error++; - } -} -double classificationError = 1 - double(error) / testData.n_cols; -BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold); + RMSprop opt(net, 0.01, 0.88, 1e-8, + maxEpochs * trainData.n_cols, 1e-18); + + net.Train(trainData, trainLabels, opt); + + MatType prediction; + net.Predict(testData, prediction); + + size_t error = 0; + for (size_t i = 0; i < testData.n_cols; i++) + { + if (arma::sum(arma::sum( + arma::abs(prediction.col(i) - testLabels.col(i)))) == 0) + { + error++; + } + } + + double classificationError = 1 - double(error) / testData.n_cols; + BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold); } /** - * Train and evaluate a DropConnect network(with a linearlayer) with the specified structure. + * Train and evaluate a DropConnect network(with a linearlayer) with the + * specified structure. */ template< typename PerformanceFunction, @@ -379,60 +386,64 @@ void BuildDropConnectNetworkLinear(MatType& trainData, const size_t maxEpochs, const double classificationErrorThreshold) { -/* -* Construct a feed forward network with trainData.n_rows input nodes, -* hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The -* network struct that looks like: -* -* Input Hidden DropConnect Output -* Layer Layer Layer Layer -* +-----+ +-----+ +-----+ +-----+ -* | | | | | | | | -* | +------>| +------>| +------>| | -* | | +>| | | | | | -* +-----+ | +--+--+ +-----+ +-----+ -* | -* Bias | -* Layer | -* +-----+ | -* | | | -* | +-----+ -* | | -* +-----+ -* -* -*/ -LinearLayer<> inputLayer(trainData.n_rows, hiddenLayerSize); -BiasLayer<> biasLayer(hiddenLayerSize); -BaseLayer hiddenLayer0; -const size_t number_of_rows = trainLabels.n_rows; -DropConnectLayer<> dropConnectLayer0(hiddenLayerSize, number_of_rows); + /* + * Construct a feed forward network with trainData.n_rows input nodes, + * hiddenLayerSize hidden nodes and trainLabels.n_rows output nodes. The + * network struct that looks like: + * + * Input Hidden DropConnect Output + * Layer Layer Layer Layer + * +-----+ +-----+ +-----+ +-----+ + * | | | | | | | | + * | +------>| +------>| +------>| | + * | | +>| | | | | | + * +-----+ | +--+--+ +-----+ +-----+ + * | + * Bias | + * Layer | + * +-----+ | + * | | | + * | +-----+ + * | | + * +-----+ + * + * + */ + LinearLayer<> inputLayer(trainData.n_rows, hiddenLayerSize); + BiasLayer<> biasLayer(hiddenLayerSize); + BaseLayer hiddenLayer0; -BaseLayer outputLayer; + DropConnectLayer<> dropConnectLayer0(hiddenLayerSize, trainLabels.n_rows); -OutputLayerType classOutputLayer; -auto modules = std::tie(inputLayer, biasLayer, hiddenLayer0, - dropConnectLayer0, outputLayer); + BaseLayer outputLayer; -FFN net(modules, classOutputLayer); -RMSprop opt(net, 0.01, 0.88, 1e-8, - maxEpochs * trainData.n_cols, 1e-18); -net.Train(trainData, trainLabels, opt); -MatType prediction; -net.Predict(testData, prediction); + OutputLayerType classOutputLayer; + auto modules = std::tie(inputLayer, biasLayer, hiddenLayer0, + dropConnectLayer0, outputLayer); -size_t error = 0; -for (size_t i = 0; i < testData.n_cols; i++) -{ - if (arma::sum(arma::sum( - arma::abs(prediction.col(i) - testLabels.col(i)))) == 0) - { - error++; - } -} -double classificationError = 1 - double(error) / testData.n_cols; -BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold); + FFN net(modules, classOutputLayer); + + RMSprop opt(net, 0.01, 0.88, 1e-8, + maxEpochs * trainData.n_cols, 1e-18); + + net.Train(trainData, trainLabels, opt); + + MatType prediction; + net.Predict(testData, prediction); + + size_t error = 0; + for (size_t i = 0; i < testData.n_cols; i++) + { + if (arma::sum(arma::sum( + arma::abs(prediction.col(i) - testLabels.col(i)))) == 0) + { + error++; + } + } + + double classificationError = 1 - double(error) / testData.n_cols; + BOOST_REQUIRE_LE(classificationError, classificationErrorThreshold); } /** * Train the dropconnect network on a larger dataset. @@ -464,8 +475,8 @@ BOOST_AUTO_TEST_CASE(DropConnectNetworkTest) (trainData, trainLabels, testData, testLabels, 4, 100, 0.1); BuildDropConnectNetworkLinear + BinaryClassificationLayer, + MeanSquaredErrorFunction> (trainData, trainLabels, testData, testLabels, 4, 100, 0.1); dataset.load("mnist_first250_training_4s_and_9s.arm"); @@ -479,24 +490,15 @@ BOOST_AUTO_TEST_CASE(DropConnectNetworkTest) // Vanilla neural net with logistic activation function. BuildDropConnectNetwork + BinaryClassificationLayer, + MeanSquaredErrorFunction> (dataset, labels, dataset, labels, 8, 30, 0.4); BuildDropConnectNetworkLinear + BinaryClassificationLayer, + MeanSquaredErrorFunction> (dataset, labels, dataset, labels, 8, 30, 0.4); - - // Vanilla neural net with tanh activation function. - BuildDropConnectNetwork - (dataset, labels, dataset, labels, 8, 30, 0.4); - BuildDropConnectNetworkLinear - (dataset, labels, dataset, labels, 8, 30, 0.4); } + BOOST_AUTO_TEST_SUITE_END();