1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2025-07-25 10:19:57 +02:00

mimgrating from another repo

This commit is contained in:
Xuan Sang LE
2018-09-19 15:08:49 +02:00
parent 91320521e8
commit 38bd13b46b
600 changed files with 362490 additions and 1 deletions

18
lib/ann/fann/examples/.gitignore vendored Normal file
View File

@ -0,0 +1,18 @@
cascade_train
mushroom
robot
scaling_test
scaling_train
simple_test
simple_train
steepness_train
xor_fixed.data
xor_fixed.net
xor_float.net
xor_test
xor_test_fixed
xor_train
cascade_train_debug
xor_test_debug
xor_test_fixed_debug
xor_train_debug

View File

@ -0,0 +1,116 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
int main()
{
struct fann *ann;
struct fann_train_data *train_data, *test_data;
const float desired_error = (const float)0.0;
unsigned int max_neurons = 30;
unsigned int neurons_between_reports = 1;
unsigned int bit_fail_train, bit_fail_test;
float mse_train, mse_test;
unsigned int i = 0;
fann_type *output;
fann_type steepness;
int multi = 0;
enum fann_activationfunc_enum activation;
enum fann_train_enum training_algorithm = FANN_TRAIN_RPROP;
printf("Reading data.\n");
train_data = fann_read_train_from_file("../../datasets/parity8.train");
test_data = fann_read_train_from_file("../../datasets/parity8.test");
fann_scale_train_data(train_data, -1, 1);
fann_scale_train_data(test_data, -1, 1);
printf("Creating network.\n");
ann = fann_create_shortcut(2, fann_num_input_train_data(train_data), fann_num_output_train_data(train_data));
fann_set_training_algorithm(ann, training_algorithm);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_LINEAR);
fann_set_train_error_function(ann, FANN_ERRORFUNC_LINEAR);
if(!multi)
{
/*steepness = 0.5;*/
steepness = 1;
fann_set_cascade_activation_steepnesses(ann, &steepness, 1);
/*activation = FANN_SIN_SYMMETRIC;*/
activation = FANN_SIGMOID_SYMMETRIC;
fann_set_cascade_activation_functions(ann, &activation, 1);
fann_set_cascade_num_candidate_groups(ann, 8);
}
if(training_algorithm == FANN_TRAIN_QUICKPROP)
{
fann_set_learning_rate(ann, 0.35f);
fann_randomize_weights(ann, -2.0f, 2.0f);
}
fann_set_bit_fail_limit(ann, (fann_type)0.9);
fann_set_train_stop_function(ann, FANN_STOPFUNC_BIT);
fann_print_parameters(ann);
fann_save(ann, "cascade_train2.net");
printf("Training network.\n");
fann_cascadetrain_on_data(ann, train_data, max_neurons, neurons_between_reports, desired_error);
fann_print_connections(ann);
mse_train = fann_test_data(ann, train_data);
bit_fail_train = fann_get_bit_fail(ann);
mse_test = fann_test_data(ann, test_data);
bit_fail_test = fann_get_bit_fail(ann);
printf("\nTrain error: %f, Train bit-fail: %d, Test error: %f, Test bit-fail: %d\n\n",
mse_train, bit_fail_train, mse_test, bit_fail_test);
for(i = 0; i < train_data->num_data; i++)
{
output = fann_run(ann, train_data->input[i]);
if((train_data->output[i][0] >= 0 && output[0] <= 0) ||
(train_data->output[i][0] <= 0 && output[0] >= 0))
{
printf("ERROR: %f does not match %f\n", train_data->output[i][0], output[0]);
}
}
printf("Saving network.\n");
fann_save(ann, "cascade_train.net");
printf("Cleaning up.\n");
fann_destroy_train(train_data);
fann_destroy_train(test_data);
fann_destroy(ann);
return 0;
}

View File

