예제 #1
0
int main() {
    cv::Mat img = util::in();
    auto vec = mat_to_vector( img );

    FANN::neural_net nn;
    nn.create_from_file( "neural.net" );
    auto result = nn.run( vec.data() );

    std::cout << "Dog index: " << result[0] << '\n';
    std::cout << "Human index: " << result[1] << '\n';
}
예제 #2
0
// Train the net with all the data
void train_net(FANN::neural_net &net,std::string oppName, std::string day, std::string type, const int num_output)
{
  

    const float desired_error = 0.001f;
    const unsigned int max_iterations = 1000;
    const unsigned int iterations_between_reports = 1000;

    std::string trainFileName="input/"+type+"_Casino_Day-"+day+"_"+oppName+"_vs_mybotisamazing.txt";
    std::string valFileName="input/"+type+"_Casino_Day-"+day+"_mybotisamazing_vs_"+oppName+".txt";
    cout << endl << "Training network." << endl;

    FANN::training_data data;
    FANN::training_data vData;
    data.read_train_from_file(trainFileName);
    vData.read_train_from_file(valFileName);

    // 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);
    clock_t start=clock();
    net.train_on_data(data, max_iterations,
        iterations_between_reports, desired_error);
    clock_t end=clock();
    
    cout<<"Runtime:"<< end-start<<endl;
    cout << endl << "Testing network." << endl;
    
    std::string oFileName="output/"+type+"_"+oppName+"_day_"+day+".txt";
    std::string actionFile="input/action_Casino_Day-"+day+"_mybotisamazing_vs_"+oppName+".txt";
    validate_net(net, oFileName,actionFile, &vData, num_output);
    /////////////////////////////////////////////////////
    net.train_on_data(vData,max_iterations,iterations_between_reports,desired_error);
    std::string oFileName2="output/"+type+"2_"+oppName+"_day_"+day+".txt";
    std::string actionFile2="input/action_Casino_Day-"+day+"_mybotisamazing_vs_"+oppName+".txt";
    validate_net(net,oFileName2,actionFile,&vData,num_output);

    // Save the network in floating point and fixed point
    std::string netFile="output/"+type+"_"+oppName+"_day_"+day+".net";
    net.save(netFile);
    //unsigned int decimal_point = net.save_to_fixed("training.net");
    //data.save_train_to_fixed("training_fixed.data", decimal_point);
	
}
예제 #3
0
// 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;
}
예제 #4
0
파일: main.cpp 프로젝트: janisozaur/obintel
// Callback function that simply prints the information to cout
int printCallback(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)
{
	qDebug() << "epoka: " << epochs << " " << "blad: " << net.get_MSE();
	//zapisac do pliku
	return 0;
}
예제 #5
0
파일: main.cpp 프로젝트: NDobrev/NNGA
// 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)
{
	ofstream &eh = *(static_cast<ofstream*>(user_data));
	eh << net.get_MSE() << " ";
	return 0;
}
예제 #6
0
void validate_net(FANN::neural_net &net,const std::string oFileName, const std::string actionFileName, FANN::training_data * vData, int num_output){

    ofstream outputFile(oFileName.c_str());
    ifstream finAction(actionFileName.c_str());
    float sumMSE=0;
    for (unsigned int i = 0; i < vData->length_train_data(); ++i)
    {
        // Run the network on the test data
      fann_type * input=vData->get_input()[i];
        fann_type *calc_out = net.run(input);
	float mse=calc_MSE(calc_out, vData->get_output()[i],num_output);
	sumMSE+=mse;
	std::string line;
	getline(finAction, line);
	outputFile<< "***********"<<mse<<endl;
	if((int)input[19]==1){
	  outputFile<<"PREFLOP"<<endl;
	}else if((int)input[20]==1){
	  outputFile<<"FLOP"<<endl;
	}else if((int)input[21]==1){
	  outputFile<<"TURN"<<endl;
	}else if((int)input[22]==1){
	  outputFile<<"RIVER"<<endl;
	}
	outputFile<<line<<endl;
	outputFile<<"nn output"<<endl;
	for(int k=0; k<num_output; k++){
	  outputFile<<calc_out[k]<<" ";
	}
	outputFile<<endl;
	for(int j=0; j<num_output;j++){
	  outputFile<<vData->get_output()[i][j]<<" ";
	}
	outputFile<<endl;
	
    }
    outputFile<<"Average MSE: "<<sumMSE/vData->length_train_data()<<endl;
    outputFile.close();
}
예제 #7
0
//Creates a neural net with 1 hidden layer
FANN::neural_net create_net(float learning_rate, unsigned int num_layers,unsigned int num_input, unsigned int num_hidden, unsigned int num_output){

    FANN::neural_net net;
    unsigned int layers[3]={num_input,num_hidden,num_output};
    net.create_standard_array(num_layers,layers);
  
    net.set_learning_rate(learning_rate);
  
    net.set_activation_steepness_hidden(.1);
    net.set_activation_steepness_output(.1);
  
    net.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC_STEPWISE);
    net.set_activation_function_output(FANN::SIGMOID_SYMMETRIC_STEPWISE);

    //net.set_training_algorithm(FANN::TRAIN_INCREMENTAL);
    // Set additional properties such as the training algorithm
    //net.set_training_algorithm(FANN::TRAIN_QUICKPROP);

    return net;
}
예제 #8
0
파일: fann_test.cpp 프로젝트: gale320/fann
void FannTest::AssertCreate(FANN::neural_net &net, unsigned int numLayers, unsigned int *layers,
                            unsigned int neurons, unsigned int connections) {
    EXPECT_EQ(numLayers, net.get_num_layers());
    EXPECT_EQ(layers[0], net.get_num_input());
    EXPECT_EQ(layers[numLayers - 1], net.get_num_output());
    unsigned int *layers_res = new unsigned int[numLayers];
    net.get_layer_array(layers_res);
    for (unsigned int i = 0; i < numLayers; 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);
}
예제 #9
0
int trainingThread::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)
{
    static int i_old = 0;
    std::stringstream log;
    log << "Epochs     " << std::setw(8) << epochs << ". "
        << "Current Error: " << std::left << net.get_MSE() << std::right << std::endl;

    //

    int i = (epochs * 100 )/ max_epochs;
    if (i != i_old )
    {
        qDebug() << "i ma : " << i << " max_epochs " << max_epochs;
        //emit updateProgressBar(i);
        static_this->updateProgressBar(i);
        static_this->updateLog(QString::fromStdString(log.str()));
        i_old = i;
    }
    return 0;

}
예제 #10
0
int main(int argc, char *argv[])
{
    if (argv[1][0] == 'r') {
        WAVFile inp(argv[2]);
        translate_wav(inp);
        return 0;
    }
    if (argc == 1 || argc % 2 != 1) {
        std::cout << "bad number of training examples\n";
        return -1;
    }

    int to_open = (argc - 1)/2;
    for (int i = 0; i < to_open; i++) {
        WAVFile inp(argv[2*i+1]);
        WAVFile out(argv[2*i+2]);
        add_training_sound(inp, out);
    }

    float *train_in[input_training.size()];
    float *train_out[output_training.size()];
    for (int i = 0; i < input_training.size(); i++) {
        train_in[i] = input_training[i];
        train_out[i] = output_training[i];
    }
    FANN::training_data training;
    training.set_train_data(input_training.size(), (samples_per_segment/2+1)*2,
            train_in, (samples_per_segment/2+1)*2, train_out);
    FANN::neural_net net; 
    const unsigned int layers[] = {(samples_per_segment/2+1)*2, (samples_per_segment/2+1)*2, (samples_per_segment/2+1)*2};
    net.create_standard_array(3, (unsigned int*)layers);
    net.set_activation_function_output(FANN::LINEAR);
    //net.set_activation_function_hidden(FANN::LINEAR);
    net.set_learning_rate(1.2f);
    net.train_on_data(training, 50000, 1, 3.0f);

    net.save("net.net");
}
예제 #11
0
void translate_wav(WAVFile input)
{
    double *in;
    fftw_complex *out;
    fftw_complex *in_back;
    double *out_back;
    fftw_plan plan;
    fftw_plan plan_back;

    int length = input.m_data_header.sub_chunk_2_size;
    in = (double*)fftw_malloc(sizeof(double)*samples_per_segment);
    out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*samples_per_segment/2+1);
    in_back = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*samples_per_segment/2+1);
    out_back = (double*)fftw_malloc(sizeof(double)*samples_per_segment+2);

    plan = fftw_plan_dft_r2c_1d(samples_per_segment, in, out, FFTW_ESTIMATE);
    plan_back = fftw_plan_dft_c2r_1d(samples_per_segment+1, in_back, out_back, FFTW_ESTIMATE);

    FANN::neural_net net;
    net.create_from_file("net.net");
    
    short outbuffer[(((length/2)/samples_per_segment)+1)*samples_per_segment];

    int sample_number = 0;
    while (sample_number < length/2) {
        for (int i = 0; i < samples_per_segment; i++) {
            if (sample_number+i >= length/2) {
                break;
            }
            in[i] = (double)input.m_data.PCM16[sample_number+i]/((double)(65536/2));
        }

        fftw_execute(plan);

        float *input_train = (float*)malloc(sizeof(float)*samples_per_segment/2+1);
        for (int i = 0; i < samples_per_segment/2+1; i++) {
            input_train[i] = out[0][i];
        }

        // Use neural net to translate voice
        std::cout << "outputting data\n";
        float *out_net = net.run(input_train);

        for (int i = 0; i < samples_per_segment/2+1; i++) {
            in_back[0][i] = out_net[i];
            in_back[1][i] = out_net[i+samples_per_segment/2+1];
            //in_back[0][i] = out[0][i];
            //in_back[1][i] = out[1][i];
        }

        memset(out_back, 0, sizeof(double)*samples_per_segment);
        fftw_execute(plan_back);

        for (int i = 0; i < samples_per_segment; i++) {
            outbuffer[sample_number+i] = ((out_back[i])/samples_per_segment)*(65536.f/2.f);
            //std::cout << ((out_back[i])/(float)samples_per_segment)*(65536.f/2.f)<<"\n";//outbuffer[sample_number+i] << "\n";
        }

        sample_number += samples_per_segment;
    }
    WAVFile output_wav(outbuffer, length, 16);
    output_wav.writeToFile("output.wav");
}
예제 #12
0
파일: main.cpp 프로젝트: NDobrev/NNGA
		void PrintBest(vector<dna>& dnas, int gen)
		{
			cout << "Best gen : "<< gen << "----------" << endl;
			for (int i = 0; i < gen; ++i)
			{
				cout << endl;
				cout << "-********************-" << endl;
				cout << "fitness" << dnas[i].fitness << endl;
				cout << "learningRate" << dnas[i].learningRate << endl;
				cout << "momentum" << dnas[i].momentum << endl;
				cout << "numberOfHidden" << dnas[i].numberOfHidden << endl;
				cout << "maxIteration" << dnas[i].maxIteration << endl;
				cout << "desiredError" << dnas[i].desiredError<< endl;
				cout << "-********************-" << endl;
				cout << endl;
			}
			FANN::neural_net net;
			net.create_standard(3, data.num_input_train_data(), dnas[0].numberOfHidden, data.num_output_train_data());
			net.set_learning_rate(dnas[0].learningRate);
			net.set_learning_momentum(dnas[0].momentum);

			net.set_activation_steepness_hidden(1.0);
			net.set_activation_steepness_output(1.0);

			net.set_activation_function_hidden(FANN::SIGMOID);
			net.set_activation_function_output(FANN::SIGMOID);
			ofstream file("ErrorHistory/" + file + ".csv");
			net.set_callback(print_callback, &file);
			net.set_error_log(NULL);
			net.train_on_data(data, dnas[0].maxIteration,
				1, dnas[0].desiredError);


			cout << "----------------------" << endl;
		}
예제 #13
0
void trainingThread::train()
{
    std::stringstream log;
    log << std::endl << " test started." << std::endl;

    const float learning_rate = netConfigPTR->learning_rate ;
    const unsigned int num_layers = netConfigPTR->num_layers;
    const unsigned int num_input = netConfigPTR->num_input;
    const unsigned int num_hidden = netConfigPTR->num_hidden;
    const unsigned int num_output = netConfigPTR->num_output;
    const float desired_error = netConfigPTR->desired_error;
    const unsigned int max_iterations = netConfigPTR->max_iterations;
    const unsigned int iterations_between_reports = netConfigPTR->iterations_between_reports;

    log << std::endl << "Creating network." << std::endl;

    FANN::neural_net net;
    if (netConfigPTR->leyersVector.size() > 1)
    {
        unsigned int vectorSize = netConfigPTR->leyersVector.size();
        unsigned int* leyers = new unsigned int[vectorSize+2];
        leyers[0] = num_input;
        for (unsigned int i = 0; i < vectorSize; ++i)
        {
            leyers[i+1] = netConfigPTR->leyersVector.at(i);
        }

        leyers[num_layers-1] = num_output;

        for ( unsigned int i = 0 ; i< vectorSize+2 ; ++i)
        {
            qDebug() << "vector size: "<< vectorSize+2<<" i:"<<i<< " leyers "<< leyers[i];
        }
        net.create_standard_array(vectorSize+2, leyers);
        //net.create_standard(vectorSize+2, leyers[0], leyers[2],leyers[3], leyers[1]);


        delete[] leyers;
    }
    else
    {
        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(netConfigPTR->trainingAlgo);

    // Output network type and parameters
    log << std::endl << "Network Type                         :  ";
    switch (net.get_network_type())
    {
    case FANN::LAYER:
        log << "LAYER" << std::endl;
        break;
    case FANN::SHORTCUT:
        log << "SHORTCUT" << std::endl;
        break;
    default:
        log << "UNKNOWN" << std::endl;
        break;
    }
    //net.print_parameters();

    log << std::endl << "Training network." << std::endl;

    FANN::training_data data;
    if (data.read_train_from_file(netConfigPTR->trainingDataPatch))
    {
        // Initialize and train the network with the data
        net.init_weights(data);

        log << "Max Epochs " << std::setw(8) << max_iterations << ". "
            << "Desired Error: " << std::left << desired_error << std::right << std::endl;
         emit updateLog(QString::fromStdString(log.str()));
        log << "dupa";
        log.str("");
        log.clear();
        net.set_callback(print_callback, nullptr);
        net.train_on_data(data, max_iterations,
                          iterations_between_reports, desired_error);

        log << std::endl << "Testing network." << std::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]);

            log << "test (";
            for (unsigned int j = 0; j < num_input; ++j)
            {
                log  << std::showpos << data.get_input()[i][j] << ", ";
                //qDebug()<< "jestem w log<<";
            }
            log <<  ") -> " ;
            for(unsigned int k = 0 ; k < num_output ; ++k)
            {
                log << calc_out[k] <<", ";
            }
            log << ",\t should be ";
            for(unsigned int k = 0 ; k < num_output ; ++k)
            {
                log << data.get_output()[i][k] <<", ";
            }
            log << std::endl ;
        }

        log << std::endl << "Saving network." << std::endl;

        // Save the network in floating point and fixed point
        net.save(netConfigPTR->netFloat);
        unsigned int decimal_point = net.save_to_fixed(netConfigPTR->netFixed);
        std::string path = netConfigPTR->trainingDataPatch.substr(0,netConfigPTR->trainingDataPatch.size()-5);
        data.save_train_to_fixed(path +"_fixed.data", decimal_point);

        log << std::endl << "test completed." << std::endl;
        emit updateLog(QString::fromStdString(log.str()));
        emit updateProgressBar(100);
    }
}
예제 #14
0
int callback_html(FANN::neural_net &rede_neural,
	 training_data &dados,
	 unsigned int turnos_max,
	 unsigned int turnos_por_amostra,
	 float margem_de_erro,
	 unsigned int turnos,
	 void *_cerebro){

	 static FILE *plot = NULL;

 	 static float total = 0;
 	 static float last_MSE = 0;
 	 static float last_bit_fail = 0;

	 static int loops = 0;
	 static int MSE_counter = 0;
	 static int bit_fail_counter = 0;
	 static float steepness = 0;
	 static float aprendizagem = 0;
	 static float last_aprendizagem = 0;
	 static float aprendizagem_inicial = 0;

	 static bool set_steepness = false;
	 static bool set_aprendizagem = false;

	 static training_data *dados_teste = NULL;

	 char buffer[20000];
	 char MSE_buffer[64];
	 char bit_fail_buffer[64];
	 float diferenca_MSE;
	 float MSE;
	 int bit_fail = 0;
	 int diferenca_bit_fail = 0;

	 CCerebro *cerebro = (CCerebro*)_cerebro;

	 if(dados_teste){
		//rede_neural.reset_MSE();
		//printf("%f ", rede_neural.get_MSE());
		MSE = rede_neural.test_data(*dados_teste);
		//printf("%f\n", MSE);
	 }
	MSE = rede_neural.get_MSE();

	 bit_fail = rede_neural.get_bit_fail();

	//rede_neural.reset_MSE();

	//printf("%f %f %d\n", MSE, last_MSE, (MSE - last_MSE) > 0.000001);
	if(last_MSE)
		diferenca_MSE = (last_MSE - MSE);
	else
		diferenca_MSE = 0;

	if(last_bit_fail)
		diferenca_bit_fail = last_bit_fail - bit_fail;
	else
		diferenca_bit_fail = 0;

	if(last_bit_fail == bit_fail){
		if(bit_fail_counter < BIT_FAIL_COUNTER)
		bit_fail_counter++;
	}else if(bit_fail_counter > 0)
		bit_fail_counter--;// = bit_fail_counter * 0.85;

	if(fabs(diferenca_MSE) < margem_de_erro){
		if(MSE_counter < MSE_COUNTER)
			MSE_counter++;
	}else if(MSE_counter > 0)
			MSE_counter--;// = MSE_counter * 0.9;

	//printf("%d\n", bit_fail_counter);
	//printf("%f %f\n",last_MSE, diferenca_MSE);
	if(last_MSE){
		fprintf(plot, "%d %f %f %d %d\n",turnos, MSE, diferenca_MSE, bit_fail_counter, MSE_counter);
		fflush(plot);
		total = total + diferenca_MSE;
	}

	last_MSE = MSE;
	last_bit_fail = bit_fail;

	if(set_aprendizagem){
		if(!aprendizagem_inicial)
			aprendizagem_inicial = cerebro->rede_neural->get_learning_rate();

		aprendizagem = (1-((float)(MSE_counter + bit_fail_counter) / 200)) * aprendizagem_inicial;
		if(!last_aprendizagem)
			last_aprendizagem = aprendizagem;

		if(last_aprendizagem > aprendizagem && !((MSE_counter + bit_fail_counter)%2)){
			rede_neural.set_learning_rate(aprendizagem);
			last_aprendizagem = aprendizagem;
		}
	}

	if(set_steepness){
		if(!steepness)
			steepness = rede_neural.get_activation_steepness(1,0);

		rede_neural.set_activation_steepness_layer(
			(1-((float)(MSE_counter + bit_fail_counter) / 100))*steepness
			,1);
			//set_sneepness = true;
	}

	loops++;
	if(turnos > 1){
		if(loops < LOOPS && (bit_fail_counter < BIT_FAIL_COUNTER || MSE_counter < MSE_COUNTER) )
			return 0;
	}else{
		bit_fail_counter = 0;
		MSE_counter = 0;
		last_aprendizagem = 0;

		if(dados_teste)
			free(dados_teste);

		if(plot)
			fclose(plot);
/*
		dados_teste = new training_data();
		if(cerebro->getDadosIdentificadosEscalados()){
			dados_teste->set_train_data(cerebro->num_dados_identificados,
					cerebro->num_caracteristicas, cerebro->dados_identificados_escalados,
					cerebro->num_caracteristicas, cerebro->dados_identificados_escalados);
			printf("Utilizando massa de testes identificada com %d casos.\n",cerebro->num_dados_identificados );
		}else if(cerebro->getDadosEscalados()){
			dados_teste->set_train_data(cerebro->num_dados,
						cerebro->num_caracteristicas, cerebro->dados_escalados,
						cerebro->num_caracteristicas, cerebro->dados_escalados);
			printf("Utilizando massa de testes não identificada com %d casos.\n",cerebro->num_dados );
		}else{
			printf("Sem dados para treinar.\n");
			delete dados_teste;

			return -1;
		}
*/
		printf("Plotando em %s.\n",cerebro->plot_file);
		plot = fopen(cerebro->plot_file, "w");
	}
	loops = 0;

	memset(buffer,0,20000);

	//clear_screen();

	cor_nivel(bit_fail_counter,bit_fail_buffer);
    cor_nivel(MSE_counter,MSE_buffer);

	//printf("%f\n",round((float)bit_fail_counter/10));
	sprintf((char*)(buffer + strlen(buffer)), "%08d turnos, precisão %1.8f(%-1.8f)(%s%%), nivel de adaptacao %d(%d)(%s%%)",
											  turnos, MSE, diferenca_MSE, MSE_buffer, bit_fail, diferenca_bit_fail, bit_fail_buffer);
	sprintf((char*)(buffer + strlen(buffer)),", depuracao %-1.6f, aprendizagem %f, passo %f", total,last_aprendizagem,steepness);
	sprintf((char*)(buffer + strlen(buffer)),"\n");
	printf(buffer);
	//textcolor(RESET, WHITE, BLACK);
	//rede_neural.print_connections(buffer);
	//printf(buffer);


	if(bit_fail_counter == BIT_FAIL_COUNTER && MSE_counter == MSE_COUNTER){
			printf("Treino completado em %08d, adaptacao %d, MSE %f\n",turnos, bit_fail, MSE);
			return -1;
		}

	return 0;
}
예제 #15
0
파일: main.cpp 프로젝트: NDobrev/NNGA
		void NextEpoch(vector<dna>& dnas)
		{
			size_t cnt = dnas.size();
			#pragma omp parallel
			#pragma omp for
			for (short it = 0; it < cnt; ++it)
			{
				
				FANN::neural_net net;
				net.create_standard(3, data.num_input_train_data(), dnas[it].numberOfHidden, data.num_output_train_data());
				net.set_learning_rate(dnas[it].learningRate);
				net.set_learning_momentum(dnas[it].momentum);

				net.set_activation_steepness_hidden(1.0);
				net.set_activation_steepness_output(1.0);

				net.set_activation_function_hidden(FANN::LINEAR);
				net.set_activation_function_output(FANN::LINEAR);
				net.set_callback(print_fake, NULL);
				net.set_error_log(NULL);
				net.train_on_data(data, dnas[it].maxIteration,
					-1, dnas[it].desiredError);

				
				dnas[it].fitness = (1. - net.test_data(testdata))  ;
			}
		}
