| 1 | package AI::Nerl; | ||||||||||||||||||||
| 2 | 2 | ▶ | use Moose 'has',inner => { -as => 'moose_inner' }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | ▶ | use PDL; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use AI::Nerl::Network; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | # ABSTRACT: Neural networks with backpropagation. | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | # main_module | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | our $VERSION = .03; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | #A Nerl is a mechanism to build neural networks? | ||||||||||||||||||||
| 13 | #Give it training,test, and cv data? | ||||||||||||||||||||
| 14 | #it settles on a learning rate and stuff? | ||||||||||||||||||||
| 15 | #or maybe it's also a language for guided training? | ||||||||||||||||||||
| 16 | #or maybe a visual gui thing? | ||||||||||||||||||||
| 17 | #Not exactly sure. Maybe I'm tinkering with forces better left alone. | ||||||||||||||||||||
| 18 | #That's a great excuse for failing horribly. | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 AI::Nerl - A sort of stackable neural network builder thing. | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Check out L<AI::Nerl::Network>; This module is in an early stage. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 AUTHOR | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | Zach Morgan | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 2 | has scale_input => ( | |||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||
| 37 | isa => 'Num', | ||||||||||||||||||||
| 38 | required => 0, | ||||||||||||||||||||
| 39 | default => 0, | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | 2 | has l2 => ( #hidden layer. | |||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||
| 43 | isa => 'Num', | ||||||||||||||||||||
| 44 | default => 30, | ||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 2 | has [qw/ train_x | |||||||||||||||||||
| 48 | train_y /] => ( | ||||||||||||||||||||
| 49 | is => 'ro', | ||||||||||||||||||||
| 50 | isa => 'PDL', | ||||||||||||||||||||
| 51 | required => 0, #training can be done manually. | ||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||
| 53 | 2 | has [qw/ test_x cv_x | |||||||||||||||||||
| 54 | test_y cv_y /] => ( | ||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||
| 56 | isa => 'PDL', | ||||||||||||||||||||
| 57 | required => 0, | ||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | has network => ( | |||||||||||||||||||
| 61 | required=>0, | ||||||||||||||||||||
| 62 | is => 'rw', | ||||||||||||||||||||
| 63 | isa => 'AI::Nerl::Network', | ||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 2 | has passes=> ( | |||||||||||||||||||
| 67 | is => 'rw', | ||||||||||||||||||||
| 68 | isa => 'Int', | ||||||||||||||||||||
| 69 | default => 10, | ||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 2 | has basis => ( | |||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||
| 74 | isa => 'AI::Nerl', | ||||||||||||||||||||
| 75 | required => 0, | ||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | #initialize $self->network, but don't train. | ||||||||||||||||||||
| 79 | # any parameters AI::Nerl::Network takes are fine here. | ||||||||||||||||||||
| 80 | 0 | ▶ | sub init_network{ | ||||||||||||||||||
|
Subroutine
init_network: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||||||||||
| 82 | 0 | my %nn_params = @_; | |||||||||||||||||||
| 83 | #input layer size: | ||||||||||||||||||||
| 84 | 0 | ▶ | unless ($nn_params{l1}){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | ▶ | if ($self->basis){ | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 86 | 0 | $nn_params{l1} = $self->basis->network->l1 + $self->basis->network->l2; | |||||||||||||||||||
| 87 | } elsif($self->train_x) { | ||||||||||||||||||||
| 88 | 0 | ▶ | $nn_params{l1} ||= $self->train_x->dim(1); | ||||||||||||||||||
|
Condition0%0/3
$nn_params{"l1"} ||= $self->train_x->dim(1)
MC/DC0%0/2
$nn_params{"l1"} ||= $self->train_x->dim(1)
$nn_params{"l1"}
$self->train_x->dim(1)
Truth table0%0/3
A ||= B
A= $nn_params{"l1"}B= $self->train_x->dim(1)
| |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | #output layer size: | ||||||||||||||||||||
| 92 | 0 | ▶ | unless ($nn_params{l3}){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | ▶ | if ($self->basis){ | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 94 | 0 | $nn_params{l3} = $self->basis->network->l3; | |||||||||||||||||||
| 95 | } elsif($self->train_x) { | ||||||||||||||||||||
| 96 | 0 | ▶ | $nn_params{l3} ||= $self->train_y->dim(1); | ||||||||||||||||||
|
Condition0%0/3
$nn_params{"l3"} ||= $self->train_y->dim(1)
MC/DC0%0/2
$nn_params{"l3"} ||= $self->train_y->dim(1)
$nn_params{"l3"}
$self->train_y->dim(1)
Truth table0%0/3
A ||= B
A= $nn_params{"l3"}B= $self->train_y->dim(1)
| |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 0 | ▶ | $nn_params{l2} ||= $self->l2; | ||||||||||||||||||
|
Condition0%0/3
$nn_params{"l2"} ||= $self->l2
MC/DC0%0/2
$nn_params{"l2"} ||= $self->l2
$nn_params{"l2"}
$self->l2
Truth table0%0/3
A ||= B
A= $nn_params{"l2"}B= $self->l2
| |||||||||||||||||||||
| 100 | 0 | ▶ | $nn_params{scale_input} ||= $self->scale_input; | ||||||||||||||||||
|
Condition0%0/3
$nn_params{"scale_input"} ||= $self->scale_input
MC/DC0%0/2
$nn_params{"scale_input"} ||= $self->scale_input
$nn_params{"scale_input"}
$self->scale_input
Truth table0%0/3
A ||= B
A= $nn_params{"scale_input"}B= $self->scale_input
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | my $nn = AI::Nerl::Network->new( | |||||||||||||||||||
| 103 | %nn_params | ||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||
| 105 | 0 | $self->network($nn); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 2 | ▶ | sub build_network{ | ||||||||||||||||||
|
Subroutine
build_network: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 109 | 2 | my $self = shift; | |||||||||||||||||||
| 110 | 2 | my $nn = AI::Nerl::Network->new( | |||||||||||||||||||
| 111 | l1 => $self->train_x->dim(1), | ||||||||||||||||||||
| 112 | l2 => $self->l2, | ||||||||||||||||||||
| 113 | l3 => $self->train_y->dim(1), | ||||||||||||||||||||
| 114 | scale_input => $self->scale_input, | ||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||
| 116 | 2 | $nn->train($self->train_x, $self->train_y, passes=>$self->passes); | |||||||||||||||||||
| 117 | 2 | $self->network($nn); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | ▶ | sub append_l2{ | ||||||||||||||||||
|
Subroutine
append_l2: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 121 | 0 | my ($self,$x) = @_; | |||||||||||||||||||
| 122 | 0 | ▶ | if($self->basis){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | $x = $self->basis->append_l2($x); | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | 0 | return $self->network->append_l2($x); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 1 | ▶ | sub run{ | ||||||||||||||||||
|
Subroutine
run: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 130 | 1 | my ($self,$x) = @_; | |||||||||||||||||||
| 131 | 1 | $x->sever; | |||||||||||||||||||
| 132 | 1 | ▶ | if($self->basis){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 0 | $x = $self->basis->append_l2($x); | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | 1 | return $self->network->run($x); | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | 0 | ▶ | sub train{ | ||||||||||||||||||
|
Subroutine
train: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 138 | 0 | my ($self,$x,$y) = @_; | |||||||||||||||||||
| 139 | 0 | $x->sever; | |||||||||||||||||||
| 140 | 0 | ▶ | if($self->basis){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 0 | $x = $self->basis->append_l2($x); | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | 0 | return $self->network->train($x,$y); | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 0 | ▶ | sub cost{ | ||||||||||||||||||
|
Subroutine
cost: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 147 | 0 | my ($self,$x,$y) = @_; | |||||||||||||||||||
| 148 | 0 | $x->sever(); | |||||||||||||||||||
| 149 | 0 | ▶ | if($self->basis){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 0 | $x = $self->basis->append_l2($x); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | 0 | return $self->network->cost($x,$y); | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 2 | 'a neural network has your dog.'; | |||||||||||||||||||