DOKK / manpages / debian 12 / libai-fann-perl / AI::FANN.3pm.en
AI::FANN(3pm) User Contributed Perl Documentation AI::FANN(3pm)

AI::FANN - Perl wrapper for the Fast Artificial Neural Network library

Train...

  use AI::FANN qw(:all);
  # create an ANN with 2 inputs, a hidden layer with 3 neurons and an
  # output layer with 1 neuron:
  my $ann = AI::FANN->new_standard(2, 3, 1);
  $ann->hidden_activation_function(FANN_SIGMOID_SYMMETRIC);
  $ann->output_activation_function(FANN_SIGMOID_SYMMETRIC);
  # create the training data for a XOR operator:
  my $xor_train = AI::FANN::TrainData->new( [-1, -1], [-1],
                                            [-1, 1], [1],
                                            [1, -1], [1],
                                            [1, 1], [-1] );
  $ann->train_on_data($xor_train, 500000, 1000, 0.001);
  $ann->save("xor.ann");

Run...

  use AI::FANN;
  my $ann = AI::FANN->new_from_file("xor.ann");
  for my $a (-1, 1) {
    for my $b (-1, 1) {
      my $out = $ann->run([$a, $b]);
      printf "xor(%f, %f) = %f\n", $a, $b, $out->[0];
    }
  }

  WARNING:  THIS IS A VERY EARLY RELEASE,
            MAY CONTAIN CRITICAL BUGS!!!

AI::FANN is a Perl wrapper for the Fast Artificial Neural Network (FANN) Library available from <http://fann.sourceforge.net>:

  Fast Artificial Neural Network Library is a free open source neural
  network library, which implements multilayer artificial neural
  networks in C with support for both fully connected and sparsely
  connected networks. Cross-platform execution in both fixed and
  floating point are supported. It includes a framework for easy
  handling of training data sets. It is easy to use, versatile, well
  documented, and fast. PHP, C++, .NET, Python, Delphi, Octave, Ruby,
  Pure Data and Mathematica bindings are available. A reference manual
  accompanies the library with examples and recommendations on how to
  use the library. A graphical user interface is also available for
  the library.

AI::FANN object oriented interface provides an almost direct map to the C library API. Some differences have been introduced to make it more perlish:

  • Two classes are used: "AI::FANN" that wraps the C "struct fann" type and "AI::FANN::TrainData" that wraps "struct fann_train_data".
  • Prefixes and common parts on the C function names referring to those structures have been removed. For instance C "fann_train_data_shuffle" becomes "AI::FANN::TrainData::shuffle" that will be usually called as...

      $train_data->shuffle;
        
  • Pairs of C get/set functions are wrapped in Perl with dual accessor methods named as the attribute (and without any "set_"/"get_" prefix). For instance:

      $ann->bit_fail_limit($limit); # sets the bit_fail_limit
      $bfl = $ann->bit_fail_limit;  # gets the bit_fail_limit
        

    Pairs of get/set functions requiring additional indexing arguments are also wrapped inside dual accessors:

      # sets:
      $ann->neuron_activation_function($layer_ix, $neuron_ix, $actfunc);
      # gets:
      $af = $ann->neuron_activation_function($layer_ix, $neuron_ix);
        

    Important: note that on the Perl version, the optional value argument is moved to the last position (on the C version of the "set_" method it is usually the second argument).

  • Some functions have been renamed to make the naming more consistent and to follow Perl conventions:

      C                                      Perl
      -----------------------------------------------------------
      fann_create_from_file               => new_from_file
      fann_create_standard                => new_standard
      fann_get_num_input                  => num_inputs
      fann_get_activation_function        => neuron_activation_function
      fann_set_activation_function        => ^^^
      fann_set_activation_function_layer  => layer_activation_function
      fann_set_activation_function_hidden => hidden_activation_function
      fann_set_activation_function_output => output_activation_function
        
  • Boolean methods return true on success and undef on failure.
  • Any error reported from the C side is automatically converter to a Perl exception. No manual error checking is required after calling FANN functions.
  • Memory management is automatic, no need to call destroy methods.
  • Doubles are used for computations (using floats or fixed point types is not supported).

