AI-ML

 view release on metacpan or  search on metacpan

lib/AI/ML/Expr.pm  view on Meta::CPAN

# ABSTRACT: turns baubles into trinkets

package AI::ML::Expr;
use strict;
use warnings;

use Chart::Gnuplot;
use Scalar::Util 'blessed';
use AI::ML;
use Math::Lapack;
use aliased 'Math::Lapack::Matrix' => 'M';


use parent 'Exporter';
use parent 'Math::Lapack::Expr';
our @EXPORT = qw(mini_batch tanh sigmoid relu lrelu d_sigmoid d_relu d_lrelu d_tanh softmax sigmoid_cost plot plot_cost);
use Math::Lapack::Expr;

sub _bless {
    my $matrix = shift;
    return bless { _matrix => $matrix, type => 'matrix' } => "Math::Lapack::Matrix";
}

=head2 sigmoid

Allow apply the function sigmoid to every element of the matrix.

    $m = $m->sigmoid();
    $m = sigmoid($m);

=cut

sub sigmoid {
    my ($self) = @_;

    return bless { package => __PACKAGE__, type => 'sigmoid', args => [$self] } => __PACKAGE__
}

sub eval_sigmoid {
    my $tree = shift;
    if (blessed($tree) && $tree->isa("Math::Lapack::Matrix")) {
      return _bless _sigmoid($tree->matrix_id);
    }

    die "Sigmoid for non matrix: " . ref($tree);
}

=head2 relu

Allows apply the function relu to every element of the matrix.

    $m = $m->relu();
    $m = relu($m);

=cut

sub relu {
    my ($self) = @_;
    return bless { package => __PACKAGE__, type => 'relu', args => [$self] } => __PACKAGE__;
}

sub eval_relu {
    my $tree = shift;
    if (ref($tree) eq "Math::Lapack::Matrix") {
        return _bless _relu($tree->matrix_id);
    }
    die "ReLU for non matrix";
}

=head2 d_relu

Allows apply the function d_relu to every element of the matrix.

    $m = $m->d_relu();
    $m = d_relu($m);



( run in 0.520 second using v1.01-cache-2.11-cpan-39bf76dae61 )