AI-ANN

 view release on metacpan or  search on metacpan

lib/AI/ANN/Neuron.pm  view on Meta::CPAN

	my $neurons = shift;
	if (ref $neurons eq 'HASH') {
		my @temparray;
		foreach my $i (keys %$neurons) {
			if (defined $neurons->{$i} && $neurons->{$i} != 0) {
				$temparray[$i]=$neurons->{$i};
			}
		}
		$neurons=\@temparray;
	}
	my @inputs = @$inputs;
	my @neurons = @$neurons;

	foreach my $id (0..$#{$self->{'inputs'}}) {
		unless ((not defined $self->{'inputs'}->[$id]) || 
				$self->{'inputs'}->[$id] == 0 || defined $inputs[$id])
				{return 0}
		# This probably shouldn't ever happen, as it would be weird if our
		# inputs weren't available yet.
	}
	foreach my $id (0..$#{$self->{'neurons'}}) {
		unless ((not defined $self->{'neurons'}->[$id]) || 
				$self->{'neurons'}->[$id] == 0 || defined $neurons[$id])
				{return 0}
	}
	return 1;
}


sub execute {
	my $self = shift;
	my $inputs = shift;
	my $neurons = shift;
	if (ref $neurons eq 'HASH') {
		my @temparray;
		foreach my $i (keys %$neurons) {
			$temparray[$i]=$neurons->{$i} || 0;
		}
		$neurons=\@temparray;
	}
	my @inputs = @$inputs;
	my @neurons = @$neurons;
	my @inputweights = @{$self->{'inputs'}};
	my @neuronweights = @{$self->{'neurons'}};
#	foreach my $i (0..$#inputs) {
#		$inputs[$i] ||= 0;
#	}
#	foreach my $i (0..$#neurons) {
#		$neurons[$i] ||= 0;
#	}
#	if ($#inputs < $#inputweights) {
#		foreach my $i ($#inputs+1..$#inputweights) {
#			$inputs[$i]=0;
#		}
#	}
#	if ($#neurons < $#neuronweights) {
#		foreach my $i ($#neurons+1..$#neuronweights) {
#			$neurons[$i]=0;
#		}
#	}
#print STDERR $self->{'id'}."\n";
#print STDERR join(',', @inputs)."\n";
#print STDERR join(',', @neurons)."\n";
#print STDERR join(',', @inputweights)."\n";
#print STDERR join(',', @neuronweights)."\n";
	my $output = 0;
	if ($self->{'inline_c'}) {
		$output = _execute_internals( \@inputs, \@neurons, \@inputweights, \@neuronweights );
	} else {
		foreach my $id (0..$#inputweights) {
			$output += ($inputweights[$id] || 0 ) * ($inputs[$id] || 0);
		}
		foreach my $id (0..$#neuronweights) {
			$output += ($neuronweights[$id] || 0) * ($neurons[$id] || 0);
		}
	}
	return $output;
}

__PACKAGE__->meta->make_immutable;

1;


__END__
=pod

=head1 NAME

AI::ANN::Neuron - a neuron for an artificial neural network simulator

=head1 VERSION

version 0.008

=head1 METHODS

=head2 new

AI::ANN::Neuron->new( $neuronid, {$inputid => $weight, ...}, {$neuronid => $weight} )

Weights may be whatever the user chooses. Note that packages that use this 
one may place their own restructions. Neurons and inputs are assumed to be 
zero-indexed.

eta_inputs and eta_neurons are optional, required only if you wish to use the 
Gaussian mutation in AI::ANN::Evolver.

=head2 ready

$neuron->ready( [$input0, $input1, ...], [$neuronvalue0, ...] )

All inputs must be provided or you're insane.
If a neuron is not yet available, make it undef, not zero.
Returns 1 if ready, 0 otherwise.

=head2 execute

$neuron->execute( [$input0, $input1, ...], {$neuronid => $neuronvalue, ...} )

You /must/ pass the correct number of inputs and neurons, and undefined values
    /must/ be zeros, not undef.
Returns raw value (linear potential)

=head1 AUTHOR



( run in 0.884 second using v1.01-cache-2.11-cpan-39bf76dae61 )