@ -0,0 +1,59 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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, MA02111-1307USA
*/
#include <stdio.h>
#include "fann.h"
int main()
{
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 96;
const float desired_error = (const float) 0.001;
struct fann *ann;
struct fann_train_data *train_data, *test_data;
float momentum;
train_data = fann_read_train_from_file("../../datasets/robot.train");
test_data = fann_read_train_from_file("../../datasets/robot.test");
for ( momentum = 0.0f; momentum < 0.7f; momentum += 0.1f )
{
printf("============= momentum = %f =============\n", momentum);
ann = fann_create_standard(num_layers,
train_data->num_input, num_neurons_hidden, train_data->num_output);
fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL);
fann_set_learning_momentum(ann, momentum);
fann_train_on_data(ann, train_data, 2000, 500, desired_error);
printf("MSE error on train data: %f\n", fann_test_data(ann, train_data));
printf("MSE error on test data : %f\n", fann_test_data(ann, test_data));
fann_destroy(ann);
}
fann_destroy_train(train_data);
fann_destroy_train(test_data);
return 0;
}

View File

@ -0,0 +1,74 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
int main()
{
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 32;
const float desired_error = (const float) 0.0001;
const unsigned int max_epochs = 300;
const unsigned int epochs_between_reports = 10;
struct fann *ann;
struct fann_train_data *train_data, *test_data;
unsigned int i = 0;
printf("Creating network.\n");
train_data = fann_read_train_from_file("../../datasets/mushroom.train");
ann = fann_create_standard(num_layers,
train_data->num_input, num_neurons_hidden, train_data->num_output);
printf("Training network.\n");
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID);
/*fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL); */
fann_train_on_data(ann, train_data, max_epochs, epochs_between_reports, desired_error);
printf("Testing network.\n");
test_data = fann_read_train_from_file("../../datasets/mushroom.test");
fann_reset_MSE(ann);
for(i = 0; i < fann_length_train_data(test_data); i++)
{
fann_test(ann, test_data->input[i], test_data->output[i]);
}
printf("MSE error on test data: %f\n", fann_get_MSE(ann));
printf("Saving network.\n");
fann_save(ann, "mushroom_float.net");
printf("Cleaning up.\n");
fann_destroy_train(train_data);
fann_destroy_train(test_data);
fann_destroy(ann);
return 0;
}

View File

@ -0,0 +1,54 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
#include "parallel_fann.h"
int main(int argc, const char* argv[])
{
const unsigned int max_epochs = 1000;
unsigned int num_threads = 1;
struct fann_train_data *data;
struct fann *ann;
long before;
float error;
unsigned int i;
if(argc == 2)
num_threads = atoi(argv[1]);
data = fann_read_train_from_file("../../datasets/mushroom.train");
ann = fann_create_standard(3, fann_num_input_train_data(data), 32, fann_num_output_train_data(data));
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID);
before = GetTickCount();
for(i = 1; i <= max_epochs; i++)
{
error = num_threads > 1 ? fann_train_epoch_irpropm_parallel(ann, data, num_threads) : fann_train_epoch(ann, data);
printf("Epochs %8d. Current error: %.10f\n", i, error);
}
printf("ticks %d", GetTickCount()-before);
fann_destroy(ann);
fann_destroy_train(data);
return 0;
}

View File

@ -0,0 +1,69 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
int main()
{
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 96;
const float desired_error = (const float) 0.001;
struct fann *ann;
struct fann_train_data *train_data, *test_data;
unsigned int i = 0;
printf("Creating network.\n");
train_data = fann_read_train_from_file("../../datasets/robot.train");
ann = fann_create_standard(num_layers,
train_data->num_input, num_neurons_hidden, train_data->num_output);
printf("Training network.\n");
fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL);
fann_set_learning_momentum(ann, 0.4f);
fann_train_on_data(ann, train_data, 3000, 10, desired_error);
printf("Testing network.\n");
test_data = fann_read_train_from_file("../../datasets/robot.test");
fann_reset_MSE(ann);
for(i = 0; i < fann_length_train_data(test_data); i++)
{
fann_test(ann, test_data->input[i], test_data->output[i]);
}
printf("MSE error on test data: %f\n", fann_get_MSE(ann));
printf("Saving network.\n");
fann_save(ann, "robot_float.net");
printf("Cleaning up.\n");
fann_destroy_train(train_data);
fann_destroy_train(test_data);
fann_destroy(ann);
return 0;
}

