blib/lib/AI/NeuralNet/Simple.pm

filter: Statementstmt 91.6% 66 / 72 Branchbran 76.9% 20 / 26 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 93.7% 15 / 16 Podpod 75.0% 9 / 12 totaltotal 80.8% 118 / 146 CC 19 SCAR 12.1
CC
19
Cov
84%
new
6.0
iterations
4.9
learn_rate
4.0
CRAP 3.4
↑ Summary Simple.xs »
1 package AI::NeuralNet::Simple;
2  
3 4 ▶use Log::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use vars qw( $REVISION $VERSION @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 $REVISION = '$Id: Simple.pm,v 1.3 2004/01/31 20:34:11 ovid Exp $';
10 4 $VERSION = '0.11';
11  
12 4 ▶if ( $] >= 5.006 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.006) { } 4 0 4
13 4 require XSLoader;
14 4 XSLoader::load( 'AI::NeuralNet::Simple', $VERSION );
15 }
16 else {
17 0 require DynaLoader;
18 0 push @ISA, 'DynaLoader';
19 0 AI::NeuralNet::Simple->bootstrap($VERSION);
20 }
21  
22 80042 ▶sub handle { $_[0]->{handle} }
Subroutine handle: called CC=1 SCAR=0.0
Pod undocumented
23  
24 7 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
25 7 my ( $class, @args ) = @_;
26 7 ▶ logdie "you must supply three positive integers to new()"
Branch: 1 branch
BranchTrueFalseTotal
unless 3 == @args 1 6 7
27 unless 3 == @args;
28 6 foreach (@args) {
29 16 ▶ logdie "arguments to new() must be positive integers"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and /^\d+$/ 1 15 16
Condition66%2/3
defined $_ and /^\d+$/
!ll&&!rl&&r
0115
MC/DC50%1/2
defined $_ and /^\d+$/
defined $_ /^\d+$/
Truth table66%2/3
A and B
A= defined $_B= /^\d+$/
ABresult
0X0
100
111
30 unless defined $_ && /^\d+$/;
31 }
32 5 my $seed = rand(1); # Perl invokes srand() on first call to rand()
33 5 my $handle = c_new_network(@args);
34 5 ▶ logdie "could not create new network" unless $handle >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle >= 0 0 5 5
35 5 my $self = bless {
36 input => $args[0],
37 hidden => $args[1],
38 output => $args[2],
39 handle => $handle,
40 }, $class;
41 5 $self->iterations(10000); # set a reasonable default
42 }
43  
44 80001 ▶sub train {
Subroutine train: called CC=1 SCAR=0.0
Pod documented
45 80001 my ( $self, $inputref, $outputref ) = @_;
46 80001 return c_train( $self->handle, $inputref, $outputref );
47 }
48  
49 1 ▶sub train_set {
Subroutine train_set: called CC=3 SCAR=12.0
Pod documented
50 1 my ( $self, $set, $iterations, $mse ) = @_;
51 1 ▶ $iterations ||= $self->iterations;
Condition33%1/3
$iterations ||= $self->iterations
l!l&&r!l&&!r
100
MC/DC0%0/2
$iterations ||= $self->iterations
$iterations $self->iterations
Truth table33%1/3
A ||= B
A= $iterationsB= $self->iterations
ABresult
1X1
011
000
52 1 ▶ $mse = -1.0 unless defined $mse;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mse 0 1 1
53 1 return c_train_set( $self->handle, $set, $iterations, $mse );
54 }
55  
56 5 ▶sub iterations {
Subroutine iterations: called CC=4 SCAR=15.9
Pod documented
57 5 my ( $self, $iterations ) = @_;
58 5 ▶ if ( defined $iterations ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iterations) 5 0 5
59 5 ▶ logdie "iterations() value must be a positive integer."
Branch: 1 branch
BranchTrueFalseTotal
unless $iterations and $iterations =~ /^\d+$/ 0 5 5
Condition33%1/3
$iterations and $iterations =~ /^\d+$/
!ll&&!rl&&r
005
MC/DC0%0/2
$iterations and $iterations =~ /^\d+$/
$iterations $iterations =~ /^\d+$/
Truth table33%1/3
A and B
A= $iterationsB= $iterations =~ /^\d+$/
ABresult
0X0
100
111
60 unless $iterations
61 and $iterations =~ /^\d+$/;
62 5 $self->{iterations} = $iterations;
63 5 return $self;
64 }
65 0 $self->{iterations};
66 }
67  
68 3 ▶sub delta {
Subroutine delta: called CC=3 SCAR=11.0
Pod documented
69 3 my ( $self, $delta ) = @_;
70 3 ▶ return c_get_delta( $self->handle ) unless defined $delta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delta 1 2 3
71 2 ▶ logdie "delta() value must be a positive number" unless $delta > 0.0;
Branch: 1 branch
BranchTrueFalseTotal
unless $delta > 0 0 2 2
72 2 c_set_delta( $self->handle, $delta );
73 2 return $self;
74 }
75  
76 3 ▶sub use_bipolar {
Subroutine use_bipolar: called CC=2 SCAR=6.9
Pod documented
77 3 my ( $self, $bipolar ) = @_;
78 3 ▶ return c_get_use_bipolar( $self->handle ) unless defined $bipolar;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bipolar 1 2 3
79 2 c_set_use_bipolar( $self->handle, $bipolar );
80 2 return $self;
81 }
82  
83 0 ▶sub infer {
Subroutine infer: not called CC=1 SCAR=6.9
Pod documented
84 0 my ( $self, $data ) = @_;
85 0 c_infer( $self->handle, $data );
86 }
87  
88 16 ▶sub winner {
Subroutine winner: called CC=3 SCAR=11.0
Pod documented
89  
90 # returns index of largest value in inferred answer
91 16 my ( $self, $data ) = @_;
92 16 my $arrayref = c_infer( $self->handle, $data );
93  
94 16 my $largest = 0;
95 16 for ( 0 .. $#$arrayref ) {
96 32 ▶ $largest = $_ if $arrayref->[$_] > $arrayref->[$largest];
Branch: 1 branch
BranchTrueFalseTotal
if $arrayref->[$_] > $arrayref->[$largest] 10 22 32
97 }
98 16 return $largest;
99 }
100  
101 12 ▶sub learn_rate {
Subroutine learn_rate: called CC=4 SCAR=13.9
Pod documented
102 12 my ( $self, $rate ) = @_;
103 12 ▶ return c_get_learn_rate( $self->handle ) unless defined $rate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rate 6 6 12
104 6 ▶ logdie "learn rate must be between 0 and 1, exclusive"
Branch: 1 branch
BranchTrueFalseTotal
unless $rate > 0 and $rate < 1 1 5 6
Condition66%2/3
$rate > 0 and $rate < 1
!ll&&!rl&&r
015
MC/DC50%1/2
$rate > 0 and $rate < 1
$rate > 0 $rate < 1
Truth table66%2/3
A and B
A= $rate > 0B= $rate < 1
ABresult
0X0
100
111
105 unless $rate > 0 && $rate < 1;
106 5 c_set_learn_rate( $self->handle, $rate );
107 5 return $self;
108 }
109  
110 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
111 6 my $self = shift;
112 6 c_destroy_network( $self->handle );
113 }
114  
115 #
116 # Serializing hook for Storable
117 #
118  
119 1 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
120 1 my ( $self, $cloning ) = @_;
121 1 my $internal = c_export_network( $self->handle );
122  
123 # This is an excellent example where "we know better" than
124 # the recommended way in Storable's man page...
125 # Behaviour is the same whether we're cloning or not --RAM
126  
127 1 my %copy = %$self;
128 1 delete $copy{handle};
129  
130 1 return ( "", \%copy, $internal );
131 }
132  
133 #
134 # Deserializing hook for Storable
135 #
136 1 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
137 1 my ( $self, $cloning, $x, $copy, $internal ) = @_;
138 1 %$self = %$copy;
139 1 $self->{handle} = c_import_network($internal);
140 }
141  
142 4 1;
143  
144 __END__
↑ Summary Simple.xs »