📄 fann_cpp.h
字号:
}
/* Method: run
Will run input through the neural network, returning an array of outputs, the number of which being
equal to the number of neurons in the output layer.
See also:
<test>, <fann_run>
This function appears in FANN >= 1.0.0.
*/
fann_type* run(fann_type *input)
{
if (ann == NULL)
{
return NULL;
}
return fann_run(ann, input);
}
/* Method: randomize_weights
Give each connection a random weight between *min_weight* and *max_weight*
From the beginning the weights are random between -0.1 and 0.1.
See also:
<init_weights>, <fann_randomize_weights>
This function appears in FANN >= 1.0.0.
*/
void randomize_weights(fann_type min_weight, fann_type max_weight)
{
if (ann != NULL)
{
fann_randomize_weights(ann, min_weight, max_weight);
}
}
/* Method: init_weights
Initialize the weights using Widrow + Nguyen's algorithm.
This function behaves similarly to fann_randomize_weights. It will use the algorithm developed
by Derrick Nguyen and Bernard Widrow to set the weights in such a way
as to speed up training. This technique is not always successful, and in some cases can be less
efficient than a purely random initialization.
The algorithm requires access to the range of the input data (ie, largest and smallest input),
and therefore accepts a second argument, data, which is the training data that will be used to
train the network.
See also:
<randomize_weights>, <training_data::read_train_from_file>,
<fann_init_weights>
This function appears in FANN >= 1.1.0.
*/
void init_weights(const training_data &data)
{
if ((ann != NULL) && (data.train_data != NULL))
{
fann_init_weights(ann, data.train_data);
}
}
/* Method: print_connections
Will print the connections of the ann in a compact matrix, for easy viewing of the internals
of the ann.
The output from fann_print_connections on a small (2 2 1) network trained on the xor problem
>Layer / Neuron 012345
>L 1 / N 3 BBa...
>L 1 / N 4 BBA...
>L 1 / N 5 ......
>L 2 / N 6 ...BBA
>L 2 / N 7 ......
This network have five real neurons and two bias neurons. This gives a total of seven neurons
named from 0 to 6. The connections between these neurons can be seen in the matrix. "." is a
place where there is no connection, while a character tells how strong the connection is on a
scale from a-z. The two real neurons in the hidden layer (neuron 3 and 4 in layer 1) has
connection from the three neurons in the previous layer as is visible in the first two lines.
The output neuron (6) has connections form the three neurons in the hidden layer 3 - 5 as is
visible in the fourth line.
To simplify the matrix output neurons is not visible as neurons that connections can come from,
and input and bias neurons are not visible as neurons that connections can go to.
This function appears in FANN >= 1.2.0.
*/
void print_connections()
{
if (ann != NULL)
{
fann_print_connections(ann);
}
}
/* Method: create_from_file
Constructs a backpropagation neural network from a configuration file,
which have been saved by <save>.
See also:
<save>, <save_to_fixed>, <fann_create_from_file>
This function appears in FANN >= 1.0.0.
*/
bool create_from_file(const std::string &configuration_file)
{
destroy();
ann = fann_create_from_file(configuration_file.c_str());
return (ann != NULL);
}
/* Method: save
Save the entire network to a configuration file.
The configuration file contains all information about the neural network and enables
<create_from_file> to create an exact copy of the neural network and all of the
parameters associated with the neural network.
These two parameters (<set_callback>, <set_error_log>) are *NOT* saved
to the file because they cannot safely be ported to a different location. Also temporary
parameters generated during training like <get_MSE> is not saved.
Return:
The function returns 0 on success and -1 on failure.
See also:
<create_from_file>, <save_to_fixed>, <fann_save>
This function appears in FANN >= 1.0.0.
*/
bool save(const std::string &configuration_file)
{
if (ann == NULL)
{
return false;
}
if (fann_save(ann, configuration_file.c_str()) == -1)
{
return false;
}
return true;
}
/* Method: save_to_fixed
Saves the entire network to a configuration file.
But it is saved in fixed point format no matter which
format it is currently in.
This is usefull for training a network in floating points,
and then later executing it in fixed point.
The function returns the bit position of the fix point, which
can be used to find out how accurate the fixed point network will be.
A high value indicates high precision, and a low value indicates low
precision.
A negative value indicates very low precision, and a very
strong possibility for overflow.
(the actual fix point will be set to 0, since a negative
fix point does not make sence).
Generally, a fix point lower than 6 is bad, and should be avoided.
The best way to avoid this, is to have less connections to each neuron,
or just less neurons in each layer.
The fixed point use of this network is only intended for use on machines that
have no floating point processor, like an iPAQ. On normal computers the floating
point version is actually faster.
See also:
<create_from_file>, <save>, <fann_save_to_fixed>
This function appears in FANN >= 1.0.0.
*/
int save_to_fixed(const std::string &configuration_file)
{
int fixpoint = 0;
if (ann != NULL)
{
fixpoint = fann_save_to_fixed(ann, configuration_file.c_str());
}
return fixpoint;
}
#ifndef FIXEDFANN
/* Method: train
Train one iteration with a set of inputs, and a set of desired outputs.
This training is always incremental training (see <FANN::training_algorithm_enum>),
since only one pattern is presented.
Parameters:
ann - The neural network structure
input - an array of inputs. This array must be exactly <fann_get_num_input> long.
desired_output - an array of desired outputs. This array must be exactly <fann_get_num_output> long.
See also:
<train_on_data>, <train_epoch>, <fann_train>
This function appears in FANN >= 1.0.0.
*/
void train(fann_type *input, fann_type *desired_output)
{
if (ann != NULL)
{
fann_train(ann, input, desired_output);
}
}
/* Method: train_epoch
Train one epoch with a set of training data.
Train one epoch with the training data stored in data. One epoch is where all of
the training data is considered exactly once.
This function returns the MSE error as it is calculated either before or during
the actual training. This is not the actual MSE after the training epoch, but since
calculating this will require to go through the entire training set once more, it is
more than adequate to use this value during training.
The training algorithm used by this function is chosen by the <fann_set_training_algorithm>
function.
See also:
<train_on_data>, <test_data>, <fann_train_epoch>
This function appears in FANN >= 1.2.0.
*/
float train_epoch(const training_data &data)
{
float mse = 0.0f;
if ((ann != NULL) && (data.train_data != NULL))
{
mse = fann_train_epoch(ann, data.train_data);
}
return mse;
}
/* Method: train_on_data
Trains on an entire dataset, for a period of time.
This training uses the training algorithm chosen by <set_training_algorithm>,
and the parameters set for these training algorithms.
Parameters:
ann - The neural network
data - The data, which should be used during training
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.
desired_error - The desired <get_MSE> or <get_bit_fail>, depending on which stop function
is chosen by <set_train_stop_function>.
Instead of printing out reports every epochs_between_reports, a callback function can be called
(see <set_callback>).
See also:
<train_on_file>, <train_epoch>, <fann_train_on_data>
This function appears in FANN >= 1.0.0.
*/
void train_on_data(const training_data &data, unsigned int max_epochs,
unsigned int epochs_between_reports, float desired_error)
{
if ((ann != NULL) && (data.train_data != NULL))
{
fann_train_on_data(ann, data.train_data, max_epochs,
epochs_between_reports, desired_error);
}
}
/* Method: train_on_file
Does the same as <train_on_data>, but reads the training data directly from a file.
See also:
<train_on_data>, <fann_train_on_file>
This function appears in FANN >= 1.0.0.
*/
void train_on_file(const std::string &filename, unsigned int max_epochs,
unsigned int epochs_between_reports, float desired_error)
{
if (ann != NULL)
{
fann_train_on_file(ann, filename.c_str(),
max_epochs, epochs_between_reports, desired_error);
}
}
#endif /* NOT FIXEDFANN */
/* Method: test
Test with a set of inputs, and a set of desired outputs.
This operation updates the mean square error, but does not
change the network in any way.
See also:
<test_data>, <train>, <fann_test>
This function appears in FANN >= 1.0.0.
*/
fann_type * test(fann_type *input, fann_type *desired_output)
{
fann_type * output = NULL;
if (ann != NULL)
{
output = fann_test(ann, input, desired_output);
}
return output;
}
/* Method: test_data
Test a set of training data and calculates the MSE for the training data.
This function updates the MSE and the bit fail values.
See also:
<test>, <get_MSE>, <get_bit_fail>, <fann_test_data>
This function appears in FANN >= 1.2.0.
*/
float test_data(const training_data &data)
{
float mse = 0.0f;
if ((ann != NULL) && (data.train_data != NULL))
{
mse = fann_test_data(ann, data.train_data);
}
return mse;
}
/* Method: get_MSE
Reads the mean square error from the network.
Reads the mean square error from the network. This value is calculated during
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -