| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | AI::Perceptron - example of a node in a neural network. | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | use AI::Perceptron; | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | my $p = AI::Perceptron->new | ||||||||||||||||||||||||||||||||
| 10 | ->num_inputs( 2 ) | ||||||||||||||||||||||||||||||||
| 11 | ->learning_rate( 0.04 ) | ||||||||||||||||||||||||||||||||
| 12 | ->threshold( 0.02 ) | ||||||||||||||||||||||||||||||||
| 13 | ->weights([ 0.1, 0.2 ]); | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | my @inputs = ( 1.3, -0.45 ); # input can be any number | ||||||||||||||||||||||||||||||||
| 16 | my $target = 1; # output is always -1 or 1 | ||||||||||||||||||||||||||||||||
| 17 | my $current = $p->compute_output( @inputs ); | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | print "current output: $current, target: $target\n"; | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | $p->add_examples( [ $target, @inputs ] ); | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | $p->max_iterations( 10 )->train or | ||||||||||||||||||||||||||||||||
| 24 | warn "couldn't train in 10 iterations!"; | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | print "training until it gets it right\n"; | ||||||||||||||||||||||||||||||||
| 27 | $p->max_iterations( -1 )->train; # watch out for infinite loops | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | package AI::Perceptron; | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use accessors qw( num_inputs learning_rate _weights threshold | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | 2 | training_examples max_iterations ); | |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 2 | our $VERSION = '1.0'; | |||||||||||||||||||||||||||||||
| 38 | 2 | our $Debug = 0; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 41 | 3 | my $class = shift; | |||||||||||||||||||||||||||||||
| 42 | 3 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||
| 43 | 3 | return $self->init( @_ ); | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | sub init { | ||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 47 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 48 | 3 | my %args = @_; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | $self->num_inputs( $args{Inputs} || 1 ) | ||||||||||||||||||||||||||||||||
| 51 | ->learning_rate( $args{N} || 0.05 ) | ||||||||||||||||||||||||||||||||
| 52 | ->max_iterations( -1 ) | ||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | ->threshold( $args{T} || 0.0 ) | ||||||||||||||||||||||||||||||
|
Condition83%5/6
$args{'Inputs'} || 1
$args{'N'} || 0.05
$args{'T'} || 0.0
MC/DC100%1/1
$args{'Inputs'} || 1
$args{'Inputs'}
MC/DC100%1/1
$args{'N'} || 0.05
$args{'N'}
MC/DC0%0/1
$args{'T'} || 0.0
$args{'T'}
Truth table100%2/2
A
A= $args{'Inputs'}
Truth table100%2/2
A
A= $args{'N'}
Truth table50%1/2
A
A= $args{'T'}
| |||||||||||||||||||||||||||||||||
| 54 | ->training_examples( [] ) | ||||||||||||||||||||||||||||||||
| 55 | ->weights( [] ); | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # DEPRECATED: backwards compat | ||||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if ($args{W}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 1 | $self->threshold( shift @{ $args{W} } ) | |||||||||||||||||||||||||||||||
| 60 | 1 | ->weights( [ @{ $args{W} } ] ); | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub verify_weights { | ||||||||||||||||||||||||||||||
|
Subroutine
verify_weights: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 67 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 1 | for my $i (0 .. $self->num_inputs-1) { | |||||||||||||||||||||||||||||||
| 70 | 2 | ▶ | $self->weights->[$i] ||= 0.0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->weights->[$i] ||= 0
MC/DC0%0/1
$self->weights->[$i] ||= 0
$self->weights->[$i]
Truth table50%1/2
A
A= $self->weights->[$i]
| |||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # DEPRECATED: backwards compat | ||||||||||||||||||||||||||||||||
| 77 | 100 | ▶ | sub weights { | ||||||||||||||||||||||||||||||
|
Subroutine
weights: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 78 | 100 | my $self = shift; | |||||||||||||||||||||||||||||||
| 79 | 100 | my $ret = $self->_weights(@_); | |||||||||||||||||||||||||||||||
| 80 | 100 | ▶ | return wantarray ? ( $self->threshold, @{ $self->_weights } ) : $ret; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub add_examples { | ||||||||||||||||||||||||||||||
|
Subroutine
add_examples: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 84 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 2 | foreach my $ex (@_) { | |||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | die "training examples must be arrayrefs!" unless (ref $ex eq 'ARRAY'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 2 | my @inputs = @{$ex}; # be nice, take a copy | |||||||||||||||||||||||||||||||
| 89 | 2 | my $target = shift @inputs; | |||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | die "expected result must be either -1 or 1, not $target!" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | unless (abs $target == 1); | ||||||||||||||||||||||||||||||||
| 92 | # TODO: avoid duplicate entries | ||||||||||||||||||||||||||||||||
| 93 | 2 | push @{ $self->training_examples }, [$target, @inputs]; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub add_example { | ||||||||||||||||||||||||||||||
|
Subroutine
add_example: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 100 | 0 | shift->add_examples(@_); | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 31 | ▶ | sub compute_output { | ||||||||||||||||||||||||||||||
|
Subroutine
compute_output: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 104 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||
| 105 | 31 | my @inputs = @_; | |||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 31 | my $sum = $self->threshold; # start at threshold | |||||||||||||||||||||||||||||||
| 108 | 31 | for my $i (0 .. $self->num_inputs-1) { | |||||||||||||||||||||||||||||||
| 109 | 62 | $sum += $self->weights->[$i] * $inputs[$i]; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | # binary (returning the real $sum is not part of this model) | ||||||||||||||||||||||||||||||||
| 113 | 31 | ▶ | return ($sum > 0) ? 1 : -1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | ## | ||||||||||||||||||||||||||||||||
| 117 | # $p->train( [ @training_examples ] ) | ||||||||||||||||||||||||||||||||
| 118 | # \--> [ $target_output, @inputs ] | ||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | sub train { | ||||||||||||||||||||||||||||||
|
Subroutine
train: called CC=8 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 120 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | $self->add_examples( @_ ) if @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 1 | $self->verify_weights; | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | # adjust the weights for each training example until the output | ||||||||||||||||||||||||||||||||
| 126 | # function correctly classifies all the training examples. | ||||||||||||||||||||||||||||||||
| 127 | 1 | my $iter = 0; | |||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | while(! $self->classifies_examples_correctly ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 14 | ▶ | if (($self->max_iterations > 0) and | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->max_iterations > 0 and $iter >= $self->max_iterations
MC/DC0%0/2
$self->max_iterations > 0 and $iter >= $self->max_iterations
$self->max_iterations > 0
$iter >= $self->max_iterations
Truth table33%1/3
A and B
A= $self->max_iterations > 0B= $iter >= $self->max_iterations
| |||||||||||||||||||||||||||||||||
| 131 | ($iter >= $self->max_iterations)) { | ||||||||||||||||||||||||||||||||
| 132 | 0 | $self->emit( "stopped training after $iter iterations" ); | |||||||||||||||||||||||||||||||
| 133 | 0 | return; | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 14 | $iter++; | |||||||||||||||||||||||||||||||
| 137 | 14 | $self->emit( "Training iteration $iter" ); | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 14 | foreach my $training_example (@{ $self->training_examples }) { | |||||||||||||||||||||||||||||||
| 140 | 14 | my ($expected_output, @inputs) = @$training_example; | |||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 14 | ▶ | $self->emit( "Training X=<", join(',', @inputs), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | "> with target $expected_output" ) if $Debug > 1; | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | # want the perceptron's output equal to training output | ||||||||||||||||||||||||||||||||
| 146 | # TODO: this duplicates work by classifies_examples_correctly() | ||||||||||||||||||||||||||||||||
| 147 | 14 | my $output = $self->compute_output(@inputs); | |||||||||||||||||||||||||||||||
| 148 | 14 | ▶ | next if ($output == $expected_output); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 14 | $self->adjust_threshold( $expected_output, $output ) | |||||||||||||||||||||||||||||||
| 151 | ->adjust_weights( \@inputs, $expected_output, $output ); | ||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 1 | $self->emit( "completed in $iter iterations." ); | |||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | # return true unless all training examples are correctly classified | ||||||||||||||||||||||||||||||||
| 161 | 15 | ▶ | sub classifies_examples_correctly { | ||||||||||||||||||||||||||||||
|
Subroutine
classifies_examples_correctly: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 162 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||
| 163 | 15 | my $training_examples = $self->training_examples; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 15 | foreach my $training_example (@$training_examples) { | |||||||||||||||||||||||||||||||
| 166 | 15 | my ($output, @inputs) = @{$training_example}; | |||||||||||||||||||||||||||||||
| 167 | 15 | ▶ | return if ($self->compute_output( @inputs ) != $output); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 1 | return 1; | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 14 | ▶ | sub adjust_threshold { | ||||||||||||||||||||||||||||||
|
Subroutine
adjust_threshold: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 174 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||
| 175 | 14 | my $expected_output = shift; | |||||||||||||||||||||||||||||||
| 176 | 14 | my $output = shift; | |||||||||||||||||||||||||||||||
| 177 | 14 | my $n = $self->learning_rate; | |||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 14 | my $delta = $n * ($expected_output - $output); | |||||||||||||||||||||||||||||||
| 180 | 14 | $self->threshold( $self->threshold + $delta ); | |||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 14 | return $self; | |||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 14 | ▶ | sub adjust_weights { | ||||||||||||||||||||||||||||||
|
Subroutine
adjust_weights: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 186 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||
| 187 | 14 | my $inputs = shift; | |||||||||||||||||||||||||||||||
| 188 | 14 | my $expected_output = shift; | |||||||||||||||||||||||||||||||
| 189 | 14 | my $output = shift; | |||||||||||||||||||||||||||||||
| 190 | 14 | my $n = $self->learning_rate; | |||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | 14 | for my $i (0 .. $self->num_inputs-1) { | |||||||||||||||||||||||||||||||
| 193 | 28 | my $delta = $n * ($expected_output - $output) * $inputs->[$i]; | |||||||||||||||||||||||||||||||
| 194 | 28 | $self->weights->[$i] += $delta; | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 14 | return $self; | |||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 15 | ▶ | sub emit { | ||||||||||||||||||||||||||||||
|
Subroutine
emit: called CC=3 SCAR=19.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 201 | 15 | ▶ | return unless $Debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | push @_, "\n" unless grep /\n/, @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 0 | warn( @_ ); | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 2 | 1; | |||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | __END__ | ||||||||||||||||||||||||||||||||