Пример #1
0
int main(int argc, char *argv[])
{
	/* parameters */
	char output_file[256];

	if(argc != 12)
	{
		printf("usage %s train_file test_file output_file width height topology neighborhood decay max_training_examples seconds_between_reports number_of_runs\n", argv[0]);
		return -1;
	}

	unsigned int width = atoi(argv[4]);
	unsigned int height = atoi(argv[5]);
	unsigned int max_training_examples = atoi(argv[9]);
	double seconds_between_reports = atof(argv[10]);
	unsigned int number_of_runs = atoi(argv[11]);

	fann_train_data *train_data = fann_read_train_from_file(argv[1]);
	fann_train_data *test_data = fann_read_train_from_file(argv[2]);

	char *train_out_file = argv[3];
	FILE *train_out = 0;

	fann_seed_rand();

	for(unsigned int i = 0; i < number_of_runs; i++)
	{
	  /*		test_collector->newCollection();
	  		train_collector->newCollection();
	  */
		
		if(strlen(train_out_file) == 1 && train_out_file[0] == '-')
			train_out = stdout;
		else
		{
			if(number_of_runs == 1)
				train_out = fopen(train_out_file, "w");
			else
			{
				sprintf(output_file, "%s_%d_run", train_out_file, i);
				train_out = fopen(output_file, "w");
			}
		}
	
		fprintf(stdout, "Quality test of %s\n", argv[1]);
	
		quality_benchmark_fann_som(width, height, argv[6], argv[7], argv[8], train_data, test_data,
					   train_out, train_data->num_input, max_training_examples, seconds_between_reports);
		
		
		fclose(train_out);
	}

	fann_destroy_train(train_data);
	fann_destroy_train(test_data);

	return 0;
}
Пример #2
0
void cunit_xor_test(void)
{
	fann_type *calc_out = NULL;
	unsigned int i;
	int ret = 0;

	struct fann *ann = NULL;
	struct fann_train_data *data = NULL;

#ifdef FIXEDFANN
	ann = fann_create_from_file("xor_fixed.net");
#else
	ann = fann_create_from_file("xor_float.net");
#endif

	CU_ASSERT_PTR_NOT_NULL_FATAL(ann);

#ifdef FIXEDFANN
	data = fann_read_train_from_file("xor_fixed.data");
#else
	data = fann_read_train_from_file("xor.data");
#endif

	CU_ASSERT_PTR_NOT_NULL_FATAL(data);

	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]);

		CU_ASSERT_PTR_NOT_NULL_FATAL(calc_out);

#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)
		{
			CU_FAIL("XOR test failed.");
			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
	}

	fann_destroy_train(data);
	fann_destroy(ann);
}
Пример #3
0
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_STEPWISE);
	fann_set_activation_function_output(ann, FANN_SIGMOID_STEPWISE);

	/*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;
}
void NeuralNet::trainNet(char* ptrDataFileName){
    const unsigned int maxEpochs = 1000;
    const float desiredError = 0;
    this->ptrData = fann_read_train_from_file(ptrDataFileName);
    const unsigned int epochsBetweenReports = 10;
    fann_train_on_data(this->ptrNeuralNet, this->ptrData, maxEpochs, epochsBetweenReports, desiredError);
}
Пример #5
0
int main()
{

    const unsigned int num_input = 1;
    const unsigned int num_output = 1;
    const unsigned int num_layers = 3;
    const unsigned int num_neurons_hidden = 10;
    const float desired_error = (const float) 0.0003;
    const unsigned int max_epochs = 50000;
    const unsigned int epochs_between_reports = 1000;
    struct fann_train_data 	*train_data;

    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);
	
	train_data=fann_read_train_from_file("seno.data");

	//AQUI
	//fann_set_scaling_params(ann,train_data,-1.0,1.0,-1.0,1.0);
	//fann_scale_train(ann,train_data);
	//fann_scale_train_data(train_data,-1,1);	

    fann_train_on_data(ann, train_data, max_epochs, epochs_between_reports, desired_error);

    fann_save(ann, "mlp2.net");

    fann_destroy(ann);

    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 = 9;
	const float desired_error = (const float) 0;
	const unsigned int max_epochs = 500000;
	const unsigned int epochs_between_reports = 1000;
	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("osyslec_train.data");

	fann_set_activation_steepness_hidden(ann, 1);
	fann_set_activation_steepness_output(ann, 1);

	fann_set_activation_function_hidden(ann, FANN_SIGMOID);
	fann_set_activation_function_output(ann, FANN_SIGMOID);

	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("GG 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, "osyslec_train_float.net");

	decimal_point = fann_save_to_fixed(ann, "osyslec_train_fixed.net");
	fann_save_train_to_fixed(data, "osyslec_train_fixed.data", decimal_point);

	printf("Cleaning up.\n");
	fann_destroy_train(data);
	fann_destroy(ann);

	return 0;
}
Пример #7
0
int main() {
	printf("Neural Network training\n");

	const unsigned int num_input = 3;
    const unsigned int num_output = 4;
    const unsigned int num_layers = 4;
    const unsigned int num_neurons_hidden = 30;
    const float desired_error = (const float) 0.02;
    const unsigned int max_epochs = 500000;
    const unsigned int epochs_between_reports = 10;

    struct fann *ann = fann_create_standard(
		num_layers,
    	num_input,
    	num_neurons_hidden,
        num_neurons_hidden,
    	// num_neurons_hidden,
    	num_output);

    fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
    fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
    // fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
    fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);

    struct fann_train_data *data = fann_read_train_from_file("../training.data");

    fann_train_on_data(ann, data, max_epochs, epochs_between_reports, desired_error);
    fann_destroy_train(data);

    fann_save(ann, "../neural.net");

    fann_destroy(ann);

	return 0;
}
Пример #8
0
int 
main(int argc, char **argv)
{
	const unsigned int num_input = 360;
	const unsigned int num_output = 1;
	const unsigned int num_layers = 4;
	const unsigned int num_neurons_hidden = num_input / 2;
	const float desired_error = (const float) 0.001;
	const unsigned int max_epochs = 300;
	const unsigned int epochs_between_reports = 10;
	struct fann_train_data * data = NULL;
	struct fann *ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_neurons_hidden / 2, num_output);
	unsigned int i, j;

	if (argc != 2)
	{
		fprintf(stderr, "Use: %s arquivoTreino\n", argv[0]); 
		exit(1);
	}

	fann_set_activation_function_hidden(ann, FANN_ELLIOT);
	fann_set_activation_function_output(ann, FANN_LINEAR);
	fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
	fann_set_learning_rate(ann, 0.1);
	fann_set_learning_momentum(ann, 0.6);
	data = fann_read_train_from_file(argv[1]);

	fann_train_on_data(ann, data, max_epochs, epochs_between_reports, desired_error);

	free(data);
	fann_save(ann, ARQ_RNA);
	fann_destroy(ann);

	return 0;
}
Пример #9
0
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("xor_float.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("5K.txt");
	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;
}
Пример #10
0
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;
}
Пример #11
0
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;
}
Пример #12
0
int main()
{
	struct fann *ann;
	struct fann_train_data *train_data, *test_data;
	const float desired_error = (const float) 0.001;
	unsigned int max_neurons = 40;
	unsigned int neurons_between_reports = 1;

	printf("Reading data.\n");

	train_data = fann_read_train_from_file("../benchmarks/datasets/two-spiral.train");
	test_data = fann_read_train_from_file("../benchmarks/datasets/two-spiral.test");

	fann_scale_train_data(train_data, 0, 1);
	fann_scale_train_data(test_data, 0, 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, FANN_TRAIN_RPROP);
	fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
	fann_set_activation_function_output(ann, FANN_LINEAR_PIECE);
	fann_set_train_error_function(ann, FANN_ERRORFUNC_LINEAR);

	fann_print_parameters(ann);

	printf("Training network.\n");

	fann_cascadetrain_on_data(ann, train_data, max_neurons, neurons_between_reports, desired_error);

	fann_print_connections(ann);

	printf("\nTrain error: %f, Test error: %f\n\n", fann_test_data(ann, train_data),
		   fann_test_data(ann, test_data));

	printf("Saving network.\n");

	fann_save(ann, "two_spirali.net");

	printf("Cleaning up.\n");
	fann_destroy_train(train_data);
	fann_destroy_train(test_data);
	fann_destroy(ann);

	return 0;
}
Пример #13
0
/*

Creer le meilleur fichier .net (apprentissage) possible
basé sur des tests effectués au cours de l'apprentissage
en fonction du nombre de neuronnes cachés choisis en entrée.

*/
void train(struct fann *ann, char* trainFile, char *testFile, char *netFile , unsigned int max_epochs, unsigned int epochs_between_reports, float desired_error, const unsigned int num_neurons_hidden) {
        
	struct fann_train_data *trainData, *testData;
	struct fann *annBest = fann_copy(ann);
	float error;
	unsigned int i;
	char buffer[1024];
	float testError = 1;
	float testErrorBest = 1;

	trainData = fann_read_train_from_file(trainFile);
	testData = fann_read_train_from_file(testFile);
	
        	for(i = 1; i <= max_epochs; i++){ 

  		fann_shuffle_train_data(trainData); //melange les données
		error = fann_train_epoch(ann, trainData); //fait une epoque, ann : le réseaux créer, erreur : l'erreur d'apprentissage
               
		//Toute les 5 epoques 
		if(i % epochs_between_reports == 0 || error < desired_error){
                        	
			fann_test_data(ann,testData);// teste le reseau sur les donnée de test
	
			testError = fann_get_MSE(ann);
			if (testError < testErrorBest) {
				testErrorBest = testError;
				annBest = fann_copy(ann);
				printf("Epochs     %8d; trainError : %f; testError : %f;\n", i, error,testError);
				sprintf(buffer,"%s_%u_%d.net",netFile,num_neurons_hidden,i);
				fann_save(annBest, buffer);
			}		
                	}
		if(error < desired_error){
			break;
		}
	}
	sprintf(buffer,"%s_%u.net",netFile,num_neurons_hidden);
	fann_save(annBest, buffer);
	fann_destroy_train(trainData);
	fann_destroy_train(testData);
}
Пример #14
0
int 
main(int argc, char **argv)
{
	fann_type *calc_out;
	unsigned int i, j;
	struct fann *ann;
	struct fann_train_data *data;
    float error = 0.0;

	if (argc < 3) 
	{
		fprintf(stderr, "Use: %s FANN_network.net patternsFile\n", argv[0]); 
		exit(1);
	}

	printf("Openning ANN `%s'\n", argv[1]);
	ann = fann_create_from_file(argv[1]);

	if (!ann)
	{
		fprintf(stderr, "Error creating the ANN.\n"); 
		return (1); 
	}

	printf("Running ANN.\n");

	data = fann_read_train_from_file(argv[2]);

	for(i = 0; i < fann_length_train_data(data); i++)
	{

		calc_out = fann_run(ann, data->input[i]);
        
		printf("ANN: %f ", calc_out[0]);
		printf("Expected: %f ", data->output[i][0]);
		printf("Error: %f ", (float) (data->output[i][0] -calc_out[0]));
        printf("Throttle_Effort: %f Brake_Effort: %f Current_Velocity: %f\n",
                // essa multiplicacao ocorre para desfazer o que o 'gerarEntrada.c' fez
                data->input[i][360-3]*100.0, data->input[i][360-2]*100.0, data->input[i][360-1]*5.0);
        error += (float) powf(fann_abs(calc_out[0] - data->output[i][0]),2);
	}

    printf("Test:: Squared Error: %f Mean Squared Error: %f\n", error, error/(fann_length_train_data(data)-1));

	printf("Cleaning memory.\n");
	fann_destroy_train(data);
	fann_destroy(ann);

	return (0);
}
Пример #15
0
FANN_EXTERNAL void FANN_API fann_train_on_file(struct fann *ann, const char *filename,
											   unsigned int max_epochs,
											   unsigned int epochs_between_reports,
											   float desired_error)
{
	struct fann_train_data *data = fann_read_train_from_file(filename);

	if(data == NULL)
	{
		return;
	}
	fann_train_on_data(ann, data, max_epochs, epochs_between_reports, desired_error);
	fann_destroy_train(data);
}
void NeuralNet::runNet(char* ptrDataFileName){
    struct fann_train_data *ptrDataTest = fann_read_train_from_file(ptrDataFileName);
    fann_reset_MSE(this->ptrNeuralNet);
    fann_test_data(this->ptrNeuralNet, ptrDataTest);
    printf("Mean Square Error: %f\n", fann_get_MSE(this->ptrNeuralNet));

    fann_type *calc_out;
    for(int i = 0; i < fann_length_train_data(ptrDataTest); i++){
        calc_out = fann_run(this->ptrNeuralNet, ptrDataTest->input[i]);
        cout << "Sample testing:  "<< calc_out[0] << " " << ptrDataTest->output[i][0] << " " << fann_abs(calc_out[0] - ptrDataTest->output[i][0]) << endl;
    }


    fann_destroy_train(ptrDataTest);
}
Пример #17
0
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("../benchmarks/datasets/robot.train");
	test_data = fann_read_train_from_file("../benchmarks/datasets/robot.test");

	for ( momentum = 0.0; momentum < 0.7; momentum += 0.1 )
	{
		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;
}
Пример #18
0
int main(int argc , char **argv) {
  fann_type *calc_out;
  int i;
	
  if (argc==1) {
    printf("usage : eval testFile.txt network.net\n");
    return 0;
  }

  struct fann_train_data *testData = fann_read_train_from_file(argv[1]);
  struct fann *ann = ann = fann_create_from_file(argv[2]);
  int output_size = fann_get_num_output(ann);

	

  for(i=0;i<testData->num_data ;i++) {
    // calcul du résultat de la prédiction
    calc_out = fann_run(ann, testData->input[i]);
    // interprétation et affichage du résultat

	switch (maxIndex(calc_out,output_size)){
		case 0 :
			printf ("fr\n");
			break;
		case 1 :
			printf ("en\n");
			break;
		case 2 :
			printf ("de\n");
			break;
		case 3 :
			printf ("es\n");
			break;
		case 4 :
			printf ("pt\n");
			break;
		case 5 :
			printf ("it\n");
			break;
		case 6 :
			printf ("tr\n");
			break;
	}
  }        
  fann_destroy(ann);
  return 0;
}
Пример #19
0
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;
}
Пример #20
0
int 
main(int argc, char **argv)
{
	fann_type *calc_out;
	unsigned int i, j;
	struct fann *ann;
	struct fann_train_data *data;

	if (argc < 2) 
	{
		fprintf(stderr, "Use: %s arquivoTeste\n", argv[0]); 
		exit(1);
	}

	printf("Abrindo a Rede `%s'\n", ARQ_RNA);
	ann = fann_create_from_file(ARQ_RNA);

	if (!ann)
	{
		fprintf(stderr, "Erro criando a RNA.\n"); 
		return (1); 
	}

	//fann_print_connections(ann);
	//fann_print_parameters(ann);

	printf("Testando a RNA.\n");

	data = fann_read_train_from_file(argv[1]);

	for(i = 0; i < fann_length_train_data(data); i++)
	{
		fann_reset_MSE(ann);

		calc_out = fann_run(ann, data->input[i]);

		printf("Resultado: %f ", calc_out[0]);
		printf("Original: %f " , data->output[i][0]);
		printf("Erro: %f\n"    , (float) fann_abs(calc_out[0] - data->output[i][0]));
	}
	printf("Limpando memoria.\n");
	fann_destroy_train(data);
	fann_destroy(ann);

	return (0);
}
Пример #21
0
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);
}
Пример #22
0
int main(int argc, char *argv[])
{
	int i = 0;
	const unsigned int width = 10;
	const unsigned int height = 10;
	const unsigned int num_dimensions = 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_som(width, height, num_dimensions);

	ann->som_params->som_topology = FANN_SOM_TOPOLOGY_HEXAGONAL;
	ann->som_params->som_neighborhood = FANN_SOM_NEIGHBORHOOD_DISTANCE;
	ann->som_params->som_learning_decay	= FANN_SOM_LEARNING_DECAY_LINEAR;
	ann->som_params->som_learning_rate = 0.01;
	ann->som_params->som_learning_rate_constant = 0.01;

	/* Example using init_weights */
	struct fann_train_data *data = fann_read_train_from_file("som_train.data");

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-init") == 0)
			fann_init_weights_som(ann, data);
		else if (strcmp(argv[i], "-epoch") == 0) {
			fann_train_epoch(ann, data);
			return EXIT_SUCCESS;
		}
	}

	/* Example without init_weights (random initialization)*/
	fann_train_on_file(ann, "som_train.data", max_epochs, epochs_between_reports, desired_error);

	/*fann_save(ann, "som_train.net"); */

	fann_destroy(ann);

	printf("\n");
	return 0;
}
Пример #23
0
int main()
{
	const float desired_error = (const float) 0.0001;
	const unsigned int max_epochs = 350;
	const unsigned int epochs_between_reports = 25;
	struct fann *ann;
	struct fann_train_data *train_data;

	unsigned int i = 0;

	printf("Creating network.\n");

	train_data = fann_read_train_from_file("ann_training_data");
    // Using incremental training -> shuffle training data    
    fann_shuffle_train_data(train_data);

//	ann = fann_create_standard(num_layers,
//					  train_data->num_input, num_neurons_hidden, train_data->num_output);

    //ann = fann_create_standard(500, 2, 50, 50, 21);
    unsigned int layers[4] = {150, 70, 30, 22};
    ann = fann_create_standard_array(4, layers);
	printf("Training network.\n");

	fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC_STEPWISE);
	fann_set_activation_function_output(ann, FANN_LINEAR_PIECE); //FANN_SIGMOID_STEPWISE);

    fann_set_training_algorithm(ann, FANN_TRAIN_INCREMENTAL);

	fann_train_on_data(ann, train_data, max_epochs, epochs_between_reports, desired_error);

	printf("Saving network.\n");

	fann_save(ann, "mymoves_gestures.net");

	printf("Cleaning up.\n");
	fann_destroy_train(train_data);
	fann_destroy(ann);

	return 0;
}
Пример #24
0
/*! fann.read_train_from_file(filename)
 *# Creates a training object by reading a training data file.
 *x train = fann.read_train_from_file("xor.data")
 *-
 */
static int ann_read_train_from_file(lua_State *L)
{
	struct fann_train_data **train;
	const char *fname;

	luaL_argcheck(L, lua_isstring(L,1), 1, "Argument to fann.open_file() must be a string");

	fname = lua_tostring(L, 1);
#ifdef FANN_VERBOSE
	printf("Opening training data from file '%s'\n", fname);
#endif

	train = lua_newuserdata(L, sizeof *train);

	luaL_getmetatable(L, FANN_TRAIN_METATABLE);
	lua_setmetatable(L, -2);

	*train = fann_read_train_from_file(fname);
	if(!*train)
		luaL_error(L, "Unable to read train data from %s", fname);

	return 1;
}
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("digitde_validation_fixed.net");
#else
	ann = fann_create_from_file("digitde_validation_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("digitde_validation_fixed.data");
#else
	data = fann_read_train_from_file("digitde_validation.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("GG 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("GG 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;
}
Пример #26
0
int main ( int argc, char **argv )
{
    srand(time(NULL));
    if ( argc<=1 )
    {
        //      printf ( "neuro num\r\n" );
        //     exit ( 0 );
    }

    if (argc>2)
    {
        //desired_error=atof(argv[2]);
        numn=atoi(argv[1]);
        l1n=atoi(argv[2]);
        if (argc>3)
            l2n=atoi(argv[3]);
        if (argc>4)
            l3n=atoi(argv[4]);
        if (argc>5)
            l4n=atoi(argv[5]);
        if (argc>6)
            l5n=atoi(argv[6]);
        if (argc>7)
            l6n=atoi(argv[7]);
    }

    signal ( 2, sig_term );



    srand ( time ( NULL ) );

    printf("loading training data...");

    train_data = fann_read_train_from_file ( "train.dat" );
    test_data = fann_read_train_from_file ( "test.dat" );

    weight_data=fann_merge_train_data(train_data,test_data);

    cln_weight_data=fann_duplicate_train_data(weight_data);
    cln_test_data=fann_duplicate_train_data(test_data);
    cln_train_data=fann_duplicate_train_data(train_data);

    //num_neurons_hidden = atoi ( argv[1] );



    srand(time(NULL));

    y=atoi(argv[2]);

    lay=atoi(argv[1]);
    ln=lay+2;
    if (lay==1)
        y2=train_data->num_output;
    best_perc=1;

    printf("\r\ndoing %ux%u [layers=%u,out=%u]",lay,y,ln, train_data->num_output);
    while (true)

    {
        neur1=1+(rand()%y);
        neur2=1+(rand()%y);
        conn_rate=0.5f+((rand()%50)*0.01f);
        printf("\r\n%2dx%-4d: ",neur1,neur2);
        //  printf("create network: layers=%d l1n=%d l2n=%d l3n=%d l4n=%d\ l5n=%d l6n=%dr\n",numn,l1n,l2n,l3n,l4n,l5n,l6n);
        ann = fann_create_standard (//conn_rate,
                  ln,
                  train_data->num_input,
                  neur1,
                  neur2,
                  train_data->num_output );
        //fann_init_weights ( ann, train_data );
        printf(" [%p] ",ann);

        if ( ( int ) ann==NULL )
        {
            printf ( "error" );
            exit ( 0 );
        }



        fann_set_activation_function_hidden(ann,FANN_SIGMOID);
        fann_set_activation_function_output(ann,FANN_SIGMOID);

        rebuild_functions(neur1);
        fann_set_training_algorithm ( ann, FANN_TRAIN_RPROP );
        fann_set_sarprop_temperature(ann,15000.0f);
        //fann_randomize_weights ( ann, -((rand()%10)*0.1f), ((rand()%10)*0.1f) );
        fann_init_weights(ann,train_data);
        got_inc=0;
        prev_epoch_mse=1;
        //
        epochs=0;

        unsigned last_best_perc_epoch=0;
        unsigned last_sync_epoch=0;
        unsigned last_ftest_secs=0;

        last_sync_epoch=0;
        last_best_perc_epoch=0;
        if (good_ann)
            fann_destroy(good_ann);

        good_ann=fann_copy(ann);
        unlink(histfile);
        for (u=0;u<1000;u++)
        {
            fflush(NULL);
            train_mse=fann_train_epoch(ann, train_data);

            if (jitter_train)
                apply_jjit(train_data,cln_train_data);


            if (time(NULL)-last_ftest_secs>=1)
            {
                //printf("\r\n%5u %9.6f %5.2f ",epochs,train_mse,test_perc);
                //printf(" %4.2f",test_perc);
                printf(".");


                last_ftest_secs=time(NULL);
            }
            ftest_data();
            plot(epochs,train_mse,test_mse);

            /*         if (epochs>10&&((int)test_perc==43||(int)test_perc==57))
                    {
                        printf(" [excluded %.2f] ",test_perc);
                        break;
                    }            else            {

                    } */
            //printf("excluded %f ",test_perc);

            double prev_test_perc;
            //   if (prev_epoch_mse==best_perc)
            //  printf("o");
            if ((int)test_perc>(int)train_perc&&epochs-last_stat_epoch>10)
            {
                fann_destroy(good_ann);
                good_ann=fann_copy(ann);

                if (test_perc!=prev_test_perc)
                    printf("%.2f [%f]",test_perc,train_mse);

                //printf(" sync[%4.2f]",test_perc);
                last_stat_epoch=epochs;
            }
            else 	if (epochs-last_sync_epoch>111500)
            {
                last_sync_epoch=epochs;
            }
            if (epochs>210&&test_perc>best_perc)
            {
                //	u--;
                //  fann_destroy(good_ann);
                //   good_ann=fann_copy(ann);
                printf(" [saved best %.0f] ",test_perc);
                last_stat_epoch=epochs;
                //		printf("%f",test_perc);
                //	fann_destroy(ann);
                //	ann=fann_copy(good_ann);
                fann_save(ann,"mutate-best.net");
                best_perc=test_perc;
                printf(" %6.2f [%f]",test_perc,train_mse);
                last_best_perc_epoch=epochs;

            }
            else     if (epochs>11100&&((int)test_perc<=63||(int)test_perc==(int)prev_test_perc))
            {
                //best_perc=test_perc;
                //		printf("x");
                //  printf(".");
                //printf("\r%6.8f",train_mse);
                //			printf("done\r\n");
                break;


            }
            static unsigned last_restore_epoch=0;
            if (epochs>100&&test_mse-train_mse>=0.25f&&epochs-last_restore_epoch>=120)
            {
                /* 	fann_set_learning_rate ( ann,0.31f+(rand()%90)*0.01f);
                	fann_set_learning_momentum(ann,(rand()%90)*0.01f);
                	printf(" [restored @ %u lr %.2f mm %.2f]",epochs,fann_get_learning_rate(ann),
                	fann_get_learning_momentum(ann));
                	fann_destroy(ann);
                	ann=fann_copy(good_ann);
                	last_stat_epoch=epochs;
                	last_restore_epoch=epochs; */





                double rdec,rinc;
                rdec=0.0101f+((rand()%100)*0.00001f);
                if (!rdec)
                    rdec=0.01f;
                rinc=1.0001f+((rand()%90)*0.00001f);
                if (!rinc)
                    rinc=1.1f;
                static double prev_test_epoch_mse;

                //		rinc+=diff_mse*0.000001f;
                //			fann_set_rprop_increase_factor(ann,rinc );
                //	fann_set_rprop_decrease_factor(ann, rdec);
            }
            else if (test_mse-train_mse<=0.1f)
            {
                fann_destroy(good_ann);
                good_ann=fann_copy(ann);
                //	printf("s");
            }
            else
            {
                fann_set_sarprop_temperature(ann,fann_get_sarprop_temperature(ann)-0.0001f);
            }
            static unsigned last_train_change_epoch=0;
            if (test_mse>=train_mse&&epochs-last_train_change_epoch>=100)
            {
                last_train_change_epoch=epochs;
                //fann_set_training_algorithm(ann,FANN_TRAIN_SARPROP);
                jitter_train=0;
            }
            else
            {
                //fann_set_training_algorithm(ann,FANN_TRAIN_RPROP);
                jitter_train=0;
            }

            got_inc=test_perc-prev_epoch_mse;
            prev_epoch_mse=test_perc;
            prev_test_perc=test_perc;
            epochs++;
            if (epochs-last_best_perc_epoch>511500)
            {
                printf(" failed");
                break;
            }
            if (epochs>2200&&(int)train_perc<40)
            {
                printf("skip 1\r\n");
                break;
            }

            if ((int)test_perc>=80)
            {
                printf("\r\ngot it %f\r\n",test_perc);
                fann_save(ann,"good.net");
                exit(0);
            }
            // printf("\n%6u ",epochs);
        }
        printf(" %6.2f inc: %.2f",test_perc,got_inc);
//            printf("%6.2f %6.2f",train_perc,test_perc);

        fann_destroy ( ann );

    }

    fann_destroy_train ( train_data );
    fann_destroy_train ( test_data );
    fann_destroy ( ann );

    return 0;
}
Пример #27
0
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("../benchmarks/datasets/parity8.train");
	test_data = fann_read_train_from_file("../benchmarks/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.35);
		fann_randomize_weights(ann, -2.0,2.0);
	}
	
	fann_set_bit_fail_limit(ann, 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;
}
Пример #28
0
int main() 

{ 

 fann_type *calc_out; 

 const unsigned int num_input = 22500; 

 const unsigned int num_output = 1; 

 //const unsigned int num_layers = 4; 
 const unsigned int num_layers = 4; 

 /* this value can be changed to tweak the network */ 

 const unsigned int num_neurons_hidden = 50; 
 //const unsigned int num_neurons_hidden = 150; 

const float desired_error = (const float) 0.02; 

 const unsigned int max_epochs = 15000; 
const unsigned int epochs_between_reports = 20; 

 float learning_rate = .5; 

 struct fann *ann; 

 struct fann_train_data *data; 

 int num_neurons = 0; 

 unsigned int i = 0; 

 unsigned int decimal_point; 

 /* CREATING NETWORK */ 

ann = fann_create_standard(num_layers, num_input, 

 num_neurons_hidden, 

 num_neurons_hidden, num_output); 

 /* reading training data */ 

 data = fann_read_train_from_file("training.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_init_weights(ann, data); 

 /* 

 TRAINING NETWORK 

 run x epochs at learn rate .y 

 */ 

 //fann_set_learning_rate(ann, .7); 

 //fann_train_on_data(ann, data, 200, epochs_between_reports, .4); 
 //fann_train_on_data(ann, data, 500, epochs_between_reports, .002); 

 fann_set_learning_rate(ann, .5); 

 fann_train_on_data(ann, data,5000, epochs_between_reports, .2); 
 //fann_train_on_data(ann, data,50, epochs_between_reports, .2); 

 fann_set_learning_rate(ann, .2); 

 fann_train_on_data(ann, data,1000, epochs_between_reports, .15); 
 //fann_train_on_data(ann, data,100, epochs_between_reports, .15); 

 fann_set_learning_rate(ann, .1); 

 fann_train_on_data(ann, data,5000, epochs_between_reports, .002); 
 //fann_train_on_data(ann, data,200, epochs_between_reports, .00002); 

 /* TESTING NETWORK */ 

 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("%f, should be %f, difference=%f\n", 

 calc_out[0], data->output[i][0], 

 fann_abs(calc_out[0] - data->output[i][0])); */

 } 

 /* SAVING NETWORK */ 

 fann_save(ann, "image_spam.net"); 

 /* CLEANING UP */ 

 fann_destroy_train(data);
fann_destroy(ann); 

 return 0; 

}
Пример #29
0
int main() {

	printf("Reading XML.. .. ..\n");
	ezxml_t f1 = ezxml_parse_file("test.xml"), classification, temp, algo, temp2;
	 
	classification = ezxml_child(f1, "classification");
	temp = ezxml_child(classification, "algorithm");
	algo = ezxml_child(temp, "MultiLayerPerceptron");

	const unsigned int num_input = atoi(ezxml_child(classification, "input")->txt);
	const unsigned int num_output = atoi(ezxml_child(classification, "output")->txt);
	const unsigned int num_layers = atoi(ezxml_child(classification, "numberOfLayers")->txt);
	const unsigned int num_neurons_hidden = atoi(ezxml_child(algo, "hiddenNeurons")->txt);
	const float desired_error = (const float) (atof(ezxml_child(algo, "desiredError")->txt));
	const unsigned int max_epochs = atoi(ezxml_child(algo, "maxEpochs")->txt);
	const unsigned int epochs_between_reports = atoi(ezxml_child(algo, "epochsBetweenReports")->txt);

	fann_type *calc_out;
	
	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(ezxml_child(classification, "datafile")->txt);

	fann_set_activation_steepness_hidden(ann, atoi(ezxml_child(algo, "hiddenActivationSteepness")->txt));
	fann_set_activation_steepness_output(ann, atoi(ezxml_child(algo, "outputActivationSteepness")->txt));

	fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
	fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
	
	temp2 = ezxml_child(algo, "trainStopFuction");
	const char *stopFunc = temp2->txt;
	if(stopFunc == "FANN_STOPFUNC_BIT"){
		fann_set_train_stop_function(ann, FANN_STOPFUNC_BIT);
	} else {
		fann_set_train_stop_function(ann, FANN_STOPFUNC_MSE);
	}
	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("Test Results (%f,%f,%f) -> %f, should be %f, difference=%f\n",
			   data->input[i][0], data->input[i][1], data->input[i][2], 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);

	ezxml_free(f1);

	return 0;
}
Пример #30
0
/*
arguments (all required):
 - data filename
 - topology, as number of neurons per layer separated by dashes
 - epochs (integer)
 - learning rate (0.0-1.0 float)
 - output filename
*/
int main(int argc, char **argv)
{
    // Argument 1: data filename.
    const char *datafn = argv[1];

    // Argument 2: topology.
    unsigned int layer_sizes[MAX_LAYERS];
    unsigned int num_layers = 0;
    char *token = strtok(argv[2], "-");
    while (token != NULL) {
        layer_sizes[num_layers] = atoi(token);
        ++num_layers;
        token = strtok(NULL, "-");
    }

    // Argument 3: epoch count.
    unsigned int max_epochs = atoi(argv[3]);

    // Argument 4: learning rate.
    float learning_rate = atof(argv[4]);

    // Argument 5: output filename.
    const char *outfn = argv[5];

    struct fann *ann;
	ann = fann_create_standard_array(num_layers, layer_sizes);

    // Misc parameters.
    fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
	fann_set_activation_steepness_hidden(ann, 0.5);
	fann_set_activation_steepness_output(ann, 0.5);
	fann_set_activation_function_hidden(ann, FANN_SIGMOID);
	fann_set_activation_function_output(ann, FANN_SIGMOID);
    //fann_set_train_stop_function(ann, FANN_STOPFUNC_BIT);
    //fann_set_bit_fail_limit(ann, 0.01f);

    struct fann_train_data *data;
    data = fann_read_train_from_file(datafn);
	fann_init_weights(ann, data);
	
    fann_set_learning_rate(ann, learning_rate);
	fann_train_on_data(
        ann,
        data,
        max_epochs,
        10,  // epochs between reports
        DESIRED_ERROR
    );

	printf("Testing network. %f\n", fann_test_data(ann, data));

    fann_type *calc_out;
	for(unsigned int i = 0; i < fann_length_train_data(data); ++i)
	{
		calc_out = fann_run(ann, data->input[i]);
	}
	
	printf("RMSE = %f\n", sqrt(fann_get_MSE(ann)));

	fann_save(ann, outfn);

	fann_destroy_train(data);
	fann_destroy(ann);

	return 0;
}