Click here to Skip to main content
15,884,176 members
Articles / Artificial Intelligence

Artificial Neural Networks made easy with the FANN library

Rate me:
Please Sign up or sign in to vote.
4.93/5 (46 votes)
28 Aug 2013CPOL24 min read 194.2K   10.6K   206  
Neural networks are typically associated with specialised applications, developed only by select groups of experts. This misconception has had a highly negative effect on its popularity. Hopefully, the FANN library will help fill this gap.
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003 Steffen Nissen (lukesky@diku.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 "fixedfann.h"

int main(int argc, char* argv[])
{
	struct fann_train_data *train_data, *test_data;
	FILE *train_out, *test_out;
	struct fann *ann;
	float train_error, test_error;
	unsigned int i, j;
	unsigned int epochs = 0;
	double total_elapsed = 0;
	char file[256];
	
	if(argc < 6){
		printf("usage %s train_file test_file train_file_out test_file_out fixed_conf_files\n", argv[0]);
		return -1;
	}

	if(strlen(argv[3]) == 1 && argv[3][0] == '-'){
		train_out = stdout;
	}else{
		train_out = fopen(argv[3], "w");
	}
	
	if(strlen(argv[4]) == 1 && argv[4][0] == '-'){
		test_out = stdout;
	}else{
		test_out = fopen(argv[4], "w");
	}

	fprintf(stderr, "Quality test of fixed point ");
	
	for(j = 5; j < argc; j++){
		ann = fann_create_from_file(argv[j]);

		sprintf(file, "%s_%d", argv[1], fann_get_decimal_point(ann));
		train_data = fann_read_train_from_file(file);

		sprintf(file, "%s_%d", argv[2], fann_get_decimal_point(ann));
		test_data = fann_read_train_from_file(file);

		fann_reset_MSE(ann);
		for(i = 0; i != train_data->num_data; i++){
			fann_test(ann, train_data->input[i], train_data->output[i]);
		}
		train_error = fann_get_MSE(ann);

		fann_reset_MSE(ann);
		for(i = 0; i != test_data->num_data; i++){
			fann_test(ann, test_data->input[i], test_data->output[i]);
		}
		test_error = fann_get_MSE(ann);

		sscanf(argv[j], "%d_%lf", &epochs, &total_elapsed);
		fprintf(train_out, "%f %.20e %d\n", total_elapsed, train_error, epochs);
		fprintf(test_out, "%f %.20e %d\n", total_elapsed, test_error, epochs);
		fprintf(stderr, ".");

		fann_destroy(ann);
	}

	fprintf(stderr, "\n");
	
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Publisher
Poland Poland
Software Developer's Journal (formerly Software 2.0) is a magazine for professional programmers and developers publishing news from the software world and practical articles presenting very interesting ready programming solutions. To read more

Comments and Discussions