View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include "fann.h"
int main( int argc, char** argv )
{
fann_type *calc_out;
unsigned int i;
int ret = 0;
struct fann *ann;
struct fann_train_data *data;
printf("Creating network.\n");
ann = fann_create_from_file("scaling.net");
if(!ann)
{
printf("Error creating ann --- ABORTING.\n");
return 0;
}
fann_print_connections(ann);
fann_print_parameters(ann);
printf("Testing network.\n");
data = fann_read_train_from_file("../../datasets/scaling.data");
for(i = 0; i < fann_length_train_data(data); i++)
{
fann_reset_MSE(ann);
fann_scale_input( ann, data->input[i] );
calc_out = fann_run( ann, data->input[i] );
fann_descale_output( ann, calc_out );
printf("Result %f original %f error %f\n",
calc_out[0], data->output[i][0],
(float) fann_abs(calc_out[0] - data->output[i][0]));
}
printf("Cleaning up.\n");
fann_destroy_train(data);
fann_destroy(ann);
return ret;
}

View File

@ -0,0 +1,33 @@
#include "fann.h"
int main( int argc, char** argv )
{
const unsigned int num_input = 3;
const unsigned int num_output = 1;
const unsigned int num_layers = 4;
const unsigned int num_neurons_hidden = 5;
const float desired_error = (const float) 0.0001;
const unsigned int max_epochs = 5000;
const unsigned int epochs_between_reports = 1000;
struct fann_train_data * data = NULL;
struct fann *ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_neurons_hidden, num_output);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_LINEAR);
fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
data = fann_read_train_from_file("../../datasets/scaling.data");
fann_set_scaling_params(
ann,
data,
-1, /* New input minimum */
1, /* New input maximum */
-1, /* New output minimum */
1); /* New output maximum */
fann_scale_train( ann, data );
fann_train_on_data(ann, data, max_epochs, epochs_between_reports, desired_error);
fann_destroy_train( data );
fann_save(ann, "scaling.net");
fann_destroy(ann);
return 0;
}

View File

@ -0,0 +1,38 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "floatfann.h"
int main()
{
fann_type *calc_out;
fann_type input[2];
struct fann *ann = fann_create_from_file("xor_float.net");
input[0] = -1;
input[1] = 1;
calc_out = fann_run(ann, input);
printf("xor test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]);
fann_destroy(ann);
return 0;
}

View File

@ -0,0 +1,44 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
int main()
{
const unsigned int num_input = 2;
const unsigned int num_output = 1;
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 3;
const float desired_error = (const float) 0.001;
const unsigned int max_epochs = 500000;
const unsigned int epochs_between_reports = 1000;
struct fann *ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_train_on_file(ann, "xor.data", max_epochs, epochs_between_reports, desired_error);
fann_save(ann, "xor_float.net");
fann_destroy(ann);
return 0;
}

View File

@ -0,0 +1,116 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
#include <stdio.h>
void train_on_steepness_file(struct fann *ann, char *filename,
unsigned int max_epochs, unsigned int epochs_between_reports,
float desired_error, float steepness_start,
float steepness_step, float steepness_end)
{
float error;
unsigned int i;
struct fann_train_data *data = fann_read_train_from_file(filename);
if(epochs_between_reports)
{
printf("Max epochs %8d. Desired error: %.10f\n", max_epochs, desired_error);
}
fann_set_activation_steepness_hidden(ann, steepness_start);
fann_set_activation_steepness_output(ann, steepness_start);
for(i = 1; i <= max_epochs; i++)
{
/* train */
error = fann_train_epoch(ann, data);
/* print current output */
if(epochs_between_reports &&
(i % epochs_between_reports == 0 || i == max_epochs || i == 1 || error < desired_error))
{
printf("Epochs %8d. Current error: %.10f\n", i, error);
}
if(error < desired_error)
{
steepness_start += steepness_step;
if(steepness_start <= steepness_end)
{
printf("Steepness: %f\n", steepness_start);
fann_set_activation_steepness_hidden(ann, steepness_start);
fann_set_activation_steepness_output(ann, steepness_start);
}
else
{
break;
}
}
}
fann_destroy_train(data);
}
int main()
{
const unsigned int num_input = 2;
const unsigned int num_output = 1;
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 3;
const float desired_error = (const float) 0.001;
const unsigned int max_epochs = 500000;
const unsigned int epochs_between_reports = 1000;
unsigned int i;
fann_type *calc_out;
struct fann_train_data *data;
struct fann *ann = fann_create_standard(num_layers,
num_input, num_neurons_hidden, num_output);
data = fann_read_train_from_file("xor.data");
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_training_algorithm(ann, FANN_TRAIN_QUICKPROP);
train_on_steepness_file(ann, "xor.data", max_epochs,
epochs_between_reports, desired_error, (float) 1.0, (float) 0.1,
(float) 20.0);
fann_set_activation_function_hidden(ann, FANN_THRESHOLD_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_THRESHOLD_SYMMETRIC);
for(i = 0; i != fann_length_train_data(data); i++)
{
calc_out = fann_run(ann, data->input[i]);
printf("XOR test (%f, %f) -> %f, should be %f, difference=%f\n",
data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0],
(float) fann_abs(calc_out[0] - data->output[i][0]));
}
fann_save(ann, "xor_float.net");
fann_destroy(ann);
fann_destroy_train(data);
return 0;
}

