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 Cstruct fanntype, andAI::FANN::TrainData, which wrapsstruct 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_shufflebecomesAI::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_filefann_create_standard=>new_standardfann_get_num_input=>num_inputsfann_get_activation_function=>neuron_activation_functionfann_set_activation_function=>^^^(yep, same as the one above โ alsoneuron_activation_function)fann_set_activation_function_layer=>layer_activation_functionfann_set_activation_function_hidden=>hidden_activation_functionfann_set_activation_function_output=>output_activation_function
- Boolean methods return true on success and
undefon 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$inputxand$outputxare 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-newAI::FANN::TrainDataobject 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$inputand$outputare two arrays, and the input and output layers at$indexget 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 AND LICENSE โ๏ธ
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
- ubuntu stonking: libai-fann-perl 0.10-5build8 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms