1 #ifndef FANN_CPP_H_INCLUDED 2 #define FANN_CPP_H_INCLUDED 92 #include "base/Vector2d.h" 110 enum error_function_enum {
111 ERRORFUNC_LINEAR = FANN_ERRORFUNC_LINEAR,
128 enum stop_function_enum
130 STOPFUNC_MSE = FANN_STOPFUNC_MSE,
166 enum training_algorithm_enum {
167 TRAIN_INCREMENTAL = FANN_TRAIN_INCREMENTAL,
265 enum activation_function_enum {
266 LINEAR = FANN_LINEAR,
272 SIGMOID_SYMMETRIC_STEPWISE,
279 LINEAR_PIECE_SYMMETRIC,
296 enum network_type_enum
298 LAYER = FANN_NETTYPE_LAYER,
315 typedef struct fann_connection connection;
351 typedef int (*callback_type) (neural_net &net, training_data &train,
352 unsigned int max_epochs,
unsigned int epochs_between_reports,
353 float desired_error,
unsigned int epochs,
void *user_data);
382 if (data.train_data != NULL)
384 train_data = fann_duplicate_train_data(data.train_data);
396 #ifdef USE_VIRTUAL_DESTRUCTOR 413 if (train_data != NULL)
415 fann_destroy_train(train_data);
440 bool read_train_from_file(
const std::string &filename)
443 train_data = fann_read_train_from_file(filename.c_str());
444 return (train_data != NULL);
459 bool save_train(
const std::string &filename)
461 if (train_data == NULL)
465 if (fann_save_train(train_data, filename.c_str()) == -1)
486 bool save_train_to_fixed(
const std::string &filename,
unsigned int decimal_point)
488 if (train_data == NULL)
492 if (fann_save_train_to_fixed(train_data, filename.c_str(), decimal_point) == -1)
506 void shuffle_train_data()
508 if (train_data != NULL)
510 fann_shuffle_train_data(train_data);
522 fann_train_data *new_data = fann_merge_train_data(train_data, data.train_data);
523 if (new_data != NULL)
526 train_data = new_data;
539 unsigned int length_train_data()
541 if (train_data == NULL)
547 return fann_length_train_data(train_data);
560 unsigned int num_input_train_data()
562 if (train_data == NULL)
568 return fann_num_input_train_data(train_data);
581 unsigned int num_output_train_data()
583 if (train_data == NULL)
589 return fann_num_output_train_data(train_data);
605 fann_type **get_input()
607 if (train_data == NULL)
613 return train_data->input;
625 fann_type **get_output()
627 if (train_data == NULL)
633 return train_data->output;
655 void set_train_data(
unsigned int num_data,
656 unsigned int num_input, fann_type **input,
657 unsigned int num_output, fann_type **output)
660 struct fann_train_data *data =
661 (
struct fann_train_data *)malloc(
sizeof(
struct fann_train_data));
662 data->input = (fann_type **)calloc(num_data,
sizeof(fann_type *));
663 data->output = (fann_type **)calloc(num_data,
sizeof(fann_type *));
665 data->num_data = num_data;
666 data->num_input = num_input;
667 data->num_output = num_output;
669 fann_type *data_input = (fann_type *)calloc(num_input*num_data,
sizeof(fann_type));
670 fann_type *data_output = (fann_type *)calloc(num_output*num_data,
sizeof(fann_type));
672 for (
unsigned int i = 0; i < num_data; ++i)
674 data->input[i] = data_input;
675 data_input += num_input;
676 for (
unsigned int j = 0; j < num_input; ++j)
678 data->input[i][j] = input[i][j];
680 data->output[i] = data_output;
681 data_output += num_output;
682 for (
unsigned int j = 0; j < num_output; ++j)
684 data->output[i][j] = output[i][j];
687 set_train_data(data);
692 void set_train_data(
const std::vector< std::vector<fann_type> >& input,
693 const std::vector< std::vector<fann_type> >& output)
695 unsigned int num_data=input.size();
697 assert(input.size()==output.size());
698 unsigned int num_input=input[0].size();
699 unsigned int num_output=output[0].size();
702 struct fann_train_data *data =
703 (
struct fann_train_data *)malloc(
sizeof(
struct fann_train_data));
704 data->input = (fann_type **)calloc(num_data,
sizeof(fann_type *));
705 data->output = (fann_type **)calloc(num_data,
sizeof(fann_type *));
707 data->num_data = num_data;
708 data->num_input = num_input;
709 data->num_output = num_output;
711 fann_type *data_input = (fann_type *)calloc(num_input*num_data,
sizeof(fann_type));
712 fann_type *data_output = (fann_type *)calloc(num_output*num_data,
sizeof(fann_type));
714 for (
unsigned int i = 0; i < num_data; ++i)
716 data->input[i] = data_input;
717 data_input += num_input;
718 for (
unsigned int j = 0; j < num_input; ++j)
720 data->input[i][j] = input[i][j];
722 data->output[i] = data_output;
723 data_output += num_output;
724 for (
unsigned int j = 0; j < num_output; ++j)
726 data->output[i][j] = output[i][j];
729 set_train_data(data);
732 void set_train_data(
const std::vector< std::vector< std::vector<fann_type> > >& input,
unsigned int num_data)
735 assert(input.size());
736 unsigned int num_class=input.size();
737 assert(input[0].size());
738 unsigned int num_input=input[0][0].size();
739 unsigned int num_output=num_class;
744 struct fann_train_data *data =
745 (
struct fann_train_data *)malloc(
sizeof(
struct fann_train_data));
746 data->input = (fann_type **)calloc(num_data,
sizeof(fann_type *));
747 data->output = (fann_type **)calloc(num_data,
sizeof(fann_type *));
749 data->num_data = num_data;
750 data->num_input = num_input;
751 data->num_output = num_output;
753 fann_type *data_input = (fann_type *)calloc(num_input*num_data,
sizeof(fann_type));
754 fann_type *data_output = (fann_type *)calloc(num_output*num_data,
sizeof(fann_type));
756 unsigned int isample=0;
757 for(
int iclass=0;iclass<num_class;++iclass){
758 for(
int csample=0;csample<input[iclass].size();++csample){
759 data->input[isample] = data_input;
760 data_input += num_input;
761 for(
int iband=0;iband<input[iclass][csample].size();++iband){
762 assert(input[iclass][csample].size()==num_input);
763 data->input[isample][iband] = input[iclass][csample][iband];
765 data->output[isample] = data_output;
766 data_output += num_output;
767 for(
int ic=0;ic<num_output;++ic){
771 data->output[isample][ic] = 1;
773 data->output[isample][ic] = -1;
778 set_train_data(data);
781 void set_train_data(
const std::vector<
Vector2d<fann_type> >& input,
unsigned int num_data)
784 assert(input.size());
785 unsigned int num_class=input.size();
786 assert(input[0].size());
787 unsigned int num_input=input[0][0].size();
788 unsigned int num_output=num_class;
793 struct fann_train_data *data =
794 (
struct fann_train_data *)malloc(
sizeof(
struct fann_train_data));
795 data->input = (fann_type **)calloc(num_data,
sizeof(fann_type *));
796 data->output = (fann_type **)calloc(num_data,
sizeof(fann_type *));
798 data->num_data = num_data;
799 data->num_input = num_input;
800 data->num_output = num_output;
802 fann_type *data_input = (fann_type *)calloc(num_input*num_data,
sizeof(fann_type));
803 fann_type *data_output = (fann_type *)calloc(num_output*num_data,
sizeof(fann_type));
805 unsigned int isample=0;
806 for(
int iclass=0;iclass<num_class;++iclass){
807 for(
int csample=0;csample<input[iclass].size();++csample){
808 data->input[isample] = data_input;
809 data_input += num_input;
810 for(
int iband=0;iband<input[iclass][csample].size();++iband){
811 assert(input[iclass][csample].size()==num_input);
812 data->input[isample][iband] = input[iclass][csample][iband];
814 data->output[isample] = data_output;
815 data_output += num_output;
816 for(
int ic=0;ic<num_output;++ic){
820 data->output[isample][ic] = 1;
822 data->output[isample][ic] = -1;
827 set_train_data(data);
835 void set_train_data(
struct fann_train_data *data)
869 void create_train_from_callback(
unsigned int num_data,
870 unsigned int num_input,
871 unsigned int num_output,
872 void (FANN_API *user_function)(
unsigned int,
879 train_data = fann_create_train_from_callback(num_data, num_input, num_output, user_function);
891 void scale_input_train_data(fann_type new_min, fann_type new_max)
893 if (train_data != NULL)
895 fann_scale_input_train_data(train_data, new_min, new_max);
908 void scale_output_train_data(fann_type new_min, fann_type new_max)
910 if (train_data != NULL)
912 fann_scale_output_train_data(train_data, new_min, new_max);
925 void scale_train_data(fann_type new_min, fann_type new_max)
927 if (train_data != NULL)
929 fann_scale_train_data(train_data, new_min, new_max);
951 void subset_train_data(
unsigned int pos,
unsigned int length)
953 if (train_data != NULL)
955 struct fann_train_data *temp = fann_subset_train_data(train_data, pos, length);
968 struct fann_train_data* train_data;
1002 #ifdef USE_VIRTUAL_DESTRUCTOR 1021 user_context *user_data =
static_cast<user_context *
>(fann_get_user_data(ann));
1022 if (user_data != NULL)
1061 bool create_standard(
unsigned int num_layers, ...)
1064 std::vector<unsigned int> arr(num_layers);
1067 va_start(layers, num_layers);
1070 for (
unsigned int ii = 0; ii < num_layers; ii++)
1071 arr[ii] = va_arg(layers,
unsigned int);
1072 bool status = create_standard_array(num_layers, &(arr[0]));
1089 bool create_standard_array(
unsigned int num_layers,
const unsigned int * layers)
1092 ann = fann_create_standard_array(num_layers, layers);
1093 return (ann != NULL);
1118 bool create_sparse(
float connection_rate,
unsigned int num_layers, ...)
1121 std::vector<unsigned int> arr(num_layers);
1124 va_start(layers, num_layers);
1128 for (
unsigned int ii = 0; ii < num_layers; ii++)
1129 arr[ii] = va_arg(layers,
unsigned int);
1130 bool status = create_sparse_array(connection_rate, num_layers, &(arr[0]));
1149 bool create_sparse_array(
float connection_rate,
1150 unsigned int num_layers,
const unsigned int * layers)
1153 ann = fann_create_sparse_array(connection_rate, num_layers, layers);
1154 return (ann != NULL);
1174 bool create_shortcut(
unsigned int num_layers, ...)
1177 std::vector<unsigned int> arr(num_layers);
1180 va_start(layers, num_layers);
1183 for (
unsigned int ii = 0; ii < num_layers; ii++)
1184 arr[ii] = va_arg(layers,
unsigned int);
1185 bool status = create_shortcut_array(num_layers, &(arr[0]));
1205 bool create_shortcut_array(
unsigned int num_layers,
1206 const unsigned int * layers)
1209 ann = fann_create_shortcut_array(num_layers, layers);
1210 return (ann != NULL);
1223 fann_type* run(fann_type *input)
1232 return fann_run(ann, input);
1235 std::vector<fann_type> run(std::vector<fann_type> input)
1237 std::vector<fann_type> vresult;
1242 vresult.resize(get_num_output());
1244 result=fann_run(ann,&(input[0]));
1245 for(
int iout=0;iout<get_num_output();++iout)
1246 vresult[iout]=*(result++);
1261 void randomize_weights(fann_type min_weight, fann_type max_weight)
1265 fann_randomize_weights(ann, min_weight, max_weight);
1290 if ((ann != NULL) && (data.train_data != NULL))
1292 fann_init_weights(ann, data.train_data);
1322 void print_connections()
1326 fann_print_connections(ann);
1340 bool create_from_file(
const std::string &configuration_file)
1343 ann = fann_create_from_file(configuration_file.c_str());
1344 return (ann != NULL);
1367 bool save(
const std::string &configuration_file)
1373 if (fann_save(ann, configuration_file.c_str()) == -1)
1412 int save_to_fixed(
const std::string &configuration_file)
1417 fixpoint = fann_save_to_fixed(ann, configuration_file.c_str());
1439 void train(fann_type *input, fann_type *desired_output)
1443 fann_train(ann, input, desired_output);
1469 if ((ann != NULL) && (data.train_data != NULL))
1471 mse = fann_train_epoch(ann, data.train_data);
1500 void train_on_data(
const training_data &data,
unsigned int max_epochs,
1501 unsigned int epochs_between_reports,
float desired_error)
1503 if ((ann != NULL) && (data.train_data != NULL))
1505 fann_train_on_data(ann, data.train_data, max_epochs,
1506 epochs_between_reports, desired_error);
1512 void train_on_data(
const std::vector< std::vector<fann_type> >& input,
1513 const std::vector< std::vector<fann_type> >& output,
1515 unsigned int max_epochs,
1516 unsigned int epochs_between_reports,
1517 float desired_error)
1522 data.set_train_data(input,output);
1523 if(data.train_data != NULL){
1526 fann_train_on_data(ann, data.train_data, max_epochs,
1527 epochs_between_reports, desired_error);
1533 unsigned int num_data,
1535 unsigned int max_epochs,
1536 unsigned int epochs_between_reports,
1537 float desired_error)
1542 data.set_train_data(input,num_data);
1543 if(data.train_data != NULL){
1546 fann_train_on_data(ann, data.train_data, max_epochs,
1547 epochs_between_reports, desired_error);
1554 unsigned int ntraining,
1556 unsigned int max_epochs,
1557 float desired_error,
1558 std::vector<unsigned short>& referenceVector,
1559 std::vector<unsigned short>& outputVector,
1562 referenceVector.clear();
1563 outputVector.clear();
1564 assert(cv<ntraining);
1566 int nclass=trainingFeatures.size();
1567 std::vector< Vector2d<float> > testFeatures(nclass);
1570 int nrun=(cv>1)? cv : ntraining;
1573 for(
int irun=0;irun<nrun;++irun){
1575 std::cout <<
"run " << irun << std::endl;
1578 std::cout <<
"reset training sample from last run" << std::endl;
1579 for(
int iclass=0;iclass<nclass;++iclass){
1580 while(testFeatures[iclass].size()){
1581 trainingFeatures[iclass].push_back(testFeatures[iclass].back());
1582 testFeatures[iclass].pop_back();
1585 std::cout <<
"training size " << iclass <<
": " << trainingFeatures[iclass].size() << std::endl;
1586 std::cout <<
"test size " << iclass <<
": " << testFeatures[iclass].size() << std::endl;
1588 assert(trainingFeatures[iclass].size());
1592 std::cout <<
"create test sample" << std::endl;
1593 unsigned int nsample=0;
1594 int ntest=(cv>1)? ntraining/cv : 1;
1595 while(nsample<ntest){
1599 testFeatures[testclass].push_back(trainingFeatures[testclass][0]);
1600 trainingFeatures[testclass].erase(trainingFeatures[testclass].begin());
1601 if(!trainingFeatures[testclass].size())
1602 std::cout <<
"Error: testclass " << testclass <<
" has no training" << std::endl;
1603 assert(trainingFeatures[testclass].size());
1605 if(static_cast<float>(trainingFeatures[testclass].size())/static_cast<float>(testFeatures[testclass].size())<=(cv-1)){
1607 std::cout <<
"training size " << testclass <<
": " << trainingFeatures[testclass].size() << std::endl;
1608 std::cout <<
"test size " << testclass <<
": " << testFeatures[testclass].size() << std::endl;
1610 testclass=(testclass+1)%nclass;
1613 assert(nsample==ntest);
1616 std::cout << std::endl <<
"Set training data" << std::endl;
1617 bool initWeights=
true;
1618 unsigned int epochs_between_reports=0;
1619 train_on_data(trainingFeatures,ntraining-ntest,initWeights, max_epochs,
1620 epochs_between_reports, desired_error);
1623 std::cout << std::endl <<
"Cross validation" << std::endl;
1625 std::vector<float> result(nclass);
1627 for(
int iclass=0;iclass<testFeatures.size();++iclass){
1628 assert(trainingFeatures[iclass].size());
1629 for(
int isample=0;isample<testFeatures[iclass].size();++isample){
1630 result=run(testFeatures[iclass][isample]);
1633 for(
int ic=0;ic<nclass;++ic){
1634 float pv=(result[ic]+1.0)/2.0;
1641 referenceVector.push_back(iclass);
1642 outputVector.push_back(maxClass);
1652 for(
int iclass=0;iclass<nclass;++iclass){
1653 while(testFeatures[iclass].size()){
1654 trainingFeatures[iclass].push_back(testFeatures[iclass].back());
1655 testFeatures[iclass].pop_back();
1663 float cross_validation(std::vector< std::vector<fann_type> >& input,
1664 std::vector< std::vector<fann_type> >& output,
1666 unsigned int max_epochs,
1667 float desired_error,
1668 std::vector< std::vector<fann_type> >& referenceVector,
1669 std::vector< std::vector<fann_type> >& outputVector,
1672 assert(input.size());
1673 assert(output.size()==input.size());
1674 unsigned int ntraining=input.size();
1675 unsigned int noutput=output[0].size();
1676 referenceVector.clear();
1677 outputVector.clear();
1678 assert(cv<ntraining);
1680 std::vector< std::vector<fann_type> > testInput;
1681 std::vector< std::vector<fann_type> > testOutput;
1683 int nrun=(cv>1)? cv : ntraining;
1686 for(
int irun=0;irun<nrun;++irun){
1688 std::cout <<
"run " << irun << std::endl;
1691 std::cout <<
"reset training sample from last run" << std::endl;
1692 while(testInput.size()){
1693 input.push_back(testInput.back());
1694 testInput.pop_back();
1696 while(testOutput.size()){
1697 output.push_back(testOutput.back());
1698 testOutput.pop_back();
1700 assert(testInput.size()==testOutput.size());
1702 std::cout <<
"training size: " << input.size() << std::endl;
1703 std::cout <<
"test size: " << testInput.size() << std::endl;
1705 assert(input.size());
1708 std::cout <<
"create test sample" << std::endl;
1709 unsigned int nsample=0;
1710 int ntest=(cv>1)? ntraining/cv : 1;
1711 while(nsample<ntest){
1712 testInput.push_back(input[0]);
1713 testOutput.push_back(output[0]);
1714 input.erase(input.begin());
1715 output.erase(output.begin());
1716 assert(input.size());
1717 assert(output.size());
1718 assert(input.size()==output.size());
1721 assert(nsample==ntest);
1722 assert(testInput.size()==testOutput.size());
1725 std::cout << std::endl <<
"Set training data" << std::endl;
1726 bool initWeights=
true;
1727 unsigned int epochs_between_reports=0;
1729 train_on_data(input,output,initWeights, max_epochs,
1730 epochs_between_reports, desired_error);
1733 std::cout << std::endl <<
"Cross validation" << std::endl;
1735 std::vector<fann_type> result(noutput);
1736 for(
int isample=0;isample<testInput.size();++isample){
1737 result=run(testInput[isample]);
1738 referenceVector.push_back(testOutput[isample]);
1739 outputVector.push_back(result);
1743 while(testInput.size()){
1744 input.push_back(testInput.back());
1745 testInput.pop_back();
1747 while(testOutput.size()){
1748 output.push_back(testOutput.back());
1749 testOutput.pop_back();
1763 void train_on_file(
const std::string &filename,
unsigned int max_epochs,
1764 unsigned int epochs_between_reports,
float desired_error)
1768 fann_train_on_file(ann, filename.c_str(),
1769 max_epochs, epochs_between_reports, desired_error);
1785 fann_type * test(fann_type *input, fann_type *desired_output)
1787 fann_type * output = NULL;
1790 output = fann_test(ann, input, desired_output);
1809 if ((ann != NULL) && (data.train_data != NULL))
1811 mse = fann_test_data(ann, data.train_data);
1819 assert(input.size());
1820 unsigned int num_class=input.size();
1821 assert(input[0].size());
1822 unsigned int num_input=input[0][0].size();
1823 unsigned int num_output=num_class;
1824 struct fann_train_data *data =
1825 (
struct fann_train_data *)malloc(
sizeof(
struct fann_train_data));
1826 data->input = (fann_type **)calloc(num_data,
sizeof(fann_type *));
1827 data->output = (fann_type **)calloc(num_data,
sizeof(fann_type *));
1829 data->num_data = num_data;
1830 data->num_input = num_input;
1831 data->num_output = num_output;
1833 fann_type *data_input = (fann_type *)calloc(num_input*num_data,
sizeof(fann_type));
1834 fann_type *data_output = (fann_type *)calloc(num_output*num_data,
sizeof(fann_type));
1836 unsigned int isample=0;
1837 for(
int iclass=0;iclass<num_class;++iclass){
1838 for(
int csample=0;csample<input[iclass].size();++csample){
1839 data->input[isample] = data_input;
1840 data_input += num_input;
1841 for(
int iband=0;iband<input[iclass][csample].size();++iband){
1842 assert(input[iclass][csample].size()==num_input);
1843 data->input[isample][iband] = input[iclass][csample][iband];
1845 data->output[isample] = data_output;
1846 data_output += num_output;
1847 for(
int ic=0;ic<num_output;++ic){
1851 data->output[isample][ic] = 1;
1853 data->output[isample][ic] = -1;
1859 trainingData.train_data = data;
1860 return test_data(trainingData);
1881 mse = fann_get_MSE(ann);
1901 fann_reset_MSE(ann);
1917 void set_callback(callback_type callback,
void *user_data)
1922 user_context *user_instance =
static_cast<user_context *
>(fann_get_user_data(ann));
1923 if (user_instance != NULL)
1924 delete user_instance;
1926 user_instance =
new user_context();
1927 user_instance->user_callback = callback;
1928 user_instance->user_data = user_data;
1929 user_instance->net =
this;
1930 fann_set_user_data(ann, user_instance);
1932 if (callback != NULL)
1933 fann_set_callback(ann, &FANN::neural_net::internal_callback);
1935 fann_set_callback(ann, NULL);
1948 void print_parameters()
1952 fann_print_parameters(ann);
1972 training_algorithm_enum get_training_algorithm()
1974 fann_train_enum training_algorithm = FANN_TRAIN_INCREMENTAL;
1977 training_algorithm = fann_get_training_algorithm(ann);
1979 return static_cast<training_algorithm_enum
>(training_algorithm);
1990 void set_training_algorithm(training_algorithm_enum training_algorithm)
1994 fann_set_training_algorithm(ann,
1995 static_cast<fann_train_enum>(training_algorithm));
2015 float get_learning_rate()
2017 float learning_rate = 0.0f;
2020 learning_rate = fann_get_learning_rate(ann);
2022 return learning_rate;
2033 void set_learning_rate(
float learning_rate)
2037 fann_set_learning_rate(ann, learning_rate);
2062 activation_function_enum get_activation_function(
int layer,
int neuron)
2064 unsigned int activation_function = 0;
2067 activation_function = fann_get_activation_function(ann, layer, neuron);
2069 return static_cast<activation_function_enum
>(activation_function);
2094 void set_activation_function(activation_function_enum activation_function,
int layer,
int neuron)
2098 fann_set_activation_function(ann,
2099 static_cast<fann_activationfunc_enum>(activation_function), layer, neuron);
2117 void set_activation_function_layer(activation_function_enum activation_function,
int layer)
2121 fann_set_activation_function_layer(ann,
2122 static_cast<fann_activationfunc_enum>(activation_function), layer);
2137 void set_activation_function_hidden(activation_function_enum activation_function)
2141 fann_set_activation_function_hidden(ann,
2142 static_cast<fann_activationfunc_enum>(activation_function));
2157 void set_activation_function_output(activation_function_enum activation_function)
2161 fann_set_activation_function_output(ann,
2162 static_cast<fann_activationfunc_enum>(activation_function));
2192 fann_type get_activation_steepness(
int layer,
int neuron)
2194 fann_type activation_steepness = 0;
2197 activation_steepness = fann_get_activation_steepness(ann, layer, neuron);
2199 return activation_steepness;
2225 void set_activation_steepness(fann_type steepness,
int layer,
int neuron)
2229 fann_set_activation_steepness(ann, steepness, layer, neuron);
2247 void set_activation_steepness_layer(fann_type steepness,
int layer)
2251 fann_set_activation_steepness_layer(ann, steepness, layer);
2266 void set_activation_steepness_hidden(fann_type steepness)
2270 fann_set_activation_steepness_hidden(ann, steepness);
2285 void set_activation_steepness_output(fann_type steepness)
2289 fann_set_activation_steepness_output(ann, steepness);
2308 error_function_enum get_train_error_function()
2310 fann_errorfunc_enum train_error_function = FANN_ERRORFUNC_LINEAR;
2313 train_error_function = fann_get_train_error_function(ann);
2315 return static_cast<error_function_enum
>(train_error_function);
2329 void set_train_error_function(error_function_enum train_error_function)
2333 fann_set_train_error_function(ann,
2334 static_cast<fann_errorfunc_enum>(train_error_function));
2351 float get_quickprop_decay()
2353 float quickprop_decay = 0.0f;
2356 quickprop_decay = fann_get_quickprop_decay(ann);
2358 return quickprop_decay;
2370 void set_quickprop_decay(
float quickprop_decay)
2374 fann_set_quickprop_decay(ann, quickprop_decay);
2391 float get_quickprop_mu()
2393 float quickprop_mu = 0.0f;
2396 quickprop_mu = fann_get_quickprop_mu(ann);
2398 return quickprop_mu;
2410 void set_quickprop_mu(
float quickprop_mu)
2414 fann_set_quickprop_mu(ann, quickprop_mu);
2430 float get_rprop_increase_factor()
2432 float factor = 0.0f;
2435 factor = fann_get_rprop_increase_factor(ann);
2449 void set_rprop_increase_factor(
float rprop_increase_factor)
2453 fann_set_rprop_increase_factor(ann, rprop_increase_factor);
2468 float get_rprop_decrease_factor()
2470 float factor = 0.0f;
2473 factor = fann_get_rprop_decrease_factor(ann);
2487 void set_rprop_decrease_factor(
float rprop_decrease_factor)
2491 fann_set_rprop_decrease_factor(ann, rprop_decrease_factor);
2506 float get_rprop_delta_min()
2511 delta = fann_get_rprop_delta_min(ann);
2525 void set_rprop_delta_min(
float rprop_delta_min)
2529 fann_set_rprop_delta_min(ann, rprop_delta_min);
2544 float get_rprop_delta_max()
2549 delta = fann_get_rprop_delta_max(ann);
2563 void set_rprop_delta_max(
float rprop_delta_max)
2567 fann_set_rprop_delta_max(ann, rprop_delta_max);
2577 unsigned int get_num_input()
2579 unsigned int num_input = 0;
2582 num_input = fann_get_num_input(ann);
2593 unsigned int get_num_output()
2595 unsigned int num_output = 0;
2598 num_output = fann_get_num_output(ann);
2610 unsigned int get_total_neurons()
2616 return fann_get_total_neurons(ann);
2625 unsigned int get_total_connections()
2631 return fann_get_total_connections(ann);
2649 unsigned int get_decimal_point()
2655 return fann_get_decimal_point(ann);
2676 unsigned int get_multiplier()
2682 return fann_get_multiplier(ann);
2700 network_type_enum get_network_type()
2702 fann_nettype_enum network_type = FANN_NETTYPE_LAYER;
2705 network_type = fann_get_network_type(ann);
2707 return static_cast<network_type_enum
>(network_type);
2722 float get_connection_rate()
2728 return fann_get_connection_rate(ann);
2743 unsigned int get_num_layers()
2749 return fann_get_num_layers(ann);
2766 void get_layer_array(
unsigned int *layers)
2770 fann_get_layer_array(ann, layers);
2774 void get_layer_array(std::vector<unsigned int> vlayers)
2778 vlayers.resize(get_num_layers());
2779 unsigned int* layers=(
unsigned int *)calloc(get_num_layers(),
sizeof(
unsigned int));
2780 fann_get_layer_array(ann, layers);
2781 for(
int ilayer=0;ilayer<get_num_layers();++ilayer)
2782 vlayers[ilayer]=*(layers++);
2798 void get_bias_array(
unsigned int *bias)
2802 fann_get_bias_array(ann, bias);
2818 void get_connection_array(connection *connections)
2822 fann_get_connection_array(ann, connections);
2826 void get_connection_array(std::vector<connection>& convector)
2831 convector.resize(get_total_connections());
2832 connection* connections=(connection*)calloc(get_total_connections(),
sizeof(connection));
2833 fann_get_connection_array(ann, connections);
2834 for(
int icon=0;icon<get_total_connections();++icon)
2835 convector[icon]=*(connections++);
2853 void set_weight_array(connection *connections,
unsigned int num_connections)
2857 fann_set_weight_array(ann, connections, num_connections);
2861 void set_weight_array(std::vector<connection> convector)
2865 unsigned int num_connections=convector.size();
2866 connection* connections=(connection*)calloc(num_connections,
sizeof(connection));
2867 connections=&(convector[0]);
2868 fann_set_weight_array(ann, connections, num_connections);
2884 void set_weight(
unsigned int from_neuron,
unsigned int to_neuron, fann_type weight)
2888 fann_set_weight(ann, from_neuron, to_neuron, weight);
2910 float get_learning_momentum()
2912 float learning_momentum = 0.0f;
2915 learning_momentum = fann_get_learning_momentum(ann);
2917 return learning_momentum;
2928 void set_learning_momentum(
float learning_momentum)
2932 fann_set_learning_momentum(ann, learning_momentum);
2949 stop_function_enum get_train_stop_function()
2951 enum fann_stopfunc_enum stopfunc = FANN_STOPFUNC_MSE;
2954 stopfunc = fann_get_train_stop_function(ann);
2956 return static_cast<stop_function_enum
>(stopfunc);
2970 void set_train_stop_function(stop_function_enum train_stop_function)
2974 fann_set_train_stop_function(ann,
2975 static_cast<enum fann_stopfunc_enum>(train_stop_function));
2997 fann_type get_bit_fail_limit()
2999 fann_type bit_fail_limit = 0.0f;
3003 bit_fail_limit = fann_get_bit_fail_limit(ann);
3005 return bit_fail_limit;
3017 void set_bit_fail_limit(fann_type bit_fail_limit)
3021 fann_set_bit_fail_limit(ann, bit_fail_limit);
3040 unsigned int get_bit_fail()
3042 unsigned int bit_fail = 0;
3045 bit_fail = fann_get_bit_fail(ann);
3081 void cascadetrain_on_data(
const training_data &data,
unsigned int max_neurons,
3082 unsigned int neurons_between_reports,
float desired_error)
3084 if ((ann != NULL) && (data.train_data != NULL))
3086 fann_cascadetrain_on_data(ann, data.train_data, max_neurons,
3087 neurons_between_reports, desired_error);
3100 void cascadetrain_on_file(
const std::string &filename,
unsigned int max_neurons,
3101 unsigned int neurons_between_reports,
float desired_error)
3105 fann_cascadetrain_on_file(ann, filename.c_str(),
3106 max_neurons, neurons_between_reports, desired_error);
3133 float get_cascade_output_change_fraction()
3135 float change_fraction = 0.0f;
3138 change_fraction = fann_get_cascade_output_change_fraction(ann);
3140 return change_fraction;
3152 void set_cascade_output_change_fraction(
float cascade_output_change_fraction)
3156 fann_set_cascade_output_change_fraction(ann, cascade_output_change_fraction);
3175 unsigned int get_cascade_output_stagnation_epochs()
3177 unsigned int stagnation_epochs = 0;
3180 stagnation_epochs = fann_get_cascade_output_stagnation_epochs(ann);
3182 return stagnation_epochs;
3194 void set_cascade_output_stagnation_epochs(
unsigned int cascade_output_stagnation_epochs)
3198 fann_set_cascade_output_stagnation_epochs(ann, cascade_output_stagnation_epochs);
3225 float get_cascade_candidate_change_fraction()
3227 float change_fraction = 0.0f;
3230 change_fraction = fann_get_cascade_candidate_change_fraction(ann);
3232 return change_fraction;
3245 void set_cascade_candidate_change_fraction(
float cascade_candidate_change_fraction)
3249 fann_set_cascade_candidate_change_fraction(ann, cascade_candidate_change_fraction);
3268 unsigned int get_cascade_candidate_stagnation_epochs()
3270 unsigned int stagnation_epochs = 0;
3273 stagnation_epochs = fann_get_cascade_candidate_stagnation_epochs(ann);
3275 return stagnation_epochs;
3288 void set_cascade_candidate_stagnation_epochs(
unsigned int cascade_candidate_stagnation_epochs)
3292 fann_set_cascade_candidate_stagnation_epochs(ann, cascade_candidate_stagnation_epochs);
3309 fann_type get_cascade_weight_multiplier()
3311 fann_type weight_multiplier = 0;
3314 weight_multiplier = fann_get_cascade_weight_multiplier(ann);
3316 return weight_multiplier;
3328 void set_cascade_weight_multiplier(fann_type cascade_weight_multiplier)
3332 fann_set_cascade_weight_multiplier(ann, cascade_weight_multiplier);
3351 fann_type get_cascade_candidate_limit()
3353 fann_type candidate_limit = 0;
3356 candidate_limit = fann_get_cascade_candidate_limit(ann);
3358 return candidate_limit;
3370 void set_cascade_candidate_limit(fann_type cascade_candidate_limit)
3374 fann_set_cascade_candidate_limit(ann, cascade_candidate_limit);
3390 unsigned int get_cascade_max_out_epochs()
3392 unsigned int max_out_epochs = 0;
3395 max_out_epochs = fann_get_cascade_max_out_epochs(ann);
3397 return max_out_epochs;
3409 void set_cascade_max_out_epochs(
unsigned int cascade_max_out_epochs)
3413 fann_set_cascade_max_out_epochs(ann, cascade_max_out_epochs);
3429 unsigned int get_cascade_max_cand_epochs()
3431 unsigned int max_cand_epochs = 0;
3434 max_cand_epochs = fann_get_cascade_max_cand_epochs(ann);
3436 return max_cand_epochs;
3448 void set_cascade_max_cand_epochs(
unsigned int cascade_max_cand_epochs)
3452 fann_set_cascade_max_cand_epochs(ann, cascade_max_cand_epochs);
3479 unsigned int get_cascade_num_candidates()
3481 unsigned int num_candidates = 0;
3484 num_candidates = fann_get_cascade_num_candidates(ann);
3486 return num_candidates;
3501 unsigned int get_cascade_activation_functions_count()
3503 unsigned int activation_functions_count = 0;
3506 activation_functions_count = fann_get_cascade_activation_functions_count(ann);
3508 return activation_functions_count;
3525 activation_function_enum * get_cascade_activation_functions()
3527 enum fann_activationfunc_enum *activation_functions = NULL;
3530 activation_functions = fann_get_cascade_activation_functions(ann);
3532 return reinterpret_cast<activation_function_enum *
>(activation_functions);
3549 void set_cascade_activation_functions(activation_function_enum *cascade_activation_functions,
3550 unsigned int cascade_activation_functions_count)
3554 fann_set_cascade_activation_functions(ann,
3555 reinterpret_cast<enum fann_activationfunc_enum *>(cascade_activation_functions),
3556 cascade_activation_functions_count);
3572 unsigned int get_cascade_activation_steepnesses_count()
3574 unsigned int activation_steepness_count = 0;
3577 activation_steepness_count = fann_get_cascade_activation_steepnesses_count(ann);
3579 return activation_steepness_count;
3598 fann_type *get_cascade_activation_steepnesses()
3600 fann_type *activation_steepnesses = NULL;
3603 activation_steepnesses = fann_get_cascade_activation_steepnesses(ann);
3605 return activation_steepnesses;
3622 void set_cascade_activation_steepnesses(fann_type *cascade_activation_steepnesses,
3623 unsigned int cascade_activation_steepnesses_count)
3627 fann_set_cascade_activation_steepnesses(ann,
3628 cascade_activation_steepnesses, cascade_activation_steepnesses_count);
3649 unsigned int get_cascade_num_candidate_groups()
3651 unsigned int num_candidate_groups = 0;
3654 num_candidate_groups = fann_get_cascade_num_candidate_groups(ann);
3656 return num_candidate_groups;
3668 void set_cascade_num_candidate_groups(
unsigned int cascade_num_candidate_groups)
3672 fann_set_cascade_num_candidate_groups(ann, cascade_num_candidate_groups);
3692 fann_scale_train(ann, data.train_data);
3709 fann_descale_train(ann, data.train_data);
3722 bool set_input_scaling_params(
const training_data &data,
float new_input_min,
float new_input_max)
3724 bool status =
false;
3727 status = (fann_set_input_scaling_params(ann, data.train_data, new_input_min, new_input_max) != -1);
3741 bool set_output_scaling_params(
const training_data &data,
float new_output_min,
float new_output_max)
3743 bool status =
false;
3746 status = (fann_set_output_scaling_params(ann, data.train_data, new_output_min, new_output_max) != -1);
3761 float new_input_min,
float new_input_max,
float new_output_min,
float new_output_max)
3763 bool status =
false;
3766 status = (fann_set_scaling_params(ann, data.train_data,
3767 new_input_min, new_input_max, new_output_min, new_output_max) != -1);
3781 bool clear_scaling_params()
3783 bool status =
false;
3786 status = (fann_clear_scaling_params(ann) != -1);
3800 void scale_input(fann_type *input_vector)
3804 fann_scale_input(ann, input_vector );
3817 void scale_output(fann_type *output_vector)
3821 fann_scale_output(ann, output_vector );
3834 void descale_input(fann_type *input_vector)
3838 fann_descale_input(ann, input_vector );
3851 void descale_output(fann_type *output_vector)
3855 fann_descale_output(ann, output_vector );
3881 void set_error_log(FILE *log_file)
3883 fann_set_error_log(reinterpret_cast<struct fann_error *>(ann), log_file);
3895 unsigned int get_errno()
3897 return fann_get_errno(reinterpret_cast<struct fann_error *>(ann));
3908 fann_reset_errno(reinterpret_cast<struct fann_error *>(ann));
3919 fann_reset_errstr(reinterpret_cast<struct fann_error *>(ann));
3930 std::string get_errstr()
3932 return std::string(fann_get_errstr(reinterpret_cast<struct fann_error *>(ann)));
3943 fann_print_error(reinterpret_cast<struct fann_error *>(ann));
3950 typedef struct user_context_type
3952 callback_type user_callback;
3958 static int FANN_API internal_callback(
struct fann *ann,
struct fann_train_data *train,
3959 unsigned int max_epochs,
unsigned int epochs_between_reports,
float desired_error,
unsigned int epochs)
3961 user_context *user_data =
static_cast<user_context *
>(fann_get_user_data(ann));
3962 if (user_data != NULL)
3965 data.train_data = train;
3967 int result = (*user_data->user_callback)(*user_data->net,
3968 data, max_epochs, epochs_between_reports, desired_error, epochs, user_data);
3970 data.train_data = NULL;