All the constants defined in the C documentation are exported from the module:

  # import all...
  use AI::FANN ':all';
  # or individual constants...
  use AI::FANN qw(FANN_TRAIN_INCREMENTAL FANN_GAUSSIAN);

The values returned from this constant subs yield the integer value on numerical context and the constant name when used as strings.

The constants available are:

  # enum fann_train_enum:
  FANN_TRAIN_INCREMENTAL
  FANN_TRAIN_BATCH
  FANN_TRAIN_RPROP
  FANN_TRAIN_QUICKPROP
  # enum fann_activationfunc_enum:
  FANN_LINEAR
  FANN_THRESHOLD
  FANN_THRESHOLD_SYMMETRIC
  FANN_SIGMOID
  FANN_SIGMOID_STEPWISE
  FANN_SIGMOID_SYMMETRIC
  FANN_SIGMOID_SYMMETRIC_STEPWISE
  FANN_GAUSSIAN
  FANN_GAUSSIAN_SYMMETRIC
  FANN_GAUSSIAN_STEPWISE
  FANN_ELLIOT
  FANN_ELLIOT_SYMMETRIC
  FANN_LINEAR_PIECE
  FANN_LINEAR_PIECE_SYMMETRIC
  FANN_SIN_SYMMETRIC
  FANN_COS_SYMMETRIC
  FANN_SIN
  FANN_COS
  # enum fann_errorfunc_enum:
  FANN_ERRORFUNC_LINEAR
  FANN_ERRORFUNC_TANH
  # enum fann_stopfunc_enum:
  FANN_STOPFUNC_MSE
  FANN_STOPFUNC_BIT

The classes defined by this package are:

AI::FANN

Wraps C "struct fann" types and provides the following methods (consult the C documentation for a full description of their usage):

-
-
-
-
$ann->save($filename)
-
$ann->run($input)
"input" is an array with the input values.

returns an array with the values on the output layer.

  $out = $ann->run([1, 0.6]);
  print "@$out\n";
    
$ann->randomize_weights($min_weight, $max_weight)
$ann->train($input, $desired_output)
$input and $desired_output are arrays.
$ann->test($input, $desired_output)
$input and $desired_output are arrays.

It returns an array with the values of the output layer.