View File

@ -0,0 +1,9 @@
4 2 1
-1 -1
-1
-1 1
1
1 -1
1
1 1
-1

View File

@ -0,0 +1,152 @@
/*
*
* Fast Artificial Neural Network (fann) C++ Wrapper Sample
*
* C++ wrapper XOR sample with functionality similar to xor_train.c
*
* Copyright (C) 2004-2006 created by freegoldbar (at) yahoo dot com
*
* This wrapper 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 wrapper 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 "floatfann.h"
#include "fann_cpp.h"
#include <ios>
#include <iostream>
#include <iomanip>
using std::cout;
using std::cerr;
using std::endl;
using std::setw;
using std::left;
using std::right;
using std::showpos;
using std::noshowpos;
// Callback function that simply prints the information to cout
int print_callback(FANN::neural_net &net, FANN::training_data &train,
unsigned int max_epochs, unsigned int epochs_between_reports,
float desired_error, unsigned int epochs, void *user_data)
{
cout << "Epochs " << setw(8) << epochs << ". "
<< "Current Error: " << left << net.get_MSE() << right << endl;
return 0;
}
// Test function that demonstrates usage of the fann C++ wrapper
void xor_test()
{
cout << endl << "XOR test started." << endl;
const float learning_rate = 0.7f;
const unsigned int num_layers = 3;
const unsigned int num_input = 2;
const unsigned int num_hidden = 3;
const unsigned int num_output = 1;
const float desired_error = 0.001f;
const unsigned int max_iterations = 300000;
const unsigned int iterations_between_reports = 1000;
cout << endl << "Creating network." << endl;
FANN::neural_net net;
net.create_standard(num_layers, num_input, num_hidden, num_output);
net.set_learning_rate(learning_rate);
net.set_activation_steepness_hidden(1.0);
net.set_activation_steepness_output(1.0);
net.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC_STEPWISE);
net.set_activation_function_output(FANN::SIGMOID_SYMMETRIC_STEPWISE);
// Set additional properties such as the training algorithm
//net.set_training_algorithm(FANN::TRAIN_QUICKPROP);
// Output network type and parameters
cout << endl << "Network Type : ";
switch (net.get_network_type())
{
case FANN::LAYER:
cout << "LAYER" << endl;
break;
case FANN::SHORTCUT:
cout << "SHORTCUT" << endl;
break;
default:
cout << "UNKNOWN" << endl;
break;
}
net.print_parameters();
cout << endl << "Training network." << endl;
FANN::training_data data;
if (data.read_train_from_file("xor.data"))
{
// Initialize and train the network with the data
net.init_weights(data);
cout << "Max Epochs " << setw(8) << max_iterations << ". "
<< "Desired Error: " << left << desired_error << right << endl;
net.set_callback(print_callback, NULL);
net.train_on_data(data, max_iterations,
iterations_between_reports, desired_error);
cout << endl << "Testing network." << endl;
for (unsigned int i = 0; i < data.length_train_data(); ++i)
{
// Run the network on the test data
fann_type *calc_out = net.run(data.get_input()[i]);
cout << "XOR test (" << showpos << data.get_input()[i][0] << ", "
<< data.get_input()[i][1] << ") -> " << *calc_out
<< ", should be " << data.get_output()[i][0] << ", "
<< "difference = " << noshowpos
<< fann_abs(*calc_out - data.get_output()[i][0]) << endl;
}
cout << endl << "Saving network." << endl;
// Save the network in floating point and fixed point
net.save("xor_float.net");
unsigned int decimal_point = net.save_to_fixed("xor_fixed.net");
data.save_train_to_fixed("xor_fixed.data", decimal_point);
cout << endl << "XOR test completed." << endl;
}
}
/* Startup function. Syncronizes C and C++ output, calls the test function
and reports any exceptions */
int main(int argc, char **argv)
{
try
{
std::ios::sync_with_stdio(); // Syncronize cout and printf output
xor_test();
}
catch (...)
{
cerr << endl << "Abnormal exception." << endl;
}
return 0;
}
/******************************************************************************/

