AI-ML
view release on metacpan or search on metacpan
Matrix *matrix_exp(Matrix *m){
return element_wise(m, exponential, NULL);
}
REAL sigmoid_cost(Matrix *X, Matrix *Y, Matrix *weights){
Matrix *h;
int m, i, size;
m = Y->rows;
h = matrix_sigmoid(dot(X,weights,0,0));
//class1 = matrix_mul(mul_matrices(transpose(Y), matrix_log(h)),-1);
//class2 = mul_matrices(matrix_sum(matrix_mul(Y, -1), 1), matrix_sum(matrix_mul(h, -1), 1));
size = Y->rows * Y->columns;
# ifdef USE_REAL
double cost = 0;
for(i = 0; i < size; i++){
cost += -Y->values[i]*log(h->values[i]) - ( 1 - Y->values[i]) * log(1 - h->values[i]);
}
# else
float cost = 0;
for(i = 0; i < size; i++){
cost += -Y->values[i]*logf(h->values[i]) - ( 1 - Y->values[i]) * logf(1 - h->values[i]);
}
# endif
return cost/m;
}
Matrix *mini_batch(Matrix *m, int start, int size, int axis){
Matrix *r;
int end;
if(start < 0){
lib/AI/ML/NeuralNetwork.pm
run.sh
scripts/load_data.c
scripts/mnist.pl
t/00-report-prereqs.dd
t/00-report-prereqs.t
t/01-activation-funcs.t
t/02-cost_functions.t
t/03-mini-batch.t
t/04-linear-regression.t
t/05-logistic-regression.t
t/06-accuracy-precision-recall-f1.t
t/07-neural-network.t
t/08-gradient-checking.t_
t/W1_grad_check.csv
t/W2_grad_check.csv
t/W3_grad_check.csv
t/b1_grad_check.csv
t/b2_grad_check.csv
t/b3_grad_check.csv
t/dW1_grad_check.csv
t/dW2_grad_check.csv
t/dW3_grad_check.csv
t/dataset_adjetivos.csv
t/db1_grad_check.csv
t/db2_grad_check.csv
t/db3_grad_check.csv
t/logistic.csv
t/pred-nn.csv
t/w1.csv
t/w2.csv
t/x.csv
t/x_grad_check.csv
t/y.csv
t/y_grad_check.csv
xt/author/00-compile.t
xt/author/pod-syntax.t
lib/AI/ML/LogisticRegression.pm view on Meta::CPAN
$self->{reg} = $opts{reg} if exists $opts{reg};
$self->{cost} = $opts{cost} if exists $opts{cost};
$self->{plot} = $opts{plot} if exists $opts{plot};
$self->{n} = exists $opts{n} ? $opts{n} : 100;
$self->{alpha} = exists $opts{alpha} ? $opts{alpha} : 0.1;
return $self;
}
=head2 logistic_regression
considerando x [m,n]
considerando y [m,1]
=cut
sub train {
my ($self, $x, $y) = @_;
my ($lambda, $thetas, $h, $cost, $reg, $reg_thetas, $grad);
lib/AI/ML/LogisticRegression.pm view on Meta::CPAN
my($m, $n) = $x->shape;
$thetas = M->random($n,1);
my @cost_values=();
if ( exists $self->{reg} ) {
$lambda = $self->{reg};
for my $i (1 .. $iters) {
$h = sigmoid($x x $thetas);
$reg = ($lambda / (2 * $m)) * sum( $thetas->slice(x0 => 1) ** 2 );
$cost = (-1 / $m) * sum($y * log($h) + (1 - $y) * log(1-$h)) + $reg;
push @cost_values, $cost->get_element(0,0) if exists $self->{cost};
$reg_thetas = ($lambda / $m) * $thetas;
$reg_thetas->set_element(0,0,0);
$grad = ($x->T x ($h - $y)) / $m;
$thetas = $thetas - $alpha * ( $grad + $reg_thetas );
}
}
else {
for my $i (1 .. $iters) {
$h = sigmoid($x x $thetas);
$cost = (-1 / $m)*sum(($y * log($h)) + ((1-$y) * log(1-$h)));
push @cost_values, $cost->get_element(0,0) if exists $self->{cost};
$grad = ($x->T x ($h - $y)) / $m;
$thetas = $thetas - $alpha * $grad;
}
}
AI::ML::Expr::plot_cost($self->{cost}, @cost_values) if exists $self->{cost};
$self->{thetas} = $thetas;
}
lib/AI/ML/NeuralNetwork.pm view on Meta::CPAN
for ( 1 .. $layers-1){
$i = $_;
$j = $i - 1;
$var->{"Z$i"} = $self->{"l$i"}{w} x $var->{"A$j"} + $self->{"l$i"}{b};
$var->{"A$i"} = $functions->{ $self->{"l$i"}{func} }->($var->{"Z$i"});
$i++;
}
$i--;
if ($iter % 1000 == 0){
$cost = (-1 / $m)*sum(($y * log($var->{"A$i"})) + ((1-$y) * log(1-$var->{"A$i"})));
$cost = $cost->get_element(0,0);
}
#
## back propagation
$var->{"dz$i"} = $var->{"A$i"} - $y;
$aux = $var->{"dz$i"};
#$aux->save_csv("/tmp/DZ$i.csv");
$var->{"dw$i"} = (1 / $m) * ( $var->{"dz$i"} x T($var->{"A$j"}) );
$var->{"db$i"} = (1 / $m) * sum( $var->{"dz$i"} , 0 );
t/05-logistic-regression.t view on Meta::CPAN
#!perl
use Test2::V0 qw'is float done_testing';
use Math::Lapack::Matrix;
use Math::Lapack;
use AI::ML::LogisticRegression;
Math::Lapack->seed_rng(0);
my $x = Math::Lapack::Matrix::read_csv("t/logistic.csv", col_range =>[0,2]);
my $y = Math::Lapack::Matrix::read_csv("t/logistic.csv", col => 3);
is($x->rows, 306, "Right number of rows");
is($y->rows, 306, "Right number of rows");
is($x->columns, 3, "Right number of cols");
is($y->columns, 1, "Right number of cols");
is($x->get_element(0,0),30, "Right element 0,0 of x");
is($x->get_element(1,2),3, "Right element 1,2 of x");
t/05-logistic-regression.t view on Meta::CPAN
is($x->get_element(2,2),0, "Right element 2,2 of x");
is($y->get_element(3,0),1, "Right element 3,0 of y");
is($y->get_element(7,0),2, "Right element 7,0 of y");
$x->norm_std_deviation();
$y = $y - 1;
my $m = AI::ML::LogisticRegression->new(
n => 10000,
alpha => 0.5,
cost => "../../logistcost.png"
);
$m->train($x, $y);
my $thetas = $m->{thetas};
_float($thetas->get_element(0,0), -1.07661735,"Right vale of theta 0,0");
_float($thetas->get_element(1,0), 0.21463009,"Right vale of theta 1,0");
_float($thetas->get_element(2,0), -0.03173973,"Right vale of theta 2,0");
_float($thetas->get_element(3,0), 0.63483062,"Right vale of theta 3,0");
t/05-logistic-regression.t view on Meta::CPAN
my $n = AI::ML::LogisticRegression->new(
n => 10000,
alpha => 0.5,
cost => "../../logistcost_reg.png",
reg => 2
);
$n->train($x, $y);
$thetas = $n->{thetas};
_float($thetas->get_element(0,0), -1.07368839, "Right vale of theta 0,0");
_float($thetas->get_element(1,0), 0.204271, "Right vale of theta 1,0");
_float($thetas->get_element(2,0), -0.02933972, "Right vale of theta 2,0");
_float($thetas->get_element(3,0), 0.60950995, "Right vale of theta 3,0");
t/08-gradient-checking.t_ view on Meta::CPAN
use Test2::V0 qw'is float done_testing';
use Math::Lapack::Matrix;
use AI::ML::NeuralNetwork;
use Math::Lapack;
use Data::Dumper;
Math::Lapack->seed_rng(0);
#my $x = Math::Lapack::Matrix::read_csv("t/logistic.csv", col_range =>[0,2]);
#my $y = Math::Lapack::Matrix::read_csv("t/logistic.csv", col => 3);
my $x = Math::Lapack::Matrix::read_csv("t/x_grad_check.csv");
my $y = Math::Lapack::Matrix::read_csv("t/y_grad_check.csv");
my $params = {};
$params{"w1"} = Math::Lapack::Matrix::read_csv("t/W1_grad_check.csv");
$params{"w2"} = Math::Lapack::Matrix::read_csv("t/W2_grad_check.csv");
$params{"w3"} = Math::Lapack::Matrix::read_csv("t/W3_grad_check.csv");
$params{"b1"} = Math::Lapack::Matrix::read_csv("t/b1_grad_check.csv");
( run in 1.421 second using v1.01-cache-2.11-cpan-49f99fa48dc )