package NeuralNet; use Strict; use Carp; require "Neuron.pm"; ####################################################################### # # Class; Neural Net # # Representation of a neural network. This class directly # manages layers (LAYERS) of nodes (i.e. Neurons). # ####################################################################### # constructor sub new { my $proto = shift; my $class = ref( $proto ) || $proto; my $self = {}; $self->{LAYERS} = []; # # Allow methods to be called on this object. # bless( $self, $class ); return $self; } # # layers: read-only method that returns a list of all layers. # sub layers { my $self = shift; return $self->{LAYERS}; } # # numLayers: read-only method that returns the number of layers. # sub numLayers { my $self = shift; return scalar @{$self->{LAYERS}}; } # # layer: returns the n-th layer requested. # sub layer { my $self = shift; my $num = shift; my @layers = $self->layers; return @{$layers[$num]}; } # # lastLayer: returns the output layer. # sub lastLayer { my $self = shift; my @layers = $self->layers; return @{$layers[$#layers]}; } # # addLayer: Adds a reference to a layer of nodes. # # addLayer( number of nodes in layer ) # sub addLayer { my $self = shift; my $nodeCount = shift; my @lastlayer = $self->lastLayer; my @newlayer = ( 0..$nodeCount ); # # create children # foreach (@newlayer) { $_ = Neuron->new(); } # # hook them up with the previous layer # (if there is one) # foreach $parent (@lastlayer) { foreach $child (@newlayer) { $parent->addChild( $child ); } } # # add them to the network # push( @{$self->{LAYERS}}, \@newlayer ); } 1;