AI-NeuralNet-Simple
view release on metacpan or search on metacpan
lib/AI/NeuralNet/Simple.pm view on Meta::CPAN
package AI::NeuralNet::Simple;
use Log::Agent;
use strict;
use vars qw( $REVISION $VERSION @ISA );
$REVISION = '$Id: Simple.pm,v 1.3 2004/01/31 20:34:11 ovid Exp $';
$VERSION = '0.11';
if ( $] >= 5.006 ) {
require XSLoader;
XSLoader::load( 'AI::NeuralNet::Simple', $VERSION );
}
else {
require DynaLoader;
push @ISA, 'DynaLoader';
AI::NeuralNet::Simple->bootstrap($VERSION);
}
sub handle { $_[0]->{handle} }
sub new {
my ( $class, @args ) = @_;
logdie "you must supply three positive integers to new()"
unless 3 == @args;
foreach (@args) {
logdie "arguments to new() must be positive integers"
unless defined $_ && /^\d+$/;
}
my $seed = rand(1); # Perl invokes srand() on first call to rand()
my $handle = c_new_network(@args);
logdie "could not create new network" unless $handle >= 0;
my $self = bless {
input => $args[0],
hidden => $args[1],
output => $args[2],
handle => $handle,
}, $class;
$self->iterations(10000); # set a reasonable default
}
sub train {
my ( $self, $inputref, $outputref ) = @_;
return c_train( $self->handle, $inputref, $outputref );
}
sub train_set {
my ( $self, $set, $iterations, $mse ) = @_;
$iterations ||= $self->iterations;
$mse = -1.0 unless defined $mse;
return c_train_set( $self->handle, $set, $iterations, $mse );
}
sub iterations {
my ( $self, $iterations ) = @_;
if ( defined $iterations ) {
logdie "iterations() value must be a positive integer."
unless $iterations
and $iterations =~ /^\d+$/;
$self->{iterations} = $iterations;
return $self;
}
$self->{iterations};
}
sub delta {
my ( $self, $delta ) = @_;
return c_get_delta( $self->handle ) unless defined $delta;
logdie "delta() value must be a positive number" unless $delta > 0.0;
c_set_delta( $self->handle, $delta );
return $self;
}
sub use_bipolar {
my ( $self, $bipolar ) = @_;
return c_get_use_bipolar( $self->handle ) unless defined $bipolar;
c_set_use_bipolar( $self->handle, $bipolar );
return $self;
}
sub infer {
my ( $self, $data ) = @_;
c_infer( $self->handle, $data );
}
sub winner {
# returns index of largest value in inferred answer
my ( $self, $data ) = @_;
my $arrayref = c_infer( $self->handle, $data );
my $largest = 0;
for ( 0 .. $#$arrayref ) {
$largest = $_ if $arrayref->[$_] > $arrayref->[$largest];
}
return $largest;
}
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
( run in 0.978 second using v1.01-cache-2.11-cpan-13bb782fe5a )