void GazeTracker::trainNN() { cout << "Getting data" << endl; struct fann_train_data* data = fann_create_train_from_callback(input_count, nn_eyewidth * nn_eyeheight, 2, getTrainingData); //fann_save_train(data, "data.txt"); cout << "Getting left data" << endl; struct fann_train_data* data_left = fann_create_train_from_callback(input_count, nn_eyewidth * nn_eyeheight, 2, getTrainingData_left); //fann_save_train(data_left, "data_left.txt"); fann_set_training_algorithm(ANN, FANN_TRAIN_RPROP); fann_set_learning_rate(ANN, 0.75); fann_set_training_algorithm(ANN_left, FANN_TRAIN_RPROP); fann_set_learning_rate(ANN_left, 0.75); cout << "Training" << endl; fann_train_on_data(ANN, data, 200, 20, 0.01); cout << "Training left" << endl; fann_train_on_data(ANN_left, data_left, 200, 20, 0.01); double mse = fann_get_MSE(ANN); double mse_left = fann_get_MSE(ANN_left); cout << "MSE: " << mse << ", MSE left: " << mse_left << endl; }
void GazeTracker::trainNN() { std::cout << "Getting data" << std::endl; struct fann_train_data *data = fann_create_train_from_callback(_inputCount, _nnEyeWidth * _nnEyeHeight, 2, getTrainingData); //fann_save_train(data, "data.txt"); std::cout << "Getting left data" << std::endl; struct fann_train_data *dataLeft = fann_create_train_from_callback(_inputCount, _nnEyeWidth * _nnEyeHeight, 2, getTrainingDataLeft); //fann_save_train(dataLeft, "dataLeft.txt"); fann_set_training_algorithm(_ANN, FANN_TRAIN_RPROP); fann_set_learning_rate(_ANN, 0.75); fann_set_training_algorithm(_ANNLeft, FANN_TRAIN_RPROP); fann_set_learning_rate(_ANNLeft, 0.75); std::cout << "Training" << std::endl; fann_train_on_data(_ANN, data, 200, 20, 0.01); std::cout << "Training left" << std::endl; fann_train_on_data(_ANNLeft, dataLeft, 200, 20, 0.01); double mse = fann_get_MSE(_ANN); double mseLeft = fann_get_MSE(_ANNLeft); std::cout << "MSE: " << mse << ", MSE left: " << mseLeft << std::endl; }
static void * lua_fann_train_thread (void *ud) { struct lua_fann_train_cbdata *cbdata = ud; struct lua_fann_train_reply rep; gchar repbuf[1]; msg_info ("start learning ANN, %d epochs are possible", cbdata->max_epochs); rspamd_socket_blocking (cbdata->pair[1]); fann_train_on_data (cbdata->f, cbdata->train, cbdata->max_epochs, 0, cbdata->desired_mse); rep.errcode = 0; rspamd_strlcpy (rep.errmsg, "OK", sizeof (rep.errmsg)); rep.mse = fann_get_MSE (cbdata->f); if (write (cbdata->pair[1], &rep, sizeof (rep)) == -1) { msg_err ("cannot write to socketpair: %s", strerror (errno)); return NULL; } if (read (cbdata->pair[1], repbuf, sizeof (repbuf)) == -1) { msg_err ("cannot read from socketpair: %s", strerror (errno)); return NULL; } return NULL; }
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; }
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; }
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; }
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; }
void NFQ_train(struct fann *qlearning_ann, int num_data, state *s, action *a, carmen_simulator_ackerman_config_t *simulator_config) { int k = 0; int b; int N = num_data - 1; double gamma = 0.3, target; static struct fann_train_data *ann_data; static double max_atan_curvature; if (ann_data == NULL) { initialize_ann_data(&ann_data, N*3, 4, 1); max_atan_curvature = atan(compute_curvature(carmen_degrees_to_radians(30.0), simulator_config)); } do { target = c(s[k], a[k], s[k+1]) + gamma * min_b_Q(&b, qlearning_ann, s[k+1], a[k+1], simulator_config); ann_data->input[k][0] = s[k].atan_desired_curvature / max_atan_curvature; ann_data->input[k][1] = s[k].atan_current_curvature / max_atan_curvature; ann_data->input[k][2] = (double) s[k].b / 6.0; ann_data->input[k][3] = a[k].steering_command / 100.0; ann_data->output[k][0] = target; //fprintf(stdout, "[%d] d: %.5lf c: %.5lf b: %.5lf (%d') s: %.5lf t: %.5lf\n", // k, ann_data->input[k][0], ann_data->input[k][1], ann_data->input[k][2]*10, b, ann_data->input[k][3]*100, ann_data->output[k][0]); k++; } while(k < N); //(ann, data, max_epochs, epochs_between_reports, desired_error) fann_train_on_data(qlearning_ann, ann_data, 200, 0, 0.001); }
/*! ann:train_on_data(train, max_epochs, epochs_between_reports, desired_error) *# Trains the neural network on the data in {{train}}, for up to *# {{max_epochs}} epochs, reporting every {{epochs_between_reports}}. *# Training stops when the error reaches {{desired_error}} *x ann:train_on_data(train, 500000, 1000, 0.001) *- */ static int ann_train_on_data(lua_State *L) { struct fann **ann; struct fann_train_data **train; int max_epochs, epochs_between_reports; float desired_error; if(lua_gettop(L) < 5) luaL_error(L, "insufficient parameters"); ann = luaL_checkudata(L, 1, FANN_METATABLE); luaL_argcheck(L, ann != NULL, 1, "'neural net' expected"); train = luaL_checkudata(L, 2, FANN_TRAIN_METATABLE); luaL_argcheck(L, train != NULL, 1, "'training data' expected"); max_epochs = lua_tointeger(L, 3); epochs_between_reports = lua_tointeger(L, 4); desired_error = lua_tonumber(L, 5); #ifdef FANN_VERBOSE printf("Training on data for up to %d epochs...\n", max_epochs); #endif fann_train_on_data(*ann, *train, max_epochs, epochs_between_reports, desired_error); 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); }
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); }
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; }
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); }
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; }
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; }
int main(int argc, char *argv[]) { struct fann_train_data *dadosTreino, *dadosTeste; struct fann *ANN; fann_type *ANN_Answers; int *layers, i, j, aux; chromosome chromo; float erro = 0.0; checkArgs(argc, argv); buildChromosome(argv, &chromo); checkDatasetFiles(); dadosTreino = fann_read_train_from_file(nomeArqTreino); layers = (int *) calloc(2+chromo.qntCamadasOcultas, sizeof(int)); layers[0] = qntNeuroniosEntrada; layers[2+chromo.qntCamadasOcultas-1] = qntNeuroniosSaida; aux = chromo.neurOcultos; for (i=1; i < 2+chromo.qntCamadasOcultas-1 ; i++) { layers[i] = aux; aux = aux/2; } // CRIANDO A RNA: ANN = fann_create_standard_array(2+chromo.qntCamadasOcultas, layers); // TREINO fann_set_learning_rate(ANN, chromo.learnRate); fann_set_learning_momentum(ANN, chromo.moment); fann_set_activation_function_hidden( ANN, chromo.fcOculta ); fann_set_activation_function_output( ANN, chromo.fcSaida ); fann_set_training_algorithm(ANN, chromo.algAprend ); if (fann_get_training_algorithm(ANN) == FANN_TRAIN_QUICKPROP) fann_set_quickprop_decay(ANN, chromo.decay); // Em python, o treino ficava entre um try. // Se desse erro, escrevia "Resultado: 999.0" e exit fann_train_on_data(ANN, dadosTreino, chromo.epocasTreino, 50, desiredError); fann_destroy_train(dadosTreino); // TESTES: dadosTeste = fann_read_train_from_file( nomeArqValidacao); // Em python, o teste também ficava entre um try. // Se desse erro, escrevia "Resultado: 999.0" e exit for(i = 0; i < fann_length_train_data(dadosTeste); i++) { ANN_Answers = fann_run(ANN, dadosTeste->input[i]); if (ANN_Answers == NULL) { printf("Resultado: 999.0\n"); exit(2); } for (j=0; j < qntNeuroniosSaida; j++) erro += (float) powf(fann_abs(ANN_Answers[j] - dadosTeste->output[i][j]), 2); } printf("Resultado: %f\n", erro/(fann_length_train_data(dadosTeste)-1)); fann_destroy_train(dadosTeste); saveANN(argc, argv, ANN); fann_destroy(ANN); }
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; }
void OcrNet::Train(cv::Mat &image, std::map< int, std::set<ImageRegion> > &image_lines, std::string &char_set) { std::map<int, std::set<ImageRegion> >::iterator image_lines_it; std::set<ImageRegion> regions; std::set<ImageRegion>::iterator regions_it; unsigned int char_count = 0, char_length = 0, repeat = 2, index = 0; struct fann_train_data **data; fann_type **inputs, **outputs; char_length = char_set.length(); data = new struct fann_train_data* [char_length]; for(uint j=0; j < char_length; j++) { data[j] = new struct fann_train_data; data[j]->num_data = repeat; data[j]->num_input = Ann[j]->num_input; data[j]->num_output = Ann[j]->num_output; inputs = new fann_type*[data[j]->num_data]; outputs = new fann_type*[data[j]->num_data]; data[j]->input = inputs; data[j]->output = outputs; for(uint i = 0; i < repeat; i++, char_count = 0) { for(image_lines_it = image_lines.begin(); char_count < char_length && image_lines_it != image_lines.end(); image_lines_it++, char_count++) { regions = (std::set<ImageRegion>) image_lines_it->second; for(regions_it = regions.begin(); char_count < char_length && regions_it != regions.end(); regions_it++, char_count++) { index = i * char_length + char_count; inputs[index] = new fann_type[data->num_input]; outputs[index] = new fann_type[data->num_output]; CreateInput(inputs[index], data->num_input, image, *regions_it); CreateOutput(outputs[index], data->num_output, char_count); std::cout << "Repeat Index: " << i << ", index: " << index << std::endl; } } } fann_randomize_weights(Ann[j], -0.001, 0.0); // fann_init_weights(Ann, data); fann_train_on_data(Ann[j], &data[j], MaxEpochs, 10, Error); for(unsigned int i=0; i < data[j]->num_data; i++) { delete [] inputs[i]; delete [] outputs[i]; } } delete data; delete [] inputs; delete [] outputs; }
const _tstring CNeuroNetwok::Teach(const std::vector< std::pair < _tstring, bool > >& InputData) { // На входе в Data карта путей к файлам и результатам, которые должны получится при распознавании // Подгружаем данные для каждого файла std::vector< std::pair < _tstring, bool > >::const_iterator it = InputData.begin(); const std::vector< std::pair < _tstring, bool > >::const_iterator itEnd = InputData.end(); for (; it != itEnd; ++it) { // Путь const _tstring& sPath = it->first; // Результат const bool bResult = it->second; // Данные std::list< float > BmpData; // Получаем данные для смещения 0 градусов AnalizeBMP(sPath, BmpData); // Добавляем данные m_TrainData.push_back(std::pair< std::list< float >, bool > (BmpData, bResult)); /* // Получаем данные для смещения 90 градусов AnalizeBMP(sPath, BmpData, 90); // Добавляем данные m_TrainData.push_back(std::pair< std::list< float >, bool > (BmpData, bResult)); // Получаем данные для смещения 180 градусов AnalizeBMP(sPath, BmpData, 180); // Добавляем данные m_TrainData.push_back(std::pair< std::list< float >, bool > (BmpData, bResult)); // Получаем данные для смещения 270 градусов AnalizeBMP(sPath, BmpData, 270); // Добавляем данные m_TrainData.push_back(std::pair< std::list< float >, bool > (BmpData, bResult));*/ } // Получили структуру данных, необходимую для тренировки нейросети // преобразуем ее к виду, необходимую для библиотеки fann boost::scoped_ptr< fann_train_data > pTrainData(MakeTrainData(m_TrainData)); if (!pTrainData) throw std::runtime_error("Failed to make train data!"); #ifdef _DEBUG // Для дебага fann_save_train(pTrainData.get(), "debug_data.dat"); #endif // Инициализируем веса связей нейронов fann_init_weights(m_pANN, pTrainData.get()); const float fDesiredError = (const float) 0; const unsigned int nEpochsBetweenReports = 10; // Тренируем нейросеть fann_train_on_data(m_pANN, pTrainData.get(), m_nEpochsCount, nEpochsBetweenReports, fDesiredError); // Сохраняем нейросеть fann_save(m_pANN, NETWORK_FILE_NAME); // Тестируем сеть и возвращаем результат m_bIsNetworkTeached = true; return boost::lexical_cast< _tstring > (fann_test_data(m_pANN, pTrainData.get())); }
void ViFann::fannTrainManyCpu(fann *network, fann_train_data *data, const unsigned int &maxEpochs, const unsigned int &epochsBetweenReports, const float &desiredError) { fann_train_on_data(network, data, maxEpochs, epochsBetweenReports, desiredError); }
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; }
/* 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; }
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; }
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; }
int main_tr4(int argc, char *argv[]) { int i, j; /************************************************** SENNA setup **************************************************/ /* options */ char *opt_path = NULL; int opt_usrtokens = 0; int vbs_hash_novb_idx = 22; /* inputs */ SENNA_Hash *word_hash = SENNA_Hash_new(opt_path, "hash/words.lst"); SENNA_Hash *caps_hash = SENNA_Hash_new(opt_path, "hash/caps.lst"); SENNA_Hash *suff_hash = SENNA_Hash_new(opt_path, "hash/suffix.lst"); SENNA_Hash *gazt_hash = SENNA_Hash_new(opt_path, "hash/gazetteer.lst"); SENNA_Hash *gazl_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.loc.lst", "data/ner.loc.dat"); SENNA_Hash *gazm_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.msc.lst", "data/ner.msc.dat"); SENNA_Hash *gazo_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.org.lst", "data/ner.org.dat"); SENNA_Hash *gazp_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.per.lst", "data/ner.per.dat"); SENNA_Tokenizer *tokenizer = SENNA_Tokenizer_new(word_hash, caps_hash, suff_hash, gazt_hash, gazl_hash, gazm_hash, gazo_hash, gazp_hash, opt_usrtokens); SENNA_SRL *srl = SENNA_SRL_new(opt_path, "data/srl.dat"); /* labels */ SENNA_Hash *pos_hash = SENNA_Hash_new(opt_path, "hash/pos.lst"); SENNA_Hash *srl_hash = SENNA_Hash_new(opt_path, "hash/srl.lst"); SENNA_POS *pos = SENNA_POS_new(opt_path, "data/pos.dat"); SENNA_VBS *vbs = SENNA_VBS_new(opt_path, "data/vbs.dat"); SENNA_PT0 *pt0 = SENNA_PT0_new(opt_path, "data/pt0.dat"); /* FANN setup */ 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.00001; const unsigned int max_epochs = 500000; const unsigned int epochs_between_reports = 100000; 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); struct fann_train_data *train_data; train_data = fann_create_train_from_callback(4, 2, 1, &data_callback); /************************************************** main program **************************************************/ /* Read the training file line by line*/ FILE * fp; char * line = NULL; size_t len = 0; ssize_t read; fp = fopen("training.dat", "r"); int targets[] = {1, 1, 1, -1}; int id=0; while ((read = getline(&line, &len, fp)) != -1) { SENNA_Tokens* tokens = SENNA_Tokenizer_tokenize(tokenizer, line); int *pos_labels = SENNA_POS_forward(pos, tokens->word_idx, tokens->caps_idx, tokens->suff_idx, tokens->n); int *pt0_labels = SENNA_PT0_forward(pt0, tokens->word_idx, tokens->caps_idx, pos_labels, tokens->n); int n_verbs = 0; char target_vb[MAX_TARGET_VB_SIZE]; int *vbs_labels = SENNA_VBS_forward(vbs, tokens->word_idx, tokens->caps_idx, pos_labels, tokens->n); for(i = 0; i < tokens->n; i++){ vbs_labels[i] = (vbs_labels[i] != vbs_hash_novb_idx); n_verbs += vbs_labels[i]; } int **srl_labels = SENNA_SRL_forward(srl, tokens->word_idx, tokens->caps_idx, pt0_labels, vbs_labels, tokens->n); /* Logic */ train_data->input[id][0] = -1; train_data->input[id][1] = -1; for(i = 0; i < tokens->n; i++){ printf("%s %s ",tokens->words[i], SENNA_Hash_key(pos_hash, pos_labels[i])); printf("%s", (vbs_labels[i] ? tokens->words[i] : "-")); for(j = 0; j < n_verbs; j++){ printf(" '%s'", SENNA_Hash_key(srl_hash, srl_labels[j][i])); //printf("%s %s %i %i", tokens->words[i], SENNA_Hash_key(srl_hash, srl_labels[j][i]), strcmp(tokens->words[i],"want")); if(strcmp(tokens->words[i],"want") == 0 && strcmp(SENNA_Hash_key(srl_hash, srl_labels[j][i]),"S-V") == 0){ train_data->input[id][0] = 1; } else if(strcmp(tokens->words[i],"pizza") == 0 && strcmp(SENNA_Hash_key(srl_hash, srl_labels[j][i]),"E-A1") == 0){ train_data->input[id][1] = 1; } } printf("\n"); } train_data->output[id][0] = targets[id]; printf("%s", line); printf("Input: %f %f, Output: %d\n\n", train_data->input[id][0], train_data->input[id][1], targets[id]); id++; } /* Train a classifier */ fann_train_on_data(ann, train_data, max_epochs, epochs_between_reports, desired_error); fann_save(ann, "asds.net"); fann_destroy(ann); fclose(fp); return 0; }