diff --git a/.gitignore b/.gitignore index 1d559f3..26cb93e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ CMakeCache.txt CPackConfig.cmake CPackSourceConfig.cmake cmake_install.cmake +.cmake/ fann-config.cmake fann.pc install_manifest.txt diff --git a/format.sh b/format.sh index d298e6e..298848e 100755 --- a/format.sh +++ b/format.sh @@ -1,3 +1,4 @@ #!/bin/sh find src -regex '.*\.\(cpp\|hpp\|c\|h\)' -exec clang-format -i {} \; +find tests -regex '.*\.\(cpp\|hpp\|c\|h\)' -exec clang-format -i {} \; diff --git a/src/fann.c b/src/fann.c index f225ab4..b1ee769 100644 --- a/src/fann.c +++ b/src/fann.c @@ -1413,10 +1413,11 @@ void fann_update_stepwise(struct fann *ann) { fann_min(ann->multiplier - (fann_type)(ann->multiplier / 100.0 + 1.0), ann->multiplier - 1); for (i = 0; i < 6; i++) { - ann->sigmoid_values[i] = (fann_type)( - ((log(ann->multiplier / (float)ann->sigmoid_results[i] - 1) * (float)ann->multiplier) / - -2.0) * - (float)ann->multiplier); + ann->sigmoid_values[i] = + (fann_type)(((log(ann->multiplier / (float)ann->sigmoid_results[i] - 1) * + (float)ann->multiplier) / + -2.0) * + (float)ann->multiplier); ann->sigmoid_symmetric_values[i] = (fann_type)(((log((ann->multiplier - (float)ann->sigmoid_symmetric_results[i]) / ((float)ann->sigmoid_symmetric_results[i] + ann->multiplier)) * diff --git a/tests/fann_test.cpp b/tests/fann_test.cpp index 06e9f71..0045a93 100644 --- a/tests/fann_test.cpp +++ b/tests/fann_test.cpp @@ -4,172 +4,171 @@ using namespace std; void FannTest::SetUp() { - //ensure random generator is seeded at a known value to ensure reproducible results - srand(0); - fann_disable_seed_rand(); + // ensure random generator is seeded at a known value to ensure reproducible results + srand(0); + fann_disable_seed_rand(); } void FannTest::TearDown() { - net.destroy(); - data.destroy_train(); + net.destroy(); + data.destroy_train(); } void FannTest::AssertCreate(neural_net &net, unsigned int numLayers, const unsigned int *layers, unsigned int neurons, unsigned int connections) { - EXPECT_EQ(numLayers, net.get_num_layers()); - EXPECT_EQ(layers[0], net.get_num_input()); - EXPECT_EQ(layers[numLayers - 1], net.get_num_output()); - unsigned int *layers_res = new unsigned int[numLayers]; - net.get_layer_array(layers_res); - for (unsigned int i = 0; i < numLayers; i++) { - EXPECT_EQ(layers[i], layers_res[i]); - } - delete[] layers_res; + EXPECT_EQ(numLayers, net.get_num_layers()); + EXPECT_EQ(layers[0], net.get_num_input()); + EXPECT_EQ(layers[numLayers - 1], net.get_num_output()); + unsigned int *layers_res = new unsigned int[numLayers]; + net.get_layer_array(layers_res); + for (unsigned int i = 0; i < numLayers; i++) { + EXPECT_EQ(layers[i], layers_res[i]); + } + delete[] layers_res; - EXPECT_EQ(neurons, net.get_total_neurons()); - EXPECT_EQ(connections, net.get_total_connections()); + EXPECT_EQ(neurons, net.get_total_neurons()); + EXPECT_EQ(connections, net.get_total_connections()); - AssertWeights(net, -0.09, 0.09, 0.0); + AssertWeights(net, -0.09, 0.09, 0.0); } -void FannTest::AssertCreateAndCopy(neural_net &net, unsigned int numLayers, const unsigned int *layers, unsigned int neurons, +void FannTest::AssertCreateAndCopy(neural_net &net, unsigned int numLayers, + const unsigned int *layers, unsigned int neurons, unsigned int connections) { - AssertCreate(net, numLayers, layers, neurons, connections); - neural_net net_copy(net); - AssertCreate(net_copy, numLayers, layers, neurons, connections); + AssertCreate(net, numLayers, layers, neurons, connections); + neural_net net_copy(net); + AssertCreate(net_copy, numLayers, layers, neurons, connections); } void FannTest::AssertWeights(neural_net &net, fann_type min, fann_type max, fann_type avg) { - connection *connections = new connection[net.get_total_connections()]; - net.get_connection_array(connections); + connection *connections = new connection[net.get_total_connections()]; + net.get_connection_array(connections); - fann_type minWeight = connections[0].weight; - fann_type maxWeight = connections[0].weight; - fann_type totalWeight = 0.0; - for (int i = 1; i < net.get_total_connections(); ++i) { - if (connections[i].weight < minWeight) - minWeight = connections[i].weight; - if (connections[i].weight > maxWeight) - maxWeight = connections[i].weight; - totalWeight += connections[i].weight; - } + fann_type minWeight = connections[0].weight; + fann_type maxWeight = connections[0].weight; + fann_type totalWeight = 0.0; + for (int i = 1; i < net.get_total_connections(); ++i) { + if (connections[i].weight < minWeight) minWeight = connections[i].weight; + if (connections[i].weight > maxWeight) maxWeight = connections[i].weight; + totalWeight += connections[i].weight; + } - EXPECT_NEAR(min, minWeight, 0.05); - EXPECT_NEAR(max, maxWeight, 0.05); - EXPECT_NEAR(avg, totalWeight / (fann_type) net.get_total_connections(), 0.5); + EXPECT_NEAR(min, minWeight, 0.05); + EXPECT_NEAR(max, maxWeight, 0.05); + EXPECT_NEAR(avg, totalWeight / (fann_type)net.get_total_connections(), 0.5); } TEST_F(FannTest, CreateStandardThreeLayers) { - neural_net net(LAYER, 3, 2, 3, 4); - unsigned int layers[] = {2, 3, 4}; - AssertCreateAndCopy(net, 3, layers, 11, 25); + neural_net net(LAYER, 3, 2, 3, 4); + unsigned int layers[] = {2, 3, 4}; + AssertCreateAndCopy(net, 3, layers, 11, 25); } TEST_F(FannTest, CreateStandardThreeLayersUsingCreateMethod) { - ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); - unsigned int layers[] = {2, 3, 4}; - AssertCreateAndCopy(net, 3, layers, 11, 25); + ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); + unsigned int layers[] = {2, 3, 4}; + AssertCreateAndCopy(net, 3, layers, 11, 25); } TEST_F(FannTest, CreateStandardFourLayersArray) { - unsigned int layers[] = {2, 3, 4, 5}; - neural_net net(LAYER, 4, layers); - AssertCreateAndCopy(net, 4, layers, 17, 50); + unsigned int layers[] = {2, 3, 4, 5}; + neural_net net(LAYER, 4, layers); + AssertCreateAndCopy(net, 4, layers, 17, 50); } TEST_F(FannTest, CreateStandardFourLayersArrayUsingCreateMethod) { - unsigned int layers[] = {2, 3, 4, 5}; - ASSERT_TRUE(net.create_standard_array(4, layers)); - AssertCreateAndCopy(net, 4, layers, 17, 50); + unsigned int layers[] = {2, 3, 4, 5}; + ASSERT_TRUE(net.create_standard_array(4, layers)); + AssertCreateAndCopy(net, 4, layers, 17, 50); } TEST_F(FannTest, CreateStandardFourLayersVector) { - vector layers{2, 3, 4, 5}; - neural_net net(LAYER, layers.begin(), layers.end()); - AssertCreateAndCopy(net, 4, layers.data(), 17, 50); + vector layers{2, 3, 4, 5}; + neural_net net(LAYER, layers.begin(), layers.end()); + AssertCreateAndCopy(net, 4, layers.data(), 17, 50); } TEST_F(FannTest, CreateSparseFourLayers) { - neural_net net(0.5, 4, 2, 3, 4, 5); - unsigned int layers[] = {2, 3, 4, 5}; - AssertCreateAndCopy(net, 4, layers, 17, 31); + neural_net net(0.5, 4, 2, 3, 4, 5); + unsigned int layers[] = {2, 3, 4, 5}; + AssertCreateAndCopy(net, 4, layers, 17, 31); } TEST_F(FannTest, CreateSparseFourLayersUsingCreateMethod) { - ASSERT_TRUE(net.create_sparse(0.5f, 4, 2, 3, 4, 5)); - unsigned int layers[] = {2, 3, 4, 5}; - AssertCreateAndCopy(net, 4, layers, 17, 31); + ASSERT_TRUE(net.create_sparse(0.5f, 4, 2, 3, 4, 5)); + unsigned int layers[] = {2, 3, 4, 5}; + AssertCreateAndCopy(net, 4, layers, 17, 31); } TEST_F(FannTest, CreateSparseArrayFourLayers) { - unsigned int layers[] = {2, 3, 4, 5}; - neural_net net(0.5f, 4, layers); - AssertCreateAndCopy(net, 4, layers, 17, 31); + unsigned int layers[] = {2, 3, 4, 5}; + neural_net net(0.5f, 4, layers); + AssertCreateAndCopy(net, 4, layers, 17, 31); } TEST_F(FannTest, CreateSparseArrayFourLayersUsingCreateMethod) { - unsigned int layers[] = {2, 3, 4, 5}; - ASSERT_TRUE(net.create_sparse_array(0.5f, 4, layers)); - AssertCreateAndCopy(net, 4, layers, 17, 31); + unsigned int layers[] = {2, 3, 4, 5}; + ASSERT_TRUE(net.create_sparse_array(0.5f, 4, layers)); + AssertCreateAndCopy(net, 4, layers, 17, 31); } TEST_F(FannTest, CreateSparseArrayWithMinimalConnectivity) { - unsigned int layers[] = {2, 2, 2}; - neural_net net(0.01f, 3, layers); - AssertCreateAndCopy(net, 3, layers, 8, 8); + unsigned int layers[] = {2, 2, 2}; + neural_net net(0.01f, 3, layers); + AssertCreateAndCopy(net, 3, layers, 8, 8); } TEST_F(FannTest, CreateShortcutFourLayers) { - neural_net net(SHORTCUT, 4, 2, 3, 4, 5); - unsigned int layers[] = {2, 3, 4, 5}; - AssertCreateAndCopy(net, 4, layers, 15, 83); - EXPECT_EQ(SHORTCUT, net.get_network_type()); + neural_net net(SHORTCUT, 4, 2, 3, 4, 5); + unsigned int layers[] = {2, 3, 4, 5}; + AssertCreateAndCopy(net, 4, layers, 15, 83); + EXPECT_EQ(SHORTCUT, net.get_network_type()); } TEST_F(FannTest, CreateShortcutFourLayersUsingCreateMethod) { - ASSERT_TRUE(net.create_shortcut(4, 2, 3, 4, 5)); - unsigned int layers[] = {2, 3, 4, 5}; - AssertCreateAndCopy(net, 4, layers, 15, 83); - EXPECT_EQ(SHORTCUT, net.get_network_type()); + ASSERT_TRUE(net.create_shortcut(4, 2, 3, 4, 5)); + unsigned int layers[] = {2, 3, 4, 5}; + AssertCreateAndCopy(net, 4, layers, 15, 83); + EXPECT_EQ(SHORTCUT, net.get_network_type()); } TEST_F(FannTest, CreateShortcutArrayFourLayers) { - unsigned int layers[] = {2, 3, 4, 5}; - neural_net net(SHORTCUT, 4, layers); - AssertCreateAndCopy(net, 4, layers, 15, 83); - EXPECT_EQ(SHORTCUT, net.get_network_type()); + unsigned int layers[] = {2, 3, 4, 5}; + neural_net net(SHORTCUT, 4, layers); + AssertCreateAndCopy(net, 4, layers, 15, 83); + EXPECT_EQ(SHORTCUT, net.get_network_type()); } TEST_F(FannTest, CreateShortcutArrayFourLayersUsingCreateMethod) { - unsigned int layers[] = {2, 3, 4, 5}; - ASSERT_TRUE(net.create_shortcut_array(4, layers)); - AssertCreateAndCopy(net, 4, layers, 15, 83); - EXPECT_EQ(SHORTCUT, net.get_network_type()); + unsigned int layers[] = {2, 3, 4, 5}; + ASSERT_TRUE(net.create_shortcut_array(4, layers)); + AssertCreateAndCopy(net, 4, layers, 15, 83); + EXPECT_EQ(SHORTCUT, net.get_network_type()); } TEST_F(FannTest, CreateFromFile) { - ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); - neural_net netToBeSaved(LAYER, 3, 2, 3, 4); - ASSERT_TRUE(netToBeSaved.save("tmpfile")); + ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); + neural_net netToBeSaved(LAYER, 3, 2, 3, 4); + ASSERT_TRUE(netToBeSaved.save("tmpfile")); - neural_net netToBeLoaded("tmpfile"); - unsigned int layers[] = {2, 3, 4}; - AssertCreateAndCopy(netToBeLoaded, 3, layers, 11, 25); + neural_net netToBeLoaded("tmpfile"); + unsigned int layers[] = {2, 3, 4}; + AssertCreateAndCopy(netToBeLoaded, 3, layers, 11, 25); } TEST_F(FannTest, CreateFromFileUsingCreateMethod) { - ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); - neural_net inputNet(LAYER, 3, 2, 3, 4); - ASSERT_TRUE(inputNet.save("tmpfile")); + ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); + neural_net inputNet(LAYER, 3, 2, 3, 4); + ASSERT_TRUE(inputNet.save("tmpfile")); - ASSERT_TRUE(net.create_from_file("tmpfile")); + ASSERT_TRUE(net.create_from_file("tmpfile")); - unsigned int layers[] = {2, 3, 4}; - AssertCreateAndCopy(net, 3, layers, 11, 25); + unsigned int layers[] = {2, 3, 4}; + AssertCreateAndCopy(net, 3, layers, 11, 25); } TEST_F(FannTest, RandomizeWeights) { - neural_net net(LAYER, 2, 20, 10); - net.randomize_weights(-1.0, 1.0); - AssertWeights(net, -1.0, 1.0, 0); + neural_net net(LAYER, 2, 20, 10); + net.randomize_weights(-1.0, 1.0); + AssertWeights(net, -1.0, 1.0, 0); } diff --git a/tests/fann_test.h b/tests/fann_test.h index d4e9d74..1bdb3e5 100644 --- a/tests/fann_test.h +++ b/tests/fann_test.h @@ -9,21 +9,21 @@ using namespace FANN; class FannTest : public testing::Test { -protected: - neural_net net; - training_data data; + protected: + neural_net net; + training_data data; - void AssertCreateAndCopy(neural_net &net, unsigned int numLayers, const unsigned int *layers, unsigned int neurons, - unsigned int connections); + void AssertCreateAndCopy(neural_net &net, unsigned int numLayers, const unsigned int *layers, + unsigned int neurons, unsigned int connections); - void AssertCreate(neural_net &net, unsigned int numLayers, const unsigned int *layers, - unsigned int neurons, unsigned int connections); + void AssertCreate(neural_net &net, unsigned int numLayers, const unsigned int *layers, + unsigned int neurons, unsigned int connections); - void AssertWeights(neural_net &net, fann_type min, fann_type max, fann_type avg); + void AssertWeights(neural_net &net, fann_type min, fann_type max, fann_type avg); - virtual void SetUp(); + virtual void SetUp(); - virtual void TearDown(); + virtual void TearDown(); }; #endif diff --git a/tests/fann_test_data.cpp b/tests/fann_test_data.cpp index 2b5101a..2260072 100644 --- a/tests/fann_test_data.cpp +++ b/tests/fann_test_data.cpp @@ -2,222 +2,216 @@ #include "fann_error.h" void FannTestData::SetUp() { - FannTest::SetUp(); + FannTest::SetUp(); - numData = 2; - numInput = 3; - numOutput = 1; - inputValue = 1.1; - outputValue = 2.2; + numData = 2; + numInput = 3; + numOutput = 1; + inputValue = 1.1; + outputValue = 2.2; - inputData = new fann_type *[numData]; - outputData = new fann_type *[numData]; + inputData = new fann_type *[numData]; + outputData = new fann_type *[numData]; - InitializeTrainDataStructure(numData, numInput, numOutput, inputValue, outputValue, inputData, outputData); + InitializeTrainDataStructure(numData, numInput, numOutput, inputValue, outputValue, inputData, + outputData); } void FannTestData::TearDown() { - FannTest::TearDown(); - delete(inputData); - delete(outputData); + FannTest::TearDown(); + delete (inputData); + delete (outputData); } -void FannTestData::InitializeTrainDataStructure(unsigned int numData, - unsigned int numInput, - unsigned int numOutput, - fann_type inputValue, fann_type outputValue, - fann_type **inputData, +void FannTestData::InitializeTrainDataStructure(unsigned int numData, unsigned int numInput, + unsigned int numOutput, fann_type inputValue, + fann_type outputValue, fann_type **inputData, fann_type **outputData) { - for (unsigned int i = 0; i < numData; i++) { - inputData[i] = new fann_type[numInput]; - outputData[i] = new fann_type[numOutput]; - for (unsigned int j = 0; j < numInput; j++) - inputData[i][j] = inputValue; - for (unsigned int j = 0; j < numOutput; j++) - outputData[i][j] = outputValue; - } + for (unsigned int i = 0; i < numData; i++) { + inputData[i] = new fann_type[numInput]; + outputData[i] = new fann_type[numOutput]; + for (unsigned int j = 0; j < numInput; j++) inputData[i][j] = inputValue; + for (unsigned int j = 0; j < numOutput; j++) outputData[i][j] = outputValue; + } } -void FannTestData::AssertTrainData(training_data &trainingData, unsigned int numData, unsigned int numInput, - unsigned int numOutput, fann_type inputValue, fann_type outputValue) { - EXPECT_EQ(numData, trainingData.length_train_data()); - EXPECT_EQ(numInput, trainingData.num_input_train_data()); - EXPECT_EQ(numOutput, trainingData.num_output_train_data()); +void FannTestData::AssertTrainData(training_data &trainingData, unsigned int numData, + unsigned int numInput, unsigned int numOutput, + fann_type inputValue, fann_type outputValue) { + EXPECT_EQ(numData, trainingData.length_train_data()); + EXPECT_EQ(numInput, trainingData.num_input_train_data()); + EXPECT_EQ(numOutput, trainingData.num_output_train_data()); - for (int i = 0; i < numData; i++) { - for (int j = 0; j < numInput; j++) - EXPECT_DOUBLE_EQ(inputValue, trainingData.get_input()[i][j]); - for (int j = 0; j < numOutput; j++) - EXPECT_DOUBLE_EQ(outputValue, trainingData.get_output()[i][j]); - } + for (int i = 0; i < numData; i++) { + for (int j = 0; j < numInput; j++) EXPECT_DOUBLE_EQ(inputValue, trainingData.get_input()[i][j]); + for (int j = 0; j < numOutput; j++) + EXPECT_DOUBLE_EQ(outputValue, trainingData.get_output()[i][j]); + } } - TEST_F(FannTestData, CreateTrainDataFromPointerArrays) { - data.set_train_data(numData, numInput, inputData, numOutput, outputData); + data.set_train_data(numData, numInput, inputData, numOutput, outputData); - AssertTrainData(data, numData, numInput, numOutput, inputValue, outputValue); + AssertTrainData(data, numData, numInput, numOutput, inputValue, outputValue); } TEST_F(FannTestData, CreateTrainDataFromArrays) { - fann_type input[] = {inputValue, inputValue, inputValue, inputValue, inputValue, inputValue}; - fann_type output[] = {outputValue, outputValue}; - data.set_train_data(numData, numInput, input, numOutput, output); + fann_type input[] = {inputValue, inputValue, inputValue, inputValue, inputValue, inputValue}; + fann_type output[] = {outputValue, outputValue}; + data.set_train_data(numData, numInput, input, numOutput, output); - AssertTrainData(data, numData, numInput, numOutput, inputValue, outputValue); + AssertTrainData(data, numData, numInput, numOutput, inputValue, outputValue); } TEST_F(FannTestData, CreateTrainDataFromCopy) { - data.set_train_data(numData, numInput, inputData, numOutput, outputData); - training_data dataCopy(data); + data.set_train_data(numData, numInput, inputData, numOutput, outputData); + training_data dataCopy(data); - AssertTrainData(dataCopy, numData, numInput, numOutput, inputValue, outputValue); + AssertTrainData(dataCopy, numData, numInput, numOutput, inputValue, outputValue); } TEST_F(FannTestData, CreateTrainDataFromFile) { - data.set_train_data(numData, numInput, inputData, numOutput, outputData); - data.save_train("tmpFile"); - training_data dataCopy; - dataCopy.read_train_from_file("tmpFile"); + data.set_train_data(numData, numInput, inputData, numOutput, outputData); + data.save_train("tmpFile"); + training_data dataCopy; + dataCopy.read_train_from_file("tmpFile"); - AssertTrainData(dataCopy, numData, numInput, numOutput, inputValue, outputValue); + AssertTrainData(dataCopy, numData, numInput, numOutput, inputValue, outputValue); } -void callBack(unsigned int pos, unsigned int numInput, unsigned int numOutput, fann_type *input, fann_type *output) { - for(unsigned int i = 0; i < numInput; i++) - input[i] = (fann_type) 1.2; - for(unsigned int i = 0; i < numOutput; i++) - output[i] = (fann_type) 2.3; +void callBack(unsigned int pos, unsigned int numInput, unsigned int numOutput, fann_type *input, + fann_type *output) { + for (unsigned int i = 0; i < numInput; i++) input[i] = (fann_type)1.2; + for (unsigned int i = 0; i < numOutput; i++) output[i] = (fann_type)2.3; } TEST_F(FannTestData, CreateTrainDataFromCallback) { - data.create_train_from_callback(numData, numInput, numOutput, callBack); - AssertTrainData(data, numData, numInput, numOutput, 1.2, 2.3); + data.create_train_from_callback(numData, numInput, numOutput, callBack); + AssertTrainData(data, numData, numInput, numOutput, 1.2, 2.3); } TEST_F(FannTestData, ShuffleTrainData) { - //only really ensures that the data doesn't get corrupted, a more complete test would need to check - //that this was indeed a permutation of the original data - data.set_train_data(numData, numInput, inputData, numOutput, outputData); - data.shuffle_train_data(); - AssertTrainData(data, numData, numInput, numOutput, inputValue, outputValue); + // only really ensures that the data doesn't get corrupted, a more complete test would need to + // check that this was indeed a permutation of the original data + data.set_train_data(numData, numInput, inputData, numOutput, outputData); + data.shuffle_train_data(); + AssertTrainData(data, numData, numInput, numOutput, inputValue, outputValue); } TEST_F(FannTestData, MergeTrainData) { - data.set_train_data(numData, numInput, inputData, numOutput, outputData); - training_data dataCopy(data); - data.merge_train_data(dataCopy); - AssertTrainData(data, numData*2, numInput, numOutput, inputValue, outputValue); + data.set_train_data(numData, numInput, inputData, numOutput, outputData); + training_data dataCopy(data); + data.merge_train_data(dataCopy); + AssertTrainData(data, numData * 2, numInput, numOutput, inputValue, outputValue); } TEST_F(FannTestData, SubsetTrainData) { - data.set_train_data(numData, numInput, inputData, numOutput, outputData); - //call merge 2 times to get 8 data samples - data.merge_train_data(data); - data.merge_train_data(data); + data.set_train_data(numData, numInput, inputData, numOutput, outputData); + // call merge 2 times to get 8 data samples + data.merge_train_data(data); + data.merge_train_data(data); - data.subset_train_data(2, 5); + data.subset_train_data(2, 5); - AssertTrainData(data, 5, numInput, numOutput, inputValue, outputValue); + AssertTrainData(data, 5, numInput, numOutput, inputValue, outputValue); } TEST_F(FannTestData, ScaleOutputData) { - fann_type input[] = {0.0, 1.0, 0.5, 0.0, 1.0, 0.5}; - fann_type output[] = {0.0, 1.0}; - data.set_train_data(2, 3, input, 1, output); + fann_type input[] = {0.0, 1.0, 0.5, 0.0, 1.0, 0.5}; + fann_type output[] = {0.0, 1.0}; + data.set_train_data(2, 3, input, 1, output); - data.scale_output_train_data(-1.0, 2.0); + data.scale_output_train_data(-1.0, 2.0); - EXPECT_DOUBLE_EQ(0.0, data.get_min_input()); - EXPECT_DOUBLE_EQ(1.0, data.get_max_input()); - EXPECT_DOUBLE_EQ(-1.0, data.get_min_output()); - EXPECT_DOUBLE_EQ(2.0, data.get_max_output()); + EXPECT_DOUBLE_EQ(0.0, data.get_min_input()); + EXPECT_DOUBLE_EQ(1.0, data.get_max_input()); + EXPECT_DOUBLE_EQ(-1.0, data.get_min_output()); + EXPECT_DOUBLE_EQ(2.0, data.get_max_output()); } TEST_F(FannTestData, ScaleInputData) { - fann_type input[] = {0.0, 1.0, 0.5, 0.0, 1.0, 0.5}; - fann_type output[] = {0.0, 1.0}; - data.set_train_data(2, 3, input, 1, output); + fann_type input[] = {0.0, 1.0, 0.5, 0.0, 1.0, 0.5}; + fann_type output[] = {0.0, 1.0}; + data.set_train_data(2, 3, input, 1, output); - data.scale_input_train_data(-1.0, 2.0); - EXPECT_DOUBLE_EQ(-1.0, data.get_min_input()); - EXPECT_DOUBLE_EQ(2.0, data.get_max_input()); - EXPECT_DOUBLE_EQ(0.0, data.get_min_output()); - EXPECT_DOUBLE_EQ(1.0, data.get_max_output()); + data.scale_input_train_data(-1.0, 2.0); + EXPECT_DOUBLE_EQ(-1.0, data.get_min_input()); + EXPECT_DOUBLE_EQ(2.0, data.get_max_input()); + EXPECT_DOUBLE_EQ(0.0, data.get_min_output()); + EXPECT_DOUBLE_EQ(1.0, data.get_max_output()); } TEST_F(FannTestData, ScaleData) { - fann_type input[] = {0.0, 1.0, 0.5, 0.0, 1.0, 0.5}; - fann_type output[] = {0.0, 1.0}; - data.set_train_data(2, 3, input, 1, output); - - data.scale_train_data(-1.0, 2.0); + fann_type input[] = {0.0, 1.0, 0.5, 0.0, 1.0, 0.5}; + fann_type output[] = {0.0, 1.0}; + data.set_train_data(2, 3, input, 1, output); - for(unsigned int i = 0; i < 2; i++) { - fann_type *train_input = data.get_train_input(i); - EXPECT_DOUBLE_EQ(-1.0, train_input[0]); - EXPECT_DOUBLE_EQ(2.0, train_input[1]); - EXPECT_DOUBLE_EQ(0.5, train_input[2]); - } + data.scale_train_data(-1.0, 2.0); - EXPECT_DOUBLE_EQ(-1.0, data.get_train_output(0)[0]); - EXPECT_DOUBLE_EQ(2.0, data.get_train_output(1)[0]); + for (unsigned int i = 0; i < 2; i++) { + fann_type *train_input = data.get_train_input(i); + EXPECT_DOUBLE_EQ(-1.0, train_input[0]); + EXPECT_DOUBLE_EQ(2.0, train_input[1]); + EXPECT_DOUBLE_EQ(0.5, train_input[2]); + } + EXPECT_DOUBLE_EQ(-1.0, data.get_train_output(0)[0]); + EXPECT_DOUBLE_EQ(2.0, data.get_train_output(1)[0]); } TEST_F(FannTestData, ScaleDataByANN) { - // Input 0, input 1, and the output are scaled normally. Input 2 - // has a standard deviation of 0 and so is not scaled at all. - fann_type input[] = {0.0, 1.0, 0.5, 1.0, 2.0, 0.5}; - fann_type output[] = {0.0, 1.5}; - data.set_train_data(2, 3, input, 1, output); + // Input 0, input 1, and the output are scaled normally. Input 2 + // has a standard deviation of 0 and so is not scaled at all. + fann_type input[] = {0.0, 1.0, 0.5, 1.0, 2.0, 0.5}; + fann_type output[] = {0.0, 1.5}; + data.set_train_data(2, 3, input, 1, output); - neural_net net(LAYER, 2, 3, 1); - net.set_scaling_params(data, -1.0, 1.0, 0.0, 1.0); - net.scale_train(data); + neural_net net(LAYER, 2, 3, 1); + net.set_scaling_params(data, -1.0, 1.0, 0.0, 1.0); + net.scale_train(data); - EXPECT_DOUBLE_EQ(-1.0, data.get_train_input(0)[0]); - EXPECT_DOUBLE_EQ(-1.0, data.get_train_input(0)[1]); - EXPECT_DOUBLE_EQ(0.5, data.get_train_input(0)[2]); - EXPECT_DOUBLE_EQ(0.0, data.get_train_output(0)[0]); + EXPECT_DOUBLE_EQ(-1.0, data.get_train_input(0)[0]); + EXPECT_DOUBLE_EQ(-1.0, data.get_train_input(0)[1]); + EXPECT_DOUBLE_EQ(0.5, data.get_train_input(0)[2]); + EXPECT_DOUBLE_EQ(0.0, data.get_train_output(0)[0]); - EXPECT_DOUBLE_EQ(1.0, data.get_train_input(1)[0]); - EXPECT_DOUBLE_EQ(1.0, data.get_train_input(1)[1]); - EXPECT_DOUBLE_EQ(0.5, data.get_train_input(1)[2]); - EXPECT_DOUBLE_EQ(1.0, data.get_train_output(1)[0]); + EXPECT_DOUBLE_EQ(1.0, data.get_train_input(1)[0]); + EXPECT_DOUBLE_EQ(1.0, data.get_train_input(1)[1]); + EXPECT_DOUBLE_EQ(0.5, data.get_train_input(1)[2]); + EXPECT_DOUBLE_EQ(1.0, data.get_train_output(1)[0]); } TEST_F(FannTestData, ScaleDataByANNError) { - // Scaling without setting the scaling parameters should error. - fann_type input[] = {0.0, 1.0, 0.5}; - fann_type output[] = {1.5}; - - neural_net net(LAYER, 2, 3, 1); - - // Silence error messages. - net.set_error_log(nullptr); - - net.scale_input(input); - EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_SCALE_NOT_PRESENT); - EXPECT_EQ(input[0], 0.0); - EXPECT_EQ(input[1], 1.0); - EXPECT_EQ(input[2], 0.5); - - net.reset_errno(); - EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_NO_ERROR); - - net.scale_output(output); - EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_SCALE_NOT_PRESENT); - EXPECT_EQ(output[0], 1.5); - - net.reset_errno(); - EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_NO_ERROR); - - data.set_train_data(1, 3, input, 1, output); - net.scale_train(data); - EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_SCALE_NOT_PRESENT); - EXPECT_EQ(data.get_input()[0][0], 0.0); - EXPECT_EQ(data.get_input()[0][1], 1.0); - EXPECT_EQ(data.get_input()[0][2], 0.5); - EXPECT_EQ(data.get_output()[0][0], 1.5); + // Scaling without setting the scaling parameters should error. + fann_type input[] = {0.0, 1.0, 0.5}; + fann_type output[] = {1.5}; + + neural_net net(LAYER, 2, 3, 1); + + // Silence error messages. + net.set_error_log(nullptr); + + net.scale_input(input); + EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_SCALE_NOT_PRESENT); + EXPECT_EQ(input[0], 0.0); + EXPECT_EQ(input[1], 1.0); + EXPECT_EQ(input[2], 0.5); + + net.reset_errno(); + EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_NO_ERROR); + + net.scale_output(output); + EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_SCALE_NOT_PRESENT); + EXPECT_EQ(output[0], 1.5); + + net.reset_errno(); + EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_NO_ERROR); + + data.set_train_data(1, 3, input, 1, output); + net.scale_train(data); + EXPECT_EQ(net.get_errno(), fann_errno_enum::FANN_E_SCALE_NOT_PRESENT); + EXPECT_EQ(data.get_input()[0][0], 0.0); + EXPECT_EQ(data.get_input()[0][1], 1.0); + EXPECT_EQ(data.get_input()[0][2], 0.5); + EXPECT_EQ(data.get_output()[0][0], 1.5); } diff --git a/tests/fann_test_data.h b/tests/fann_test_data.h index 97e19a3..4038d1d 100644 --- a/tests/fann_test_data.h +++ b/tests/fann_test_data.h @@ -8,27 +8,28 @@ #include "fann_test.h" class FannTestData : public FannTest { -protected: - unsigned int numData; - unsigned int numInput; - unsigned int numOutput; - fann_type inputValue; - fann_type outputValue; + protected: + unsigned int numData; + unsigned int numInput; + unsigned int numOutput; + fann_type inputValue; + fann_type outputValue; - fann_type **inputData; - fann_type **outputData; + fann_type **inputData; + fann_type **outputData; - virtual void SetUp(); + virtual void SetUp(); - virtual void TearDown(); + virtual void TearDown(); - void AssertTrainData(FANN::training_data &trainingData, unsigned int numData, unsigned int numInput, - unsigned int numOutput, fann_type inputValue, fann_type outputValue); + void AssertTrainData(FANN::training_data &trainingData, unsigned int numData, + unsigned int numInput, unsigned int numOutput, fann_type inputValue, + fann_type outputValue); - - void InitializeTrainDataStructure(unsigned int numData, unsigned int numInput, unsigned int numOutput, - fann_type inputValue, fann_type outputValue, fann_type **inputData, - fann_type **outputData); + void InitializeTrainDataStructure(unsigned int numData, unsigned int numInput, + unsigned int numOutput, fann_type inputValue, + fann_type outputValue, fann_type **inputData, + fann_type **outputData); }; #endif diff --git a/tests/fann_test_train.cpp b/tests/fann_test_train.cpp index 167a209..926257d 100644 --- a/tests/fann_test_train.cpp +++ b/tests/fann_test_train.cpp @@ -2,67 +2,61 @@ using namespace std; -void FannTestTrain::SetUp() { - FannTest::SetUp(); -} - -void FannTestTrain::TearDown() { - FannTest::TearDown(); +void FannTestTrain::SetUp() { FannTest::SetUp(); } -} +void FannTestTrain::TearDown() { FannTest::TearDown(); } TEST_F(FannTestTrain, TrainOnDateSimpleXor) { - neural_net net(LAYER, 3, 2, 3, 1); + neural_net net(LAYER, 3, 2, 3, 1); - data.set_train_data(4, 2, xorInput, 1, xorOutput); - net.train_on_data(data, 100, 100, 0.001); + data.set_train_data(4, 2, xorInput, 1, xorOutput); + net.train_on_data(data, 100, 100, 0.001); - EXPECT_LT(net.get_MSE(), 0.001); - EXPECT_LT(net.test_data(data), 0.001); + EXPECT_LT(net.get_MSE(), 0.001); + EXPECT_LT(net.test_data(data), 0.001); } TEST_F(FannTestTrain, TrainOnReLUSimpleXor) { - neural_net net(LAYER, 3, 2, 3, 1); + neural_net net(LAYER, 3, 2, 3, 1); - data.set_train_data(4, 2, xorInput, 1, xorOutput); - net.set_activation_function_hidden(FANN::LINEAR_PIECE_RECT); - net.set_activation_steepness_hidden(1.0); - net.train_on_data(data, 100, 100, 0.001); + data.set_train_data(4, 2, xorInput, 1, xorOutput); + net.set_activation_function_hidden(FANN::LINEAR_PIECE_RECT); + net.set_activation_steepness_hidden(1.0); + net.train_on_data(data, 100, 100, 0.001); - EXPECT_LT(net.get_MSE(), 0.001); - EXPECT_LT(net.test_data(data), 0.001); + EXPECT_LT(net.get_MSE(), 0.001); + EXPECT_LT(net.test_data(data), 0.001); } TEST_F(FannTestTrain, TrainOnReLULeakySimpleXor) { - neural_net net(LAYER, 3, 2, 3, 1); + neural_net net(LAYER, 3, 2, 3, 1); - data.set_train_data(4, 2, xorInput, 1, xorOutput); - net.set_activation_function_hidden(FANN::LINEAR_PIECE_RECT_LEAKY); - net.set_activation_steepness_hidden(1.0); - net.train_on_data(data, 100, 100, 0.001); + data.set_train_data(4, 2, xorInput, 1, xorOutput); + net.set_activation_function_hidden(FANN::LINEAR_PIECE_RECT_LEAKY); + net.set_activation_steepness_hidden(1.0); + net.train_on_data(data, 100, 100, 0.001); - EXPECT_LT(net.get_MSE(), 0.001); - EXPECT_LT(net.test_data(data), 0.001); + EXPECT_LT(net.get_MSE(), 0.001); + EXPECT_LT(net.test_data(data), 0.001); } TEST_F(FannTestTrain, TrainSimpleIncrementalXor) { - neural_net net(LAYER, 3, 2, 3, 1); - - fann_type in_0 [] = {0.0, 0.0}; - fann_type out_0 [] = {0.0}; - fann_type in_1 [] = {1.0, 0.0}; - fann_type out_1 [] = {1.0}; - fann_type in_2 [] = {0.0, 1.0}; - fann_type out_2 [] = {1.0}; - fann_type in_3 [] = {1.0, 1.0}; - fann_type out_3 [] = {0.0}; - for(int i = 0; i < 100000; i++) { - net.train((fann_type*) in_0, (fann_type*) out_0); - net.train((fann_type*) in_1, (fann_type*) out_1); - net.train((fann_type*) in_2, (fann_type*) out_2); - net.train((fann_type*) in_3, (fann_type*) out_3); - } - - EXPECT_LT(net.get_MSE(), 0.01); + neural_net net(LAYER, 3, 2, 3, 1); + + fann_type in_0[] = {0.0, 0.0}; + fann_type out_0[] = {0.0}; + fann_type in_1[] = {1.0, 0.0}; + fann_type out_1[] = {1.0}; + fann_type in_2[] = {0.0, 1.0}; + fann_type out_2[] = {1.0}; + fann_type in_3[] = {1.0, 1.0}; + fann_type out_3[] = {0.0}; + for (int i = 0; i < 100000; i++) { + net.train((fann_type*)in_0, (fann_type*)out_0); + net.train((fann_type*)in_1, (fann_type*)out_1); + net.train((fann_type*)in_2, (fann_type*)out_2); + net.train((fann_type*)in_3, (fann_type*)out_3); + } + + EXPECT_LT(net.get_MSE(), 0.01); } - diff --git a/tests/fann_test_train.h b/tests/fann_test_train.h index 4f2e397..733d435 100644 --- a/tests/fann_test_train.h +++ b/tests/fann_test_train.h @@ -4,21 +4,13 @@ #include "fann_test.h" class FannTestTrain : public FannTest { -protected: - fann_type xorInput[8] = { - 0.0, 0.0, - 0.0, 1.0, - 1.0, 0.0, - 1.0, 1.0}; - fann_type xorOutput[4] = { - 0.0, - 1.0, - 1.0, - 0.0}; + protected: + fann_type xorInput[8] = {0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0}; + fann_type xorOutput[4] = {0.0, 1.0, 1.0, 0.0}; - virtual void SetUp(); + virtual void SetUp(); - virtual void TearDown(); + virtual void TearDown(); }; #endif