View File

@ -0,0 +1,84 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
int main()
{
fann_type *calc_out;
unsigned int i;
int ret = 0;
struct fann *ann;
struct fann_train_data *data;
printf("Creating network.\n");
#ifdef FIXEDFANN
ann = fann_create_from_file("xor_fixed.net");
#else
ann = fann_create_from_file("xor_float.net");
#endif
if(!ann)
{
printf("Error creating ann --- ABORTING.\n");
return -1;
}
fann_print_connections(ann);
fann_print_parameters(ann);
printf("Testing network.\n");
#ifdef FIXEDFANN
data = fann_read_train_from_file("xor_fixed.data");
#else
data = fann_read_train_from_file("xor.data");
#endif
for(i = 0; i < fann_length_train_data(data); i++)
{
fann_reset_MSE(ann);
calc_out = fann_test(ann, data->input[i], data->output[i]);
#ifdef FIXEDFANN
printf("XOR test (%d, %d) -> %d, should be %d, difference=%f\n",
data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0],
(float) fann_abs(calc_out[0] - data->output[i][0]) / fann_get_multiplier(ann));
if((float) fann_abs(calc_out[0] - data->output[i][0]) / fann_get_multiplier(ann) > 0.2)
{
printf("Test failed\n");
ret = -1;
}
#else
printf("XOR test (%f, %f) -> %f, should be %f, difference=%f\n",
data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0],
(float) fann_abs(calc_out[0] - data->output[i][0]));
#endif
}
printf("Cleaning up.\n");
fann_destroy_train(data);
fann_destroy(ann);
return ret;
}

View File

@ -0,0 +1,91 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
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 "fann.h"
int FANN_API test_callback(struct fann *ann, struct fann_train_data *train,
unsigned int max_epochs, unsigned int epochs_between_reports,
float desired_error, unsigned int epochs)
{
printf("Epochs %8d. MSE: %.5f. Desired-MSE: %.5f\n", epochs, fann_get_MSE(ann), desired_error);
return 0;
}
int main()
{
fann_type *calc_out;
const unsigned int num_input = 2;
const unsigned int num_output = 1;
const unsigned int num_layers = 3;
const unsigned int num_neurons_hidden = 3;
const float desired_error = (const float) 0;
const unsigned int max_epochs = 1000;
const unsigned int epochs_between_reports = 10;
struct fann *ann;
struct fann_train_data *data;
unsigned int i = 0;
unsigned int decimal_point;
printf("Creating network.\n");
ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output);
data = fann_read_train_from_file("xor.data");
fann_set_activation_steepness_hidden(ann, 1);
fann_set_activation_steepness_output(ann, 1);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_train_stop_function(ann, FANN_STOPFUNC_BIT);
fann_set_bit_fail_limit(ann, 0.01f);
fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
fann_init_weights(ann, data);
printf("Training network.\n");
fann_train_on_data(ann, data, max_epochs, epochs_between_reports, desired_error);
printf("Testing network. %f\n", fann_test_data(ann, data));
for(i = 0; i < fann_length_train_data(data); i++)
{
calc_out = fann_run(ann, data->input[i]);
printf("XOR test (%f,%f) -> %f, should be %f, difference=%f\n",
data->input[i][0], data->input[i][1], calc_out[0], data->output[i][0],
fann_abs(calc_out[0] - data->output[i][0]));
}
printf("Saving network.\n");
fann_save(ann, "xor_float.net");
decimal_point = fann_save_to_fixed(ann, "xor_fixed.net");
fann_save_train_to_fixed(data, "xor_fixed.data", decimal_point);
printf("Cleaning up.\n");
fann_destroy_train(data);
fann_destroy(ann);
return 0;
}