$ann->reset_MSE
-
$ann->train_on_file($filename, $max_epochs, $epochs_between_reports, $desired_error)
-
$ann->train_on_data($train_data, $max_epochs, $epochs_between_reports, $desired_error)
$train_data is a AI::FANN::TrainData object.
$ann->cascadetrain_on_file($filename, $max_neurons, $neurons_between_reports, $desired_error)
-
$ann->cascadetrain_on_data($train_data, $max_neurons, $neurons_between_reports, $desired_error)
$train_data is a AI::FANN::TrainData object.
$ann->train_epoch($train_data)
$train_data is a AI::FANN::TrainData object.
$ann->print_connections
-
$ann->print_parameters
-
$ann->cascade_activation_functions()
returns a list of the activation functions used for cascade training.
$ann->cascade_activation_functions(@activation_functions)
sets the list of activation function to use for cascade training.
$ann->cascade_activation_steepnesses()
returns a list of the activation steepnesses used for cascade training.
$ann->cascade_activation_steepnesses(@activation_steepnesses)
sets the list of activation steepnesses to use for cascade training.
$ann->training_algorithm
$ann->training_algorithm($training_algorithm)
-
$ann->train_error_function
$ann->train_error_function($error_function)
-
$ann->train_stop_function
$ann->train_stop_function($stop_function)
-
$ann->learning_rate
$ann->learning_rate($rate)
-
$ann->learning_momentum
$ann->learning_momentum($momentun)
-
$ann->bit_fail_limit
$ann->bit_fail_limit($bfl)
-
$ann->quickprop_decay
$ann->quickprop_decay($qpd)
-
$ann->quickprop_mu
$ann->quickprop_mu($qpmu)
-
$ann->rprop_increase_factor
$ann->rprop_increase_factor($factor)
-
$ann->rprop_decrease_factor
$ann->rprop_decrease_factor($factor)
-
$ann->rprop_delta_min
$ann->rprop_delta_min($min)
-
$ann->rprop_delta_max
$ann->rprop_delta_max($max)
-
$ann->num_inputs
-
$ann->num_outputs
-
$ann->total_neurons
-
$ann->total_connections
-
$ann->MSE
-
$ann->bit_fail
-
-
$ann->cascade_output_stagnation_epochs
$ann->cascade_output_stagnation_epochs($epochs)
-
$ann->cascade_candidate_change_fraction
$ann->cascade_candidate_change_fraction($fraction)
-
$ann->cascade_candidate_stagnation_epochs
$ann->cascade_candidate_stagnation_epochs($epochs)
-
$ann->cascade_weight_multiplier
$ann->cascade_weight_multiplier($multiplier)
-
$ann->cascade_candidate_limit
$ann->cascade_candidate_limit($limit)
-
$ann->cascade_max_out_epochs
$ann->cascade_max_out_epochs($epochs)
-
$ann->cascade_max_cand_epochs
$ann->cascade_max_cand_epochs($epochs)
-
$ann->cascade_num_candidates
-
$ann->cascade_num_candidate_groups
$ann->cascade_num_candidate_groups($groups)
-
$ann->neuron_activation_function($layer_index, $neuron_index)
$ann->neuron_activation_function($layer_index, $neuron_index, $activation_function)
-
$ann->layer_activation_function($layer_index, $activation_function)
-
$ann->hidden_activation_function($layer_index, $activation_function)
-
$ann->output_activation_function($layer_index, $activation_function)
-
$ann->neuron_activation_steepness($layer_index, $neuron_index)
$ann->neuron_activation_steepness($layer_index, $neuron_index, $activation_steepness)
-
$ann->layer_activation_steepness($layer_index, $activation_steepness)
-
$ann->hidden_activation_steepness($layer_index, $activation_steepness)
-
$ann->output_activation_steepness($layer_index, $activation_steepness)
-
$ann->num_layers
returns the number of layers on the ANN
$ann->layer_num_neurons($layer_index)
return the number of neurons on layer $layer_index.
$ann->num_neurons
return a list with the number of neurons on every layer

Wraps C "struct fann_train_data" and provides the following method:

AI::FANN::TrainData->new_from_file($filename)
-
AI::FANN::TrainData->new($input1, $output1 [, $input2, $output2, ...])
$inputx and $outputx are arrays with the values of the input and output layers.
AI::FANN::TrainData->new_empty($num_data, $num_inputs, $num_outputs)
returns a new AI::FANN::TrainData object of the sizes indicated on the arguments. The initial values of the data contained inside the object are random and should be set before using the train data object for training an ANN.
$train->data($index)
returns two arrays with the values of the input and output layer respectively for that index.
$train->data($index, $input, $output)
$input and $output are two arrays.

The input and output layers at the index $index are set to the values on these arrays.

$train->shuffle
-
$train->scale_input($new_min, $new_max)
-
$train->scale_output($new_min, $new_max)
-
$train->scale($new_min, $new_max)
-
$train->subset($pos, $length)
-
$train->num_inputs
-
$train->num_outputs
-
$train->length
-

See the README file for instruction on installing this module.

Only tested on Linux.

I/O is not performed through PerlIO because the C library doesn't have the required infrastructure to do that.

Send bug reports to my email address or use the CPAN RT system.

FANN homepage at <http://leenissen.dk/fann/index.php>.

Copyright (C) 2006-2008 by Salvador Fandiño (sfandino@yahoo.com).

This Perl module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.

The Fast Artificial Neural Network Library (FANN) Copyright (C) 2003-2006 Steffen Nissen (lukesky@diku.dk) and others.

Distributed under the GNU Lesser General Public License.

2022-10-19 perl v5.36.0