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; }
void CNeuroNetwok::Init(const int nNeirons, const int nResults, const int nEpochs) { const unsigned int nLayersCount = 3; const unsigned int nHiddenNeironsCount = 3; m_nEpochsCount = nEpochs; // Создаем нейросеть // Количество входных нейронов столько же, сколько и входных параметров // Выходных нейронов столько же, сколько и результатов. m_pANN = fann_create_standard(nLayersCount, nNeirons, nHiddenNeironsCount, nResults); if (!m_pANN) throw std::runtime_error("Failed to init fann!"); // Выполняем очень важные инициализации :) fann_set_activation_steepness_hidden(m_pANN, 1); fann_set_activation_steepness_output(m_pANN, 1); fann_set_activation_function_hidden(m_pANN, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(m_pANN, FANN_SIGMOID_SYMMETRIC); fann_set_train_stop_function(m_pANN, FANN_STOPFUNC_BIT); fann_set_bit_fail_limit(m_pANN, 0.01f); m_bIsInited = true; }
NeuralNet::NeuralNet(){ cout << "Initializing neural network" << endl; this->numInputNeurons = 144; this->numOutputNeurons = 1; this->numLayers = 3; this->numHiddenNeurons = 140; this->ptrNeuralNet = fann_create_standard(numLayers, numInputNeurons, numHiddenNeurons, numOutputNeurons); fann_set_activation_steepness_hidden(this->ptrNeuralNet, 1); fann_set_activation_steepness_output(this->ptrNeuralNet, 1); //sigmoidal function fann_set_activation_function_hidden(this->ptrNeuralNet, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(this->ptrNeuralNet, FANN_SIGMOID_SYMMETRIC); fann_set_train_stop_function(this->ptrNeuralNet, FANN_STOPFUNC_BIT); fann_set_bit_fail_limit(this->ptrNeuralNet, 0.01f); fann_set_training_algorithm(this->ptrNeuralNet, FANN_TRAIN_RPROP); }
/*! ann:set_bit_fail_limit(limit) *# Sets the bit fail limit for training the neural net. *x ann:set_bit_fail_limit(0.01) *- */ static int ann_set_bit_fail_limit(lua_State *L) { struct fann **ann; fann_type limit; ann = luaL_checkudata(L, 1, FANN_METATABLE); luaL_argcheck(L, ann != NULL, 1, "'neural net' expected"); if(lua_gettop(L) < 2) luaL_error(L, "insufficient parameters"); limit = lua_tonumber(L, 2); #ifdef FANN_VERBOSE printf("Setting bit fail limit to %f\n", limit); #endif fann_set_bit_fail_limit(*ann, limit); return 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; }
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; }
void Trainer::set_bit_fail_limit(float bit_fail_limit) { std::cerr << "Set bit fail limit to " << bit_fail_limit << std::endl; fann_set_bit_fail_limit(ann_, bit_fail_limit); }
int main(int argc, char **argv) { if(argc < 3) { printf("Usage: train_net <input.train> <output.net>\n"); exit(-1); } const unsigned int num_input = 2; const unsigned int num_output = 1; const unsigned int num_layers = 3; const unsigned int num_neurons_hidden = 8; const float desired_error = (const float) 0.000042; const unsigned int max_epochs = 500000; const unsigned int epochs_between_reports = 1000; 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); // 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_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_train_on_file(ann, argv[1], max_epochs, epochs_between_reports, desired_error); struct fann_train_data *data; data = fann_read_train_from_file(argv[1]); fann_init_weights(ann, data); printf("Training network on data from %s.\n", argv[1]); fann_train_on_data(ann, data, max_epochs, epochs_between_reports, desired_error); printf("Testing network. %f\n", fann_test_data(ann, data)); double error, errorSum = 0; unsigned int i = 0, size = fann_length_train_data(data); fann_type *calc_out; for(i = 0; i < size; i++) { calc_out = fann_run(ann, data->input[i]); error = fann_abs(calc_out[0] - data->output[i][0]) * 1000; printf("Distance test (%d dBm,%f%%) -> %f meters, should be %f meters, difference=%f meters\n", (int)(data->input[i][0] * 150 - 150), data->input[i][1], calc_out[0] * 1000, data->output[i][0] * 1000, error); errorSum += error; } printf("Average Error: %f\n", errorSum / size); fann_save(ann, argv[2]); fann_destroy(ann); return 0; }
struct fann * setup_net(struct fann_train_data * data) { struct fann *ann; #if MIMO_FANN #if OPTIMIZE == 0 ann = fann_create_standard( 3, data->num_input, H_DIM, data->num_output); fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC); #endif #if OPTIMIZE == 1 unsigned int i, j; struct fann_descr *descr=(struct fann_descr*) calloc(1, sizeof(struct fann_descr)); fann_setup_descr(descr, 2, data->num_input); i=0; fann_setup_layer_descr( &(descr->layers_descr[i]), "connected_any_any", 1, NULL ); for (j=0; j< descr->layers_descr[i].num_neurons; j++) { fann_setup_neuron_descr( descr->layers_descr[i].neurons_descr+j, H_DIM, "scalar_rprop_sigmoid_symmetric", NULL ); } i=1; fann_setup_layer_descr( &(descr->layers_descr[i]), "connected_any_any", 1, NULL ); for (j=0; j< descr->layers_descr[i].num_neurons; j++) { fann_setup_neuron_descr( descr->layers_descr[i].neurons_descr+j, data->num_output, "scalar_rprop_sigmoid_symmetric", NULL ); } ann = fann_create_from_descr( descr ); #endif #if OPTIMIZE >= 2 { unsigned int layers[] = { data->num_input, H_DIM, data->num_output }; /*char *type; asprintf(&type, "%s_%s_%s", vals(implementation), vals(algorithm), vals(activation));*/ ann = fann_create_standard_array_typed(layer_type, neuron_type, 3, layers); } #endif #else /*MIMO_FANN*/ #ifdef SPARSE ann = fann_create_sparse( SPARSE, 3, data->num_input, H_DIM, data->num_output); #else ann = fann_create_standard( 3, data->num_input, H_DIM, data->num_output); #endif fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC); fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC); #endif /*MIMO_FANN*/ fann_set_train_stop_function(ann, FANN_STOPFUNC_BIT); fann_set_bit_fail_limit(ann, 0.01f); fann_set_activation_steepness_hidden(ann, 1); fann_set_activation_steepness_output(ann, 1); #if INIT_WEIGHTS == 1 fann_randomize_weights(ann,0,1); #endif #if INIT_WEIGHTS == 2 fann_init_weights(ann, data); #endif #ifdef USE_RPROP fann_set_training_algorithm(ann, FANN_TRAIN_RPROP); #else fann_set_training_algorithm(ann, FANN_TRAIN_BATCH); #endif return ann; }