예제 #16
0
void neuralNetworkTraining(std::string training_data_file)
{
    /*
     * Parameters for create_standard method.
     *
     * num_layers             : The total number of layers including the input and the output layer.
     * num_input_neurons      : The number of neurons in the input layer.
     * num_hidden_one_neurons : The number of neurons in the first hidden layer.
     * num_hidden_two_neurons : The number of neurons in the second hidden layer.
     * num_output_neurons     : The number of neurons in the output layer.
     */
    const unsigned int num_layers = 3;
    const unsigned int num_input_neurons = 8;
    const unsigned int num_hidden_neurons = 5;
    const unsigned int num_output_neurons = 1;

    /*
     * Parameters for train_on_data method.
     *
     * desired_errors         : The desired get_MSE or get_bit_fail, depending on which stop function is chosen by set_train_stop_function.
     * max_epochs             : The maximum number of epochs the training should continue.
     * epochs_between_reports : The number of epochs between printing a status report to stdout. A value of zero means no reports should be printed.
     */
    const float desired_error = DESIRED_ERROR;
    const unsigned int max_epochs = MAX_EPOCHS;
    const unsigned int epochs_between_reports = EPOCHS_BETWEEN_REPORTS;

    FANN::neural_net net;
    // Create a standard fully connected backpropagation neural network.
    net.create_standard(num_layers, num_input_neurons, num_hidden_neurons, num_output_neurons);

    net.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC_STEPWISE); // Set the activation function for all of the hidden layers.
    net.set_activation_function_output(FANN::SIGMOID_SYMMETRIC_STEPWISE); // Set the activation function for the output layer.
    net.set_training_algorithm(FANN::TRAIN_RPROP);                        // Set the training algorithm.
    net.randomize_weights(-INIT_EPSILON, INIT_EPSILON);                   // Give each connection a random weight between -INIT_EPSILON and INIT_EPSILON.

    std::cout << std::endl << "Network Type                         :  ";
    switch (net.get_network_type())
    {
    case FANN::LAYER:
        std::cout << "LAYER" << std::endl;
        break;
    case FANN::SHORTCUT:
        std::cout << "SHORTCUT" << std::endl;
        break;
    default:
        std::cout << "UNKNOWN" << std::endl;
        break;
    }
    net.print_parameters();

    std::cout << std::endl << "Training Network." << std::endl;
    FANN::training_data data;
    if (data.read_train_from_file(training_data_file))
    {
        std::cout << "Max Epochs: " << std::setw(8) << max_epochs << ". " << "Desired Error: " << std::left << desired_error << std::right << std::endl;

        net.set_callback(printCallback, NULL);                                      // Sets the callback function for use during training.
        net.train_on_data(data, max_epochs, epochs_between_reports, desired_error); // Trains on an entire dataset, for a period of time.

        std::cout << "Saving Network." << std::endl;
        net.save("neural_network_controller_float.net");
        unsigned int decimal_point = net.save_to_fixed("neural_network_controller_fixed.net");
        data.save_train_to_fixed("neural_network_controller_fixed.data", decimal_point);
    }
}
예제 #17
0
파일: main.cpp 프로젝트: janisozaur/obintel
void train(Configuration *cfg)
{
	QString fileName(QDir::homePath() + "/" + QCoreApplication::applicationName() + ".ini");
	qDebug() << "using config file:" << fileName;
	QSettings settings(fileName, QSettings::IniFormat);
	const float learningRate = settings.value("learningRate", 0.8).toFloat();
	const unsigned int numLayers = settings.value("numLayers", 3).toInt();
	const unsigned int numInput = settings.value("numInput", 1024).toInt();
	const unsigned int numHidden = settings.value("numHidden", 32).toInt();
	const unsigned int numOutput = settings.value("numOutput", 1).toInt();
	const float desiredError = settings.value("desiredError", 0.0001f).toFloat();
	const unsigned int maxIterations = settings.value("maxIterations", 3000).toInt();
	const unsigned int iterationsBetweenReports = settings.value("iterationsBetweenReports", 100).toInt();

	FANN::neural_net net;
	net.create_standard(numLayers, numInput, numHidden, numOutput);
	net.set_learning_rate(learningRate);
	net.set_activation_steepness_hidden(0.5);
	net.set_activation_steepness_output(0.5);
	net.set_learning_momentum(0.6);
	net.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC);
	net.set_activation_function_output(FANN::SIGMOID_SYMMETRIC);
	net.set_training_algorithm(FANN::TRAIN_RPROP);
	net.print_parameters();

	FANN::training_data data;
	if (data.read_train_from_file(cfg->getDataSavePath().toStdString()))
	{
		qDebug() << "Wczytano dane";
		//inicjalizacja wag
		net.init_weights(data);
		data.shuffle_train_data();
		net.set_callback(printCallback, NULL);
		net.train_on_data(data, maxIterations,
			iterationsBetweenReports, desiredError);
		net.save(cfg->getNetSavePath().toStdString());
		qDebug() << "Nauczono i zapisano siec";
	}
}