AI-ActivationFunctions
view release on metacpan or search on metacpan
AI-ActivationFunctions-0.01/AI-ActivationFunctions-0.01/examples/simple.pl view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use AI::ActivationFunctions qw(:all);
print "=== Demonstração Completa ===\n\n";
print "1. Funções Básicas:\n";
printf(" relu(5) = %d\n", relu(5));
printf(" relu(-3) = %d\n", relu(-3));
printf(" prelu(-2, 0.1) = %.1f\n", prelu(-2, 0.1));
printf(" sigmoid(0) = %.4f\n", sigmoid(0));
printf(" tanh(1) = %.4f\n", tanh(1));
print "\n2. Funções Avançadas:\n";
printf(" elu(-1, 1) = %.4f\n", elu(-1, 1));
printf(" swish(1) = %.4f\n", swish(1));
printf(" gelu(1) = %.4f\n", gelu(1));
print "\n3. Derivadas (para backpropagation):\n";
printf(" relu_derivative(5) = %d\n", relu_derivative(5));
printf(" relu_derivative(-5) = %d\n", relu_derivative(-5));
printf(" sigmoid_derivative(0) = %.4f\n", sigmoid_derivative(0));
print "\n4. Softmax (distribuição):\n";
my $scores = [1.0, 2.0, 3.0];
my $probs = softmax($scores);
printf(" Entrada: [%.1f, %.1f, %.1f]\n", @$scores);
printf(" SaÃda: [%.4f, %.4f, %.4f]\n", @$probs);
printf(" Soma: %.4f\n", $probs->[0] + $probs->[1] + $probs->[2]);
print "\n=== Fim da Demonstração ===\n";
AI-ActivationFunctions-0.01/AI-ActivationFunctions-0.01/t/basic.t view on Meta::CPAN
# Carregar e importar
use AI::ActivationFunctions qw(relu prelu leaky_relu sigmoid tanh softmax);
# Teste 1: ReLU
is(relu(5), 5, 'relu(5) = 5');
is(relu(-5), 0, 'relu(-5) = 0');
is(relu(0), 0, 'relu(0) = 0');
# Teste 2: PReLU
is(prelu(2, 0.1), 2, 'prelu(2, 0.1) = 2');
is(sprintf("%.1f", prelu(-2, 0.1)), "-0.2", 'prelu(-2, 0.1) = -0.2');
is(sprintf("%.2f", prelu(-2)), "-0.02", 'prelu(-2) com alpha padrão = -0.02');
# Teste 3: Leaky ReLU
is(leaky_relu(2), 2, 'leaky_relu(2) = 2');
is(sprintf("%.2f", leaky_relu(-2)), "-0.02", 'leaky_relu(-2) = -0.02');
# Teste 4: Sigmoid
my $sigmoid0 = sigmoid(0);
ok($sigmoid0 > 0.49 && $sigmoid0 < 0.51, "sigmoid(0) â 0.5 ($sigmoid0)");
my $sigmoid1 = sigmoid(1);
ok($sigmoid1 > 0.73 && $sigmoid1 < 0.74, "sigmoid(1) â 0.731 ($sigmoid1)");
# Teste 5: Tanh
my $tanh0 = tanh(0);
examples/neural_network.pl view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use AI::ActivationFunctions qw(relu sigmoid softmax relu_derivative sigmoid_derivative);
print "=== Simple Neural Network Demo ===\n\n";
# Simple neural network layer simulation
sub neural_layer {
my ($inputs, $weights, $biases, $activation) = @_;
# Linear transformation: Wx + b
my @output;
for my $i (0..$#$weights) {
my $sum = $biases->[$i];
for my $j (0..$#$inputs) {
examples/neural_network.pl view on Meta::CPAN
{ input => [1, 0], output => [1] },
{ input => [1, 1], output => [1] },
);
# Initialize weights and biases randomly
my @weights = (
[rand() - 0.5, rand() - 0.5], # hidden layer weights
);
my @biases = (rand() - 0.5);
print "Initial weights: [", $weights[0][0], ", ", $weights[0][1], "]\n";
print "Initial bias: ", $biases[0], "\n\n";
# Simple forward pass
print "Forward pass through network:\n";
foreach my $example (@training_data) {
my $input = $example->{input};
my $target = $example->{output}[0];
# Forward pass
my $hidden = neural_layer($input, \@weights, \@biases, \&sigmoid);
my $prediction = $hidden->[0];
# Calculate error
my $error = $target - $prediction;
printf("Input: [%d, %d] -> Prediction: %.4f (Target: %d, Error: %.4f)\n",
$input->[0], $input->[1], $prediction, $target, $error);
}
# Backpropagation example
print "\nBackpropagation step (simplified):\n";
my $example = $training_data[1]; # [0
examples/simple.pl view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use AI::ActivationFunctions qw(:all);
print "=== Demonstração Completa ===\n\n";
print "1. Funções Básicas:\n";
printf(" relu(5) = %d\n", relu(5));
printf(" relu(-3) = %d\n", relu(-3));
printf(" prelu(-2, 0.1) = %.1f\n", prelu(-2, 0.1));
printf(" sigmoid(0) = %.4f\n", sigmoid(0));
printf(" tanh(1) = %.4f\n", tanh(1));
print "\n2. Funções Avançadas:\n";
printf(" elu(-1, 1) = %.4f\n", elu(-1, 1));
printf(" swish(1) = %.4f\n", swish(1));
printf(" gelu(1) = %.4f\n", gelu(1));
print "\n3. Derivadas (para backpropagation):\n";
printf(" relu_derivative(5) = %d\n", relu_derivative(5));
printf(" relu_derivative(-5) = %d\n", relu_derivative(-5));
printf(" sigmoid_derivative(0) = %.4f\n", sigmoid_derivative(0));
print "\n4. Softmax (distribuição):\n";
my $scores = [1.0, 2.0, 3.0];
my $probs = softmax($scores);
printf(" Entrada: [%.1f, %.1f, %.1f]\n", @$scores);
printf(" SaÃda: [%.4f, %.4f, %.4f]\n", @$probs);
printf(" Soma: %.4f\n", $probs->[0] + $probs->[1] + $probs->[2]);
print "\n=== Fim da Demonstração ===\n";
# Carregar e importar
use AI::ActivationFunctions qw(relu prelu leaky_relu sigmoid tanh softmax);
# Teste 1: ReLU
is(relu(5), 5, 'relu(5) = 5');
is(relu(-5), 0, 'relu(-5) = 0');
is(relu(0), 0, 'relu(0) = 0');
# Teste 2: PReLU
is(prelu(2, 0.1), 2, 'prelu(2, 0.1) = 2');
is(sprintf("%.1f", prelu(-2, 0.1)), "-0.2", 'prelu(-2, 0.1) = -0.2');
is(sprintf("%.2f", prelu(-2)), "-0.02", 'prelu(-2) com alpha padrão = -0.02');
# Teste 3: Leaky ReLU
is(leaky_relu(2), 2, 'leaky_relu(2) = 2');
is(sprintf("%.2f", leaky_relu(-2)), "-0.02", 'leaky_relu(-2) = -0.02');
# Teste 4: Sigmoid
my $sigmoid0 = sigmoid(0);
ok($sigmoid0 > 0.49 && $sigmoid0 < 0.51, "sigmoid(0) â 0.5 ($sigmoid0)");
my $sigmoid1 = sigmoid(1);
ok($sigmoid1 > 0.73 && $sigmoid1 < 0.74, "sigmoid(1) â 0.731 ($sigmoid1)");
# Teste 5: Tanh
my $tanh0 = tanh(0);
t/extended.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 8; # Plan primeiro
# Importar funções especÃficas
use AI::ActivationFunctions qw(elu swish gelu relu_derivative sigmoid_derivative);
# Teste ELU
my $elu_pos = elu(1, 1);
is(sprintf("%.1f", $elu_pos), "1.0", 'elu(1,1) = 1.0');
my $elu_neg = elu(-1, 1);
ok($elu_neg > -0.64 && $elu_neg < -0.63, "elu(-1,1) â -0.632 ($elu_neg)");
# Teste Swish
my $swish1 = swish(1);
ok($swish1 > 0.73 && $swish1 < 0.74, "swish(1) â 0.731 ($swish1)");
# Teste GELU
my $gelu0 = gelu(0);
test_all.sh view on Meta::CPAN
# 1. Crie a estrutura
#mkdir -p AI-ActivationFunctions-simple/lib/AI
#mkdir AI-ActivationFunctions-simple/t
#mkdir AI-ActivationFunctions-simple/examples
#cd AI-ActivationFunctions-simple
# 2. Copie os arquivos acima para seus respectivos diretórios
# 3. Teste
perl -Ilib -e "use AI::ActivationFunctions; print 'Carregou!\\n';"
# 4. Execute o teste rápido
perl test_quick.pl
# 5. Execute os testes formais
prove -Ilib t/
# 6. Execute o exemplo
perl examples/simple.pl
test_minimal.pl view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
# Teste MÃNIMO - sem usar o módulo ainda
print "1. Testando funções básicas...\n";
# Defina as funções localmente primeiro
sub relu {
my ($x) = @_;
return $x > 0 ? $x : 0;
}
sub tanh_simple {
my ($x) = @_;
my $e2x = exp(2 * $x);
return ($e2x - 1) / ($e2x + 1);
}
# Teste
print " relu(5) = " . relu(5) . " (esperado: 5)\n";
print " relu(-3) = " . relu(-3) . " (esperado: 0)\n";
print " tanh(0) = " . tanh_simple(0) . " (esperado: ~0)\n";
print "\n2. Agora testando o módulo...\n";
# Tente carregar o módulo
eval {
# Adiciona lib ao @INC
unshift @INC, 'lib';
require AI::ActivationFunctions;
print " â Módulo carregado\n";
# Testa uma função
my $test = AI::ActivationFunctions::relu(10);
print " â relu(10) = $test\n";
1;
} or do {
print " â Erro: $@\n";
# Mostra o arquivo se houver erro
if (-f 'lib/AI/ActivationFunctions.pm') {
print "\nConteúdo do arquivo (primeiras 20 linhas):\n";
open my $fh, '<', 'lib/AI/ActivationFunctions.pm' or die $!;
my $linenum = 0;
while (<$fh>) {
$linenum++;
print "$linenum: $_";
last if $linenum >= 20;
}
close $fh;
}
};
print "\nFeito!\n";
test_quick.pl view on Meta::CPAN
use FindBin qw($Bin);
use lib "$Bin/lib";
# Testar se o módulo carrega
eval {
require AI::ActivationFunctions;
AI::ActivationFunctions->import(qw(relu prelu sigmoid));
1;
} or die "Erro ao carregar módulo: $@";
print "=== Teste Rápido ===\n\n";
print "relu(5) = " . AI::ActivationFunctions::relu(5) . "\n";
print "relu(-3) = " . AI::ActivationFunctions::relu(-3) . "\n";
print "prelu(-2, 0.1) = " . AI::ActivationFunctions::prelu(-2, 0.1) . "\n";
print "sigmoid(0) = " . AI::ActivationFunctions::sigmoid(0) . "\n";
my $arr = [-2, -1, 0, 1, 2];
my $result = AI::ActivationFunctions::relu($arr);
print "relu([-2,-1,0,1,2]) = [" . join(", ", @$result) . "]\n";
print "\nOK!\n";
( run in 1.224 second using v1.01-cache-2.11-cpan-d8267643d1d )