Exemple #1
0
bool Trainer::Train(const AnnData& data, const float* random_weight_limit,
                    std::size_t max_epochs, std::size_t epochs_between_reports,
                    float desired_error,
                    float* mse, std::size_t* bit_fail) {
  if (random_weight_limit == NULL)
    fann_init_weights(ann_, data.data());
  else
    fann_randomize_weights(ann_, -*random_weight_limit, *random_weight_limit);
  fann_shuffle_train_data(data.data());
  fann_train_on_data(ann_, data.data(), max_epochs, epochs_between_reports,
                     desired_error);
  return GetMseAndBitFail(ann_, &mse, &bit_fail);
}
Exemple #2
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;
}
Exemple #3
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);
}
Exemple #4
0
int main(int argc, char *argv[])
{
	struct fann_train_data *train, *test, *all;
	unsigned int i, j;
	unsigned int length, half_length;
	
	if(argc == 2)
	{
		train = fann_read_train_from_file(argv[1]);
		fann_shuffle_train_data(train);
		fann_scale_train_data(train, 0, 1);
		fann_save_train(train, argv[1]);
		return 0;
	}
	else if(argc == 3)
	{
		train = fann_read_train_from_file(argv[1]);
		test = fann_read_train_from_file(argv[2]);
		all = fann_merge_train_data(train, test);
		
		fann_shuffle_train_data(all);
		
		for(i = 0; i < train->num_data; i++)
		{
			for(j = 0; j < train->num_input; j++)
			{
				train->input[i][j] = all->input[i][j];
			}
			
			for(j = 0; j < train->num_output; j++)
			{
				train->output[i][j] = all->output[i][j];
			}
		}
		
		for(i = 0; i < test->num_data; i++)
		{
			for(j = 0; j < test->num_input; j++)
			{
				test->input[i][j] = all->input[i + train->num_data][j];
			}
			
			for(j = 0; j < test->num_output; j++)
			{
				test->output[i][j] = all->output[i + train->num_data][j];
			}
		}
		
		fann_save_train(train, argv[1]);
		fann_save_train(test, argv[2]);
	}
	else if(argc == 4)
	{
		all = fann_read_train_from_file(argv[1]);
		fann_shuffle_train_data(all);
		fann_scale_train_data(all, 0, 1);
		length = fann_length_train_data(all);
		half_length = length/2;

		train = fann_subset_train_data(all, 0, half_length);
		test = fann_subset_train_data(all, half_length, length-half_length);		

		fann_save_train(train, argv[2]);
		fann_save_train(test, argv[3]);
	}
	else
	{
		printf("usage: %s train_file\n", argv[0]);
		printf("will shuffle the data in the file.\n");
		printf("usage: %s train_file test_file\n", argv[0]);
		printf("will shuffle the data in the two files and save the new data back to them.\n");
		printf("usage: %s input_file train_file test_file\n", argv[0]);
		printf("will shuffle the data in the input_file, and split it in two files and save the new data back to them.\n\n");
		return -1;
	}
	
	
	return 0;
}
int main(int argc, char **argv){
	if(argc != 3){
		fprintf(stderr, "\nusage ann-mlp <training set> <testing set>\n");
		exit(-1);
	}
	timer tic, toc;
	struct fann *ann = NULL;
	struct fann_train_data *training_data = NULL, *testing_data = NULL;
	unsigned int fann_length_train_data_, num_input, num_output, num_neurons_hidden, fann_length_test_data;
	double trainingtime, testingtime;
	unsigned int *classified = NULL, i;
	char trainingtimefilename[256], testtimefilename[256], predictfilename[256];
	FILE *f = NULL;

	training_data = fann_read_train_from_file(argv[1]);
	testing_data = fann_read_train_from_file(argv[2]);

	if(!training_data || !testing_data )
		return -1;

	fann_length_train_data_ = fann_length_train_data(training_data);
	fann_length_test_data = fann_length_train_data(testing_data);
	num_input = fann_num_input_train_data(training_data);
	num_output = fann_num_output_train_data(training_data);
	//num_neurons_hidden = fann_length_train_data(training_data)/4;
	num_neurons_hidden = 8;
	ann = fann_create_standard(num_layers,num_input,num_neurons_hidden,num_neurons_hidden,num_output);
	fann_shuffle_train_data(training_data);
	classified = (unsigned int *)malloc(fann_length_test_data*sizeof(unsigned int));

	/*Training ***/
	gettimeofday(&tic,NULL); fann_train_on_data(ann,training_data,max_epochs,epochs_between_reports,desired_error); gettimeofday(&toc,NULL);
	trainingtime = ((toc.tv_sec-tic.tv_sec)*1000.0 + (toc.tv_usec-tic.tv_usec)*0.001)/1000.0;

	/*Testing ***/
	gettimeofday(&tic,NULL); fann_test_on_data(ann, testing_data, classified); gettimeofday(&toc,NULL);
	testingtime = ((toc.tv_sec-tic.tv_sec)*1000.0 + (toc.tv_usec-tic.tv_usec)*0.001)/1000.0;

	sprintf(trainingtimefilename,"%s.time",argv[1]);
	f = fopen(trainingtimefilename,"a");
	fprintf(f,"%f\n",(float)trainingtime);
	fclose(f);

	sprintf(testtimefilename,"%s.time",argv[2]);
	f = fopen(testtimefilename,"a");
	fprintf(f,"%f\n",(float)testingtime);
	fclose(f);

	sprintf(predictfilename,"%s.predict",argv[2]);
	f = fopen(predictfilename,"w");

	for (i = 0; i < fann_length_test_data; i++)
		fprintf(f,"%d\n",classified[i]);
	fclose(f);

	fann_destroy_train(training_data);
	fann_destroy_train(testing_data);
	fann_destroy(ann);
	free(classified);

	return 0;
}