AI-NeuralNet-Simple
view release on metacpan or search on metacpan
lib/AI/NeuralNet/Simple.pm view on Meta::CPAN
sub learn_rate {
my ( $self, $rate ) = @_;
return c_get_learn_rate( $self->handle ) unless defined $rate;
logdie "learn rate must be between 0 and 1, exclusive"
unless $rate > 0 && $rate < 1;
c_set_learn_rate( $self->handle, $rate );
return $self;
}
sub DESTROY {
my $self = shift;
c_destroy_network( $self->handle );
}
#
# Serializing hook for Storable
#
sub STORABLE_freeze {
my ( $self, $cloning ) = @_;
my $internal = c_export_network( $self->handle );
# This is an excellent example where "we know better" than
# the recommended way in Storable's man page...
# Behaviour is the same whether we're cloning or not --RAM
my %copy = %$self;
delete $copy{handle};
return ( "", \%copy, $internal );
}
#
# Deserializing hook for Storable
#
sub STORABLE_thaw {
my ( $self, $cloning, $x, $copy, $internal ) = @_;
%$self = %$copy;
$self->{handle} = c_import_network($internal);
}
1;
__END__
=head1 NAME
AI::NeuralNet::Simple - An easy to use backprop neural net.
=head1 SYNOPSIS
use AI::NeuralNet::Simple;
my $net = AI::NeuralNet::Simple->new(2,1,2);
# teach it logical 'or'
for (1 .. 10000) {
$net->train([1,1],[0,1]);
$net->train([1,0],[0,1]);
$net->train([0,1],[0,1]);
$net->train([0,0],[1,0]);
}
printf "Answer: %d\n", $net->winner([1,1]);
printf "Answer: %d\n", $net->winner([1,0]);
printf "Answer: %d\n", $net->winner([0,1]);
printf "Answer: %d\n\n", $net->winner([0,0]);
=head1 ABSTRACT
This module is a simple neural net designed for those who have an interest
in artificial intelligence but need a "gentle" introduction. This is not
intended to replace any of the neural net modules currently available on the
CPAN.
=head1 DESCRIPTION
=head2 The Disclaimer
Please note that the following information is terribly incomplete. That's
deliberate. Anyone familiar with neural networks is going to laugh themselves
silly at how simplistic the following information is and the astute reader will
notice that I've raised far more questions than I've answered.
So why am I doing this? Because I'm giving I<just enough> information for
someone new to neural networks to have enough of an idea of what's going on so
they can actually use this module and then move on to something more powerful,
if interested.
=head2 The Biology
A neural network, at its simplest, is merely an attempt to mimic nature's
"design" of a brain. Like many successful ventures in the field of artificial
intelligence, we find that blatantly ripping off natural designs has allowed us
to solve many problems that otherwise might prove intractable. Fortunately,
Mother Nature has not chosen to apply for patents.
Our brains are comprised of neurons connected to one another by axons. The
axon makes the actual connection to a neuron via a synapse. When neurons
receive information, they process it and feed this information to other neurons
who in turn process the information and send it further until eventually
commands are sent to various parts of the body and muscles twitch, emotions are
felt and we start eyeing our neighbor's popcorn in the movie theater, wondering
if they'll notice if we snatch some while they're watching the movie.
=head2 A simple example of a neuron
Now that you have a solid biology background (uh, no), how does this work when
we're trying to simulate a neural network? The simplest part of the network is
the neuron (also known as a node or, sometimes, a neurode). A we might think
of a neuron as follows (OK, so I won't make a living as an ASCII artist):
Input neurons Synapses Neuron Output
----
n1 ---w1----> / \
n2 ---w2---->| n4 |---w4---->
n3 ---w3----> \ /
----
(Note that the above doesn't quite match what's in the C code for this module,
but it's close enough for you to get the idea. This is one of the many
oversimplifications that have been made).
In the above example, we have three input neurons (n1, n2, and n3). These
neurons feed whatever output they have through the three synapses (w1, w2, w3)
to the neuron in question, n4. The three synapses each have a "weight", which
lib/AI/NeuralNet/Simple.pm view on Meta::CPAN
The third argument is the targeted Mean Square Error (MSE). When provided,
the traning sequence will compute the maximum MSE seen during an iteration
over the training set, and if it is less than the supplied target, the
training stops. Computing the MSE at each iteration costs, but you are
certain to not over-train your network.
$net->train_set([
[1,1] => [0,1],
[1,0] => [0,1],
[0,1] => [0,1],
[0,0] => [1,0],
], 10000, 0.01);
The routine returns the MSE of the last iteration, which is the highest MSE
seen over the whole training set (and not an average MSE).
=head2 C<iterations([$integer])>
If called with a positive integer argument, this method will allow you to set
number of iterations that train_set will use and will return the network
object. If called without an argument, it will return the number of iterations
it was set to.
$net->iterations; # returns 100000
my @training_data = (
[1,1] => [0,1],
[1,0] => [0,1],
[0,1] => [0,1],
[0,0] => [1,0],
);
$net->iterations(100000) # let's have lots more iterations!
->train_set(\@training_data);
=head2 C<learn_rate($rate)>)
This method, if called without an argument, will return the current learning
rate. .20 is the default learning rate.
If called with an argument, this argument must be greater than zero and less
than one. This will set the learning rate and return the object.
$net->learn_rate; #returns the learning rate
$net->learn_rate(.1)
->iterations(100000)
->train_set(\@training_data);
If you choose a lower learning rate, you will train the network slower, but you
may get a better accuracy. A higher learning rate will train the network
faster, but it can have a tendancy to "overshoot" the answer when learning and
not learn as accurately.
=head2 C<infer(\@input)>
This method, if provided with an input array reference, will return an array
reference corresponding to the output values that it is guessing. Note that
these values will generally be close, but not exact. For example, with the
"logical or" program, you might expect results similar to:
use Data::Dumper;
print Dumper $net->infer([1,1]);
$VAR1 = [
'0.00993729281477686',
'0.990100297418451'
];
That clearly has the second output item being close to 1, so as a helper method
for use with a winner take all strategy, we have ...
=head2 C<winner(\@input)>
This method returns the index of the highest value from inferred results:
print $net->winner([1,1]); # will likely print "1"
For a more comprehensive example of how this is used, see the
"examples/game_ai.pl" program.
=head1 EXPORT
None by default.
=head1 CAVEATS
This is B<alpha> code. Very alpha. Not even close to ready for production,
don't even think about it. I'm putting it on the CPAN lest it languish on my
hard-drive forever. Hopefully someone will get some use out of it and think to
send me a patch or two.
=head1 TODO
=over 4
=item * Allow different numbers of layers
=back
=head1 BUGS
Probably.
=head1 SEE ALSO
L<AI::FANN> - Perl wrapper for the Fast Artificial Neural Network library
L<AI::NNFlex> - A base class for implementing neural networks
L<AI::NeuralNet::BackProp> - A simple back-prop neural net that uses Delta's
and Hebbs' rule
"AI Application Programming by M. Tim Jones, copyright (c) by Charles River
Media, Inc.
The C code in this module is based heavily upon Mr. Jones backpropogation
network in the book. The "game ai" example in the examples directory is based
upon an example he has graciously allowed me to use. I I<had> to use it
because it's more fun than many of the dry examples out there :)
"Naturally Intelligent Systems", by Maureen Caudill and Charles Butler,
copyright (c) 1990 by Massachussetts Institute of Technology.
This book is a decent introduction to neural networks in general. The forward
feed back error propogation is but one of many types.
=head1 AUTHORS
Curtis "Ovid" Poe, C<ovid [at] cpan [dot] org>
Multiple network support, persistence, export of MSE (mean squared error),
training until MSE below a given threshold and customization of the
activation function added by Raphael Manfredi C<Raphael_Manfredi@pobox.com>.
=head1 COPYRIGHT AND LICENSE
( run in 0.703 second using v1.01-cache-2.11-cpan-39bf76dae61 )