AI-ML
view release on metacpan or search on metacpan
lib/AI/ML/NeuralNetwork.pm view on Meta::CPAN
# ABSTRACT: turns baubles into trinkets
package AI::ML::NeuralNetwork;
use strict;
use warnings;
use Scalar::Util 'blessed';
use Math::Lapack::Matrix;
use Math::Lapack::Expr;
use parent 'AI::ML::Expr';
my $functions = {
sigmoid => \&AI::ML::Expr::sigmoid,
relu => \&AI::ML::Expr::relu,
lrelu => \&AI::ML::Expr::lrelu,
softmax => \&AI::ML::Expr::softmax,
tanh => \&AI::ML::Expr::tanh,
dsigmoid => \&AI::ML::Expr::d_sigmoid,
drelu => \&AI::ML::Expr::d_relu,
dlrelu => \&AI::ML::Expr::d_lrelu,
dtanh => \&AI::ML::Expr::d_tanh
};
=head2 new
=cut
sub new {
my ($self, $layers, %opts) = @_;
$self = bless {} => 'AI::ML::NeuralNetwork';
my $i = 0;
for my $href ( @$layers ) {
if( $i == 0 ){
$self->{"l$i"} = { units => $href };
}
else {
if( $href =~ qw.^\d+$. ){
$self->{"l$i"} = { units => $href, func => "sigmoid", dfunc => "dsigmoid" };
}
elsif( ref($href) eq "HASH" ) {
if ( exists $href->{func} ) {
if ( exists $functions->{$href->{func}} )
{
$self->{"l$i"}{func} = $href->{func};
$self->{"l$i"}{dfunc} = 'd' . $href->{func};
}
else
{
die "Invalid activation function for layer $i: $href->{func}\n";
}
}
else {
$self->{"l$i"}{func} = "sigmoid";
}
if( exists($href->{units}) && $href->{units} =~ qw. ^\d+$ . ) {
$self->{"l$i"}{units} = $href->{units};
} else{
die "undefined number of units in layer $i\n";
}
}
}
$i++;
}
$self->load_weights_bias();
$self->{n} = exists $opts{n} ? $opts{n} : 100;
$self->{alpha} = exists $opts{alpha} ? $opts{alpha} : 0.1;
$self->{reg} = exists $opts{reg} ? $opts{reg} : undef;
$self->{cost} = exists $opts{cost} ? $opts{cost} : undef;
$self->{plot} = exists $opts{plot} ? $opts{plot} : undef;
( run in 2.604 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )