AI-ActivationFunctions

 view release on metacpan or  search on metacpan

AI-ActivationFunctions-0.01/AI-ActivationFunctions-0.01/lib/AI/ActivationFunctions.pm  view on Meta::CPAN

        push @exp_vals, $exp_val;
        $sum += $exp_val;
    }
    
    # Normalizar
    return [map { $_ / $sum } @exp_vals];
}

# ELU (Exponential Linear Unit)
sub elu {
    my ($x, $alpha) = @_;
    $alpha //= 1.0;
    return $x > 0 ? $x : $alpha * (exp($x) - 1);
}

# Swish (Google)
sub swish {
    my ($x) = @_;
    return $x * sigmoid($x);
}

# GELU (Gaussian Error Linear Unit)
sub gelu {
    my ($x) = @_;
    return 0.5 * $x * (1 + tanh(sqrt(2/3.141592653589793) * 
        ($x + 0.044715 * $x**3)));
}

# Derivada da ReLU
sub relu_derivative {
    my ($x) = @_;
    return $x > 0 ? 1 : 0;
}

# Derivada da Sigmoid
sub sigmoid_derivative {
    my ($x) = @_;
    my $s = sigmoid($x);
    return $s * (1 - $s);
}

1;


=head1 NAME

AI::ActivationFunctions - Activation functions for neural networks in Perl

=head1 VERSION

Version 0.01

=head1 ABSTRACT

Activation functions for neural networks in Perl

=head1 SYNOPSIS

    use AI::ActivationFunctions qw(relu prelu sigmoid);

    my $result = relu(-5);  # returns 0
    my $prelu_result = prelu(-2, 0.1);  # returns -0.2

    # Array version works too
    my $array_result = relu([-2, -1, 0, 1, 2]);  # returns [0, 0, 0, 1, 2]

=head1 DESCRIPTION

This module provides various activation functions commonly used in neural networks
and machine learning. It includes basic functions like ReLU and sigmoid, as well
as advanced functions like GELU and Swish.

=head1 FUNCTIONS

=head2 Basic Functions

=over 4

=item * relu($input)

Rectified Linear Unit. Returns max(0, $input).

=item * prelu($input, $alpha=0.01)

Parametric ReLU. Returns $input if $input > 0, else $alpha * $input.

=item * leaky_relu($input)

Leaky ReLU with alpha=0.01.

=item * sigmoid($input)

Sigmoid function: 1 / (1 + exp(-$input)).

=item * tanh($input)

Hyperbolic tangent function.

=item * softmax(\@array)

Softmax function for probability distributions.

=back

=head2 Advanced Functions

=over 4

=item * elu($input, $alpha=1.0)

Exponential Linear Unit.

=item * swish($input)

Swish activation function.

=item * gelu($input)

Gaussian Error Linear Unit (used in transformers like BERT, GPT).

=back

=head2 Derivatives

=over 4



( run in 1.173 second using v1.01-cache-2.11-cpan-df04353d9ac )