예제 #1
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))  ;
			}
		}
예제 #2
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;
}