AI::FANN(3pm)

๐Ÿ’– NAME ๐Ÿ’–

AI::FANN (that's this page โ€” AI::FANN(3pm) bestie ๐Ÿ’…) is the Perl wrapper for the Fast Artificial Neural Network library. Neural nets, in Perl, slay.


โœจ SYNOPSIS โœจ

๐Ÿง  Train... (the glow-up era)

  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... (the main character era)

  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];
    }
  }

๐ŸŒธ DESCRIPTION ๐ŸŒธ

๐Ÿšจ WARNING: THIS IS A VERY EARLY RELEASE, MAY CONTAIN CRITICAL BUGS!!! ๐Ÿšจ

okay so, deep breath โ€” AI::FANN is a Perl wrapper for the Fast Artificial Neural Network (FANN) Library, which lives at http://fann.sourceforge.net. Here's the library describing itself, no cap:

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.

Literally iconic. ๐Ÿ˜ค

The AI::FANN object oriented interface is an almost one-to-one map onto the C library API โ€” but a few things got a lil makeover to be more perlish, and honestly? It ate. ๐Ÿ’…

  • Two classes are in the group chat: AI::FANN, which wraps the C struct fann type, and AI::FANN::TrainData, which wraps struct fann_train_data.
  • Prefixes and the common parts of C function names that point at those structs got yeeted. So for example C's fann_train_data_shuffle becomes AI::FANN::TrainData::shuffle, which you'll usually call like...
  $train_data->shuffle;
  • Pairs of C get/set functions get wrapped in Perl as dual accessor methods named after the attribute, with no set_/get_ prefix. So:
  $ann->bit_fail_limit($limit); # sets the bit_fail_limit

  $bfl = $ann->bit_fail_limit;  # gets the bit_fail_limit

And pairs that also need extra indexing arguments? Also wrapped in dual accessors:

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

  # gets:
  $af = $ann->neuron_activation_function($layer_ix, $neuron_ix);
โš ๏ธ Important: on the Perl side, that optional value argument gets moved to the last position โ€” on the C version of the set_ method it's usually the second argument. Don't get caught slippin'. ๐Ÿ™ˆ
  • Some functions got renamed so the naming is more consistent and follows Perl conventions:
    • 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 => ^^^ (yep, same as the one above โ€” also neuron_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 the C side reports gets automatically converted into a Perl exception. โœจ No manual error checking required after calling FANN functions โ€” she handles it.
  • Memory management is automatic. You do not need to call destroy methods. Self-care. ๐Ÿง–
  • Doubles are used for computations. Using floats or fixed point types is not supported. Periodt.

๐ŸŽ€ CONSTANTS ๐ŸŽ€

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);

Little detail we love: the values returned from these constant subs yield the integer value in numerical context, and the constant name when used as strings. Best of both worlds, fr fr. ๐Ÿซถ

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

...and there's more coming, bestie, don't go anywhere โ€” this page is long and it's literally still going. ๐ŸŒˆ

  • $ann->num_neurons โ€” returns a list with the number of neurons on every layer ๐Ÿ‘‘

AI::FANN::TrainData ๐Ÿงš

This one wraps the C struct fann_train_data, and it's got exactly one job but it does it cute:

  • AI::FANN::TrainData->new_from_file($filename) โ€” the <italic>-</italic> special, bestie: it's a straight-up wrapper around the C call, so the C documentation is where the full tea lives ๐Ÿ“–
  • AI::FANN::TrainData->new($input1, $output1 [, $input2, $output2, ...]) โ€” here $inputx and $outputx are arrays holding the values of the input and output layers, so you pass them in pairs, like a group chat where nobody gets left on read ๐Ÿ’…
  • AI::FANN::TrainData->new_empty($num_data, $num_inputs, $num_outputs) โ€” hands you a brand-new AI::FANN::TrainData object sized exactly like the args say. Heads up though: the initial values inside that object are random, and you 100% need to set them before you use this train data object to actually train an ANN. Not optional. Do not skip ๐Ÿ™ˆ

Methods ๐ŸŒธ

  • $train->data($index) โ€” returns two arrays: the input-layer values and the output-layer values, respectively, for that index ๐Ÿซถ
  • $train->data($index, $input, $output) โ€” here $input and $output are two arrays, and the input and output layers at $index get set to the values in those arrays โœจ
  • $train->shuffle โ€” the <italic>-</italic> treatment again; C docs have the full description, bestie
  • $train->scale_input($new_min, $new_max) โ€” same <italic>-</italic> situation, go check the C documentation
  • $train->scale_output($new_min, $new_max) โ€” <italic>-</italic>, C docs, you know the drill ๐Ÿ’–
  • $train->scale($new_min, $new_max) โ€” <italic>-</italic>, straight to the C docs
  • $train->subset($pos, $length) โ€” <italic>-</italic>, the C documentation is the source of truth here
  • $train->num_inputs โ€” <italic>-</italic>, wrapped C call, C docs, mwah ๐Ÿ’‹
  • $train->num_outputs โ€” <italic>-</italic>, again with the C docs
  • $train->length โ€” <italic>-</italic>, and once more: C documentation ๐Ÿ”ฅ

๐Ÿ› ๏ธ INSTALLATION ๐Ÿ› ๏ธ

Go read the README file for the instructions on installing this module, bestie. That's where the whole how-to lives ๐Ÿ“š


๐Ÿ› BUGS ๐Ÿ›

Okay so, the honest tea:

  • Only tested on Linux ๐Ÿง
  • I/O is not performed through PerlIO, because the C library just doesn't have the infrastructure required to do that. It's giving "we tried, the C library said no" ๐Ÿ’€
  • Send bug reports to my email address, or use the CPAN RT system ๐Ÿซถ
fr fr, if you find a bug, tell someone โ€” don't just sit on it like an unread message ๐Ÿ™ˆ

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

FANN homepage at <http://leenissen.dk/fann/index.php> โ€” go look, it's iconic ๐Ÿฆ‹


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. Slay ๐Ÿ’…

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. ๐ŸŽ€


And that's the whole page, bestie!! This was perl v5.42.3, dated 2026-08-19, from the AI::FANN(3pm) manual โ€” drink some water, go train a neural net, and remember: the C documentation is always there for you when the <italic>-</italic> leaves you hanging ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from