-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Fast Artificial Neural Network Library (fann) | ||
Copyright (C) 2003-2012 Steffen Nissen (sn@leenissen.dk) | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include "gtest/gtest.h" | ||
|
||
#include "doublefann.h" | ||
#include "fann_cpp.h" | ||
#include "fann_test.h" | ||
|
||
TEST_F(FannTest, CreateStandardThreeLayers) { | ||
ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); | ||
unsigned int layers[] = {2, 3, 4}; | ||
AssertCreateAndCopy(3, layers, 11, 25); | ||
} | ||
|
||
TEST_F(FannTest, CreateStandardFourLayers) { | ||
ASSERT_TRUE(net.create_standard(4, 2, 3, 4, 5)); | ||
unsigned int layers[] = {2, 3, 4, 5}; | ||
AssertCreateAndCopy(4, layers, 17, 50); | ||
} | ||
|
||
TEST_F(FannTest, CreateStandardFourLayersArray) { | ||
unsigned int layers[] = {2, 3, 4, 5}; | ||
ASSERT_TRUE(net.create_standard_array(4, layers)); | ||
AssertCreateAndCopy(4, layers, 17, 50); | ||
} | ||
|
||
TEST_F(FannTest, CreateSparseFourLayers) { | ||
ASSERT_TRUE(net.create_sparse(0.5f, 4, 2, 3, 4, 5)); | ||
unsigned int layers[] = {2, 3, 4, 5}; | ||
AssertCreateAndCopy(4, layers, 17, 31); | ||
} | ||
|
||
TEST_F(FannTest, CreateSparseArrayFourLayers) { | ||
unsigned int layers[] = {2, 3, 4, 5}; | ||
ASSERT_TRUE(net.create_sparse_array(0.5f, 4, layers)); | ||
AssertCreateAndCopy(4, layers, 17, 31); | ||
} | ||
|
||
TEST_F(FannTest, CreateSparseArrayWithMinimalConnectivity) { | ||
unsigned int layers[] = {2, 2, 2}; | ||
ASSERT_TRUE(net.create_sparse_array(0.01f, 3, layers)); | ||
AssertCreateAndCopy(3, layers, 8, 8); | ||
} | ||
|
||
TEST_F(FannTest, CreateShortcutFourLayers) { | ||
ASSERT_TRUE(net.create_shortcut(4, 2, 3, 4, 5)); | ||
unsigned int layers[] = {2, 3, 4, 5}; | ||
AssertCreateAndCopy(4, layers, 15, 83); | ||
EXPECT_EQ(FANN::SHORTCUT, net.get_network_type()); | ||
} | ||
|
||
TEST_F(FannTest, CreateShortcutArrayFourLayers) { | ||
unsigned int layers[] = {2, 3, 4, 5}; | ||
ASSERT_TRUE(net.create_shortcut_array(4, layers)); | ||
AssertCreateAndCopy(4, layers, 15, 83); | ||
EXPECT_EQ(FANN::SHORTCUT, net.get_network_type()); | ||
} | ||
|
||
TEST_F(FannTest, CreateFromFile) { | ||
ASSERT_TRUE(net.create_standard(3, 2, 3, 4)); | ||
ASSERT_TRUE(net.save("tmpfile")); | ||
net.destroy(); | ||
ASSERT_TRUE(net.create_from_file("tmpfile")); | ||
|
||
unsigned int layers[] = {2, 3, 4}; | ||
AssertCreateAndCopy(3, layers, 11, 25); | ||
} | ||
|
||
TEST_F(FannTest, RandomizeWeights) { | ||
net.create_standard(2, 20, 10); | ||
net.randomize_weights(-1.0, 1.0); | ||
AssertWeights(-1.0, 1.0, 0); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "fann_test.h" | ||
|
||
void FannTest::SetUp() { | ||
srand(0); | ||
fann_disable_seed_rand(); | ||
} | ||
|
||
void FannTest::TearDown() { | ||
net.destroy(); | ||
data.destroy_train(); | ||
} | ||
|
||
void FannTest::AssertCreate(FANN::neural_net net, unsigned int num_layers, unsigned int *layers, | ||
unsigned int neurons, unsigned int connections) { | ||
EXPECT_EQ(num_layers, net.get_num_layers()); | ||
EXPECT_EQ(layers[0], net.get_num_input()); | ||
EXPECT_EQ(layers[num_layers - 1], net.get_num_output()); | ||
unsigned int *layers_res = new unsigned int[num_layers]; | ||
net.get_layer_array(layers_res); | ||
for (unsigned int i = 0; i < num_layers; 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()); | ||
|
||
AssertWeights(-0.09, 0.09, 0.0); | ||
} | ||
|
||
void FannTest::AssertCreateAndCopy(unsigned int num_layers, unsigned int *layers, unsigned int neurons, | ||
unsigned int connections) { | ||
AssertCreate(net, num_layers, layers, neurons, connections); | ||
FANN::neural_net net_copy(net); | ||
AssertCreate(net_copy, num_layers, layers, neurons, connections); | ||
} | ||
|
||
void FannTest::AssertWeights(fann_type expected_min_weight, fann_type expected_max_weight, | ||
fann_type expected_avg_weight) { | ||
FANN::connection *connections = new FANN::connection[net.get_total_connections()]; | ||
net.get_connection_array(connections); | ||
|
||
fann_type min_weight = connections[0].weight; | ||
fann_type max_weight = connections[0].weight; | ||
fann_type total_weight = 0.0; | ||
for (int i = 1; i < net.get_total_connections(); ++i) { | ||
if(connections[i].weight < min_weight) | ||
min_weight = connections[i].weight; | ||
if(connections[i].weight > max_weight) | ||
max_weight = connections[i].weight; | ||
total_weight += connections[i].weight; | ||
} | ||
|
||
EXPECT_NEAR(expected_min_weight, min_weight, 0.01); | ||
EXPECT_NEAR(expected_max_weight, max_weight, 0.01); | ||
EXPECT_NEAR(expected_avg_weight, total_weight/(fann_type)net.get_total_connections(), 0.1); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef FANN_FANN_TESTFIXTURE_H | ||
#define FANN_FANN_TESTFIXTURE_H | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "doublefann.h" | ||
#include "fann_cpp.h" | ||
|
||
//ensure random generator is seeded at a known value to ensure reproducible results | ||
|
||
class FannTest : public testing::Test { | ||
protected: | ||
FANN::neural_net net; | ||
FANN::training_data data; | ||
|
||
void AssertCreateAndCopy(unsigned int num_layers, unsigned int *layers, unsigned int neurons, | ||
unsigned int connections); | ||
|
||
void AssertCreate(FANN::neural_net net, unsigned int num_layers, unsigned int *layers, | ||
unsigned int neurons, unsigned int connections); | ||
|
||
void AssertWeights(fann_type expected_min_weight, fann_type expected_max_weight, | ||
fann_type expected_avg_weight); | ||
|
||
virtual void SetUp(); | ||
|
||
virtual void TearDown(); | ||
|
||
}; | ||
|
||
#endif //FANN_FANN_TESTFIXTURE_H |
This file was deleted.