AI-NNFlex
view release on metacpan
or search on metacpan
CHANGES
view on Meta::CPAN
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | also about 50% faster ( do you sense a theme developing here?)
Removed momentum module (well, removed backprop module and
renamed momentum module in fact). There were few code differences
and its easier to maintain this way. Default option is vanilla
backprop, momentum & fahlman adjustments are only implemented if
specified in the network config.
Bundled all the maths functions into AI::NNFlex::Mathlib
Implemented calls to error transformation function, as per
Fahlman. Testing, it doesn't seem to make much difference, but
at least now the facility is there.
0.20
20050308
v0.17 was never released, as I rejigged the whole lot for
object inheritance before I got around to uploading it to CPAN.
|
CHANGES
view on Meta::CPAN
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | will be put back later (perhaps) or incorporated into a separate GUI
frontend.
Removed feedforward_pdl.pm from the distribution - it shouldn't have
been there in the first place!
Fixed the lesion subs so they expect parameter =>value pairs instead
of an anonymous hash (left over from when I didn't know you could do
that).
Fixed an error - random weights was bounded by 1, not the parameter
'randomweights' . Its now positive only. Some benchmarking needed as
it appears that positive random starting weights rather than a mix
of positive and negative make the network quicker to converge, at
least with momentum.
weights now defaults to rand (1) instead of 0 - at least for backprop
type nets, a default 0 weight will never work. For other types of nets
the random weights can be overridden with the 'fixedweights' parameter.
Fixed load_state to correctly read weights from the bias node
|
CHANGES
view on Meta::CPAN
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | its at the users risk.
See xor.pl or the perldoc & readme for examples.
Cleaned up the perldoc some more. Commented out all the method
perldocs, so there is just the single block defining the
distributions documentation, as advocated by perlmonks. Method
perldocs in importable modules have not been commented out.
Removed the weight bounding in backprop & momentum. If the network
is going into an unstable state the weight bounding won't help,
and it causes errors under perl -w.
Implemented tests (apologies to the CPAN testers for not having
done so before !).
0.13
20050121
|
README.txt
view on Meta::CPAN
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | [1,0],[1],
[1,1],[0]]);
my $counter =0;
my $err = 10;
while ( $err >.001)
{
$err = $dataset ->learn( $network );
print "Epoch = $counter error = $err\n" ;
$counter ++;
}
foreach (@{ $dataset ->run( $network )})
{
foreach ( @$_ ){ print $_ }
print "\n" ;
}
|
TODO
view on Meta::CPAN
1 2 3 4 5 6 7 8 9 10 | Put in some more error checking, particularly trying to create connections
between layers/nodes that don't exist.
Write a simple net simulator with syntax loosely based on xerion. At
present this lot is API driven, it should be straightforward to write
a basic simulator that calls the API in the backend.
read & write methods for both networks and datasets modelled on snns format ( for use with frontend script). data should be snns format , network definition file will probably have to differ
Implement an error method in addition to dbug, and clean up the dbug & error calls
|
examples/add.pl
view on Meta::CPAN
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | [ 10, 10 ], [ 20 ],
[ 15, 15 ], [ 30 ],
[ 12, 8 ], [ 20 ],
]);
my $err = 10;
for ( my $i = 0; ( $err > 0.0001) && ( $i < 4096); $i ++ ) {
$err = $dataset ->learn( $network );
print "Epoch = $i error = $err\n" ;
}
foreach (@{ $dataset ->run( $network )})
{
foreach ( @$_ ){ print $_ }
print "\n" ;
}
print "this should be 4000 - " ;
$network ->run([2000,2000]);
|
examples/bp.pl
view on Meta::CPAN
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | my $numEpochs = 500;
my $numInputs = 3;
my $numHidden = 4;
my $numPatterns = 4;
my $LR_IH = 0.7;
my $LR_HO = 0.07;
my $patNum ;
my $errThisPat ;
my $outPred ;
my $RMSerror ;
my @trainInputs ;
my @trainOutput ;
my @hiddenVal ;
my @weightsIH ;
|
examples/bp.pl
view on Meta::CPAN
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | for ( my $j = 0; $j <= $numEpochs ; $j ++)
{
for ( my $i = 0; $i < $numPatterns ; $i ++)
{
$patNum = ( rand () *$numPatterns )-0.001;
calcNet();
WeightChangesHO();
WeightChangesIH();
}
calcOverallError();
print "epoch = " . $j . " RMS Error = " . $RMSerror . "\n" ;
}
displayResults();
}
|
examples/bp.pl
view on Meta::CPAN
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | }
$outPred = 0.0;
for ( my $i = 0; $i < $numHidden ; $i ++)
{
$outPred = $outPred + $hiddenVal [ $i ] * $weightsHO [ $i ];
}
$errThisPat = $outPred - $trainOutput [ $patNum ];
}
sub WeightChangesHO()
{
for ( my $k = 0; $k < $numHidden ; $k ++)
{
|
examples/bp.pl
view on Meta::CPAN
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | $patNum = $i ;
calcNet();
print "pat = " .( $patNum +1). " actual = " . $trainOutput [ $patNum ]. " neural model = " . $outPred . "\n" ;
}
}
sub calcOverallError()
{
$RMSerror = 0.0;
for ( my $i = 0; $i < $numPatterns ; $i ++)
{
$patNum = $i ;
calcNet();
$RMSerror = $RMSerror + ( $errThisPat * $errThisPat );
}
$RMSerror = $RMSerror / $numPatterns ;
$RMSerror = sqrt ( $RMSerror );
}
|
examples/test.pl
view on Meta::CPAN
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | momentum =>0.6,
round =>1);
$network ->add_layer( nodes =>8,
activationfunction => "tanh" );
$network ->add_layer( nodes =>8,
errorfunction => 'atanh' ,
activationfunction => "tanh" );
$network ->add_layer( nodes =>8,
activationfunction => "linear" );
$network ->init();
my $dataset = AI::NNFlex::Dataset->new(\ @data );
my $counter =0;
my $err = 10;
while ( $err >.01)
{
$err = $dataset ->learn( $network );
print "Epoch = $counter error = $err\n" ;
$counter ++;
}
$network ->run([0,0,0,0,0,1,0,1]);
my $output = $network ->output();
print $output . "\n" ;
foreach ( @$output ){ print $_ }
print "\n" ;
|
examples/xor.pl
view on Meta::CPAN
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | $dataset ->save( filename => 'xor.pat' );
$dataset ->load( filename => 'xor.pat' );
my $counter =0;
my $err = 10;
while ( $err >.001)
{
$err = $dataset ->learn( $network );
print "Epoch = $counter error = $err\n" ;
$counter ++;
}
foreach (@{ $dataset ->run( $network )})
{
foreach ( @$_ ){ print $_ }
print "\n" ;
}
|
examples/xorminus.pl
view on Meta::CPAN
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | $dataset ->save( filename => 'xor.pat' );
$dataset ->load( filename => 'xor.pat' );
my $counter =0;
my $err = 10;
while ( $err >.001)
{
$err = $dataset ->learn( $network );
print "Epoch = $counter error = $err\n" ;
$counter ++;
}
foreach (@{ $dataset ->run( $network )})
{
foreach ( @$_ ){ print $_ }
print "\n" ;
}
|
lib/AI/NNFlex.pm
view on Meta::CPAN
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | if (! $network ->{ 'debug' })
{
@DEBUGLEVELS = @DEBUG ;
}
else
{
@DEBUGLEVELS = @{ $network ->{ 'debug' }};
}
if (!( grep /0/, @DEBUGLEVELS )){ push @DEBUGLEVELS ,0}
foreach ( @DEBUGLEVELS )
{
if ( $level == $_ )
{
print "$message\n" ;
}
}
|
lib/AI/NNFlex.pm
view on Meta::CPAN
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 | $$node { 'activation' } =
rand ( $$params { 'random' });
AI::NNFlex::dbug( $params , "Randomly activated at " . $$node { 'activation' },2);
}
else
{
$$node { 'activation' } = 0;
}
$$node { 'active' } = 1;
$$node { 'error' } = 0;
bless $node , $class ;
AI::NNFlex::dbug( $params , "Created node $node" ,2);
return $node ;
}
sub lesion
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | sub calc_error
{
my $network = shift ;
my $outputPatternRef = shift ;
my @outputPattern = @$outputPatternRef ;
my @debug = @{ $network ->{ 'debug' }};
if ( scalar @debug > 0)
{ $network ->dbug ( "Output pattern @outputPattern received by Backprop" ,4);}
my $outputLayer = $network ->{ 'layers' }->[-1]->{ 'nodes' };
if ( scalar @$outputLayer != scalar @outputPattern )
{
$network ->dbug ( "Wrong number of output values, net has " . scalar @$outputLayer . " nodes" ,0);
return 0;
}
my $counter =0;
foreach ( @$outputLayer )
{
my $value = $_ ->{ 'activation' } - $outputPattern [ $counter ];
if ( $_ ->{ 'errorfunction' })
{
my $errorfunction = $_ ->{ 'errorfunction' };
$value = $network -> $errorfunction ( $value );
}
$_ ->{ 'error' } = $value ;
$counter ++;
if ( scalar @debug > 0)
{ $network ->dbug ( "Error on output node $_ = " . $_ ->{ 'error' },4);}
}
}
sub learn
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | if (! $network ->{ 'fahlmanconstant' })
{
$network ->{ 'fahlmanconstant' } = 0.1;
}
my @outputPattern = @$outputPatternRef ;
$network ->calc_error( $outputPatternRef );
$network ->hiddenToOutput;
if ( scalar @{ $network ->{ 'layers' }} > 2)
{
$network ->hiddenOrInputToHidden;
}
my $Err = $network ->RMSErr( $outputPatternRef );
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | if ( $network ->{ 'momentum' })
{
if ( $node ->{ 'connectedNodesWest' }->{ 'lastdelta' }->[ $connectedNodeCounter ])
{
$momentum = ( $network ->{ 'momentum' })*( $node ->{ 'connectedNodesWest' }->{ 'lastdelta' }->[ $connectedNodeCounter ]);
}
}
if ( scalar @debug > 0)
{ $network ->dbug( "Learning rate is " . $network ->{ 'learningrate' },4);}
my $deltaW = (( $network ->{ 'learningrate' }) * ( $node ->{ 'error' }) * ( $connectedNode ->{ 'activation' }));
$deltaW = $deltaW + $momentum ;
$node ->{ 'connectedNodesWest' }->{ 'lastdelta' }->[ $connectedNodeCounter ] = $deltaW ;
if ( scalar @debug > 0)
{ $network ->dbug( "Applying delta $deltaW on hiddenToOutput $connectedNode to $node" ,4);}
$node ->{ 'connectedNodesWest' }->{ 'weights' }->[ $connectedNodeCounter ] -= $deltaW ;
$connectedNodeCounter ++;
}
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | my @layers = @{ $network ->{ 'layers' }};
my @debug = @{ $network ->{ 'debug' }};
pop @layers ;
if ( scalar @debug > 0)
{ $network ->dbug( "Starting Backprop of error on " . scalar @layers . " hidden layers" ,4);}
foreach my $layer ( reverse @layers )
{
foreach my $node (@{ $layer ->{ 'nodes' }})
{
my $connectedNodeCounter =0;
if (! $node ->{ 'connectedNodesWest' }) { last }
my $nodeError ;
foreach my $connectedNode (@{ $node ->{ 'connectedNodesEast' }->{ 'nodes' }})
{
$nodeError += ( $connectedNode ->{ 'error' }) * ( $connectedNode ->{ 'connectedNodesWest' }->{ 'weights' }->[ $connectedNodeCounter ]);
$connectedNodeCounter ++;
}
if ( scalar @debug > 0)
{ $network ->dbug( "Hidden node $node error = $nodeError" ,4);}
if ( $node ->{ 'errorfunction' })
{
my $functioncall = $node ->{ 'errorfunction' };
$nodeError = $network -> $functioncall ( $nodeError );
}
$node ->{ 'error' } = $nodeError ;
$connectedNodeCounter =0;
foreach my $westNodes (@{ $node ->{ 'connectedNodesWest' }->{ 'nodes' }})
{
my $momentum = 0;
if ( $network ->{ 'momentum' })
{
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | my $value = $node ->{ 'activation' };
my $functionSlope = $node ->{ 'activationfunction' }. "_slope" ;
$value = $network -> $functionSlope ( $value );
$value += $network ->{ 'fahlmanconstant' };
$value = $value * $node ->{ 'error' } * $network ->{ 'learningrate' } * $westNodes ->{ 'activation' };
my $dW = $value ;
$dW = $dW + $momentum ;
if ( scalar @debug > 0)
{ $network ->dbug( "Applying deltaW $dW to inputToHidden connection from $westNodes to $node" ,4);}
$node ->{ 'connectedNodesWest' }->{ 'lastdelta' }->{ $westNodes } = $dW ;
$node ->{ 'connectedNodesWest' }->{ 'weights' }->[ $connectedNodeCounter ] -= $dW ;
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | my $sqrErr ;
my $outputLayer = $network ->{ 'layers' }->[-1]->{ 'nodes' };
if ( scalar @$outputLayer != scalar @outputPattern )
{
$network ->dbug( "Wrong number of output values, net has " . scalar @$outputLayer . " nodes" ,0);
return 0;
}
my $counter =0;
foreach ( @$outputLayer )
{
my $value = $_ ->{ 'activation' } - $outputPattern [ $counter ];
$sqrErr += $value *$value ;
$counter ++;
if ( scalar @debug > 0)
{ $network ->dbug( "Error on output node $_ = " . $_ ->{ 'error' },4);}
}
my $error = sqrt ( $sqrErr );
return $error ;
}
1;
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | momentum
fahlmanconstant
If randomweights is not specified the network will default to a random value from 0 to 1.
If momentum is not specified the network will default to vanilla (non momentum) backprop.
The Fahlman constant modifies the slope of the error curve. 0.1 is the standard value for everything, and speeds the network up immensely. If no Fahlman constant is set, the network will default to 0.1
|
lib/AI/NNFlex/Backprop.pm
view on Meta::CPAN
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
lib/AI/NNFlex/Dataset.pm
view on Meta::CPAN
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | }
sub learn
{
my $self = shift ;
my $network = shift ;
my $error ;
for ( my $itemCounter =0; $itemCounter <( scalar @{ $self ->{ 'data' }}); $itemCounter +=2)
{
$network ->run(@{ $self ->{ 'data' }}[ $itemCounter ]);
$error += $network ->learn(@{ $self ->{ 'data' }}[ $itemCounter +1]);
}
$error = $error *$error ;
return $error ;
}
sub save
{
my $dataset = shift ;
my %config = @_ ;
open (OFILE, ">" . $config { 'filename' }) or return "File error $!" ;
print OFILE "No. of patterns : " .(( scalar @{ $dataset ->{ 'data' }})/2). "\n" ;
print OFILE "No. of input units : " .( scalar @{ $dataset ->{ 'data' }->[0]}). "\n" ;
print OFILE "No. of output units : " .( scalar @{ $dataset ->{ 'data' }->[1]}). "\n\n" ;
my $counter = 1;
my @values = @{ $dataset ->{ 'data' }};
while ( @values )
{
print OFILE "# Input pattern $counter:\n" ;
|
lib/AI/NNFlex/Mathlib.pm
view on Meta::CPAN
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | {
my $network = shift ;
my $value = shift ;
if ( $value <0){ return -1}
if ( $value >0){ return 1}
return $value ;
}
sub atanh
{
my $network = shift ;
my $value = shift ;
if ( $value >-0.5 && $value <0.5)
{
$value = log ((1+ $value )/(1- $value ))/2;
}
return $value ;
|
lib/AI/NNFlex/Mathlib.pm
view on Meta::CPAN
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
lib/AI/NNFlex/Mathlib.pm
view on Meta::CPAN
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |