AI-ML

 view release on metacpan or  search on metacpan

C/nn.c  view on Meta::CPAN

# 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){
        fprintf(stderr, "start index need to be bigger or equal index 0\n");
        exit(1);
    }
    end = start + size - 1;
    if(axis == 0) // every training example is a column
    {
        if(end >= m->columns){
            fprintf(stderr, "Out of index of columns\n");
            exit(1);
        }
        r = slice(m, -1, -1, start, end);
    }
    else if(axis == 1){
        if(end >= m->rows){
            fprintf(stderr, "Out of index of rows\n");
            exit(1);
        }
        r = slice(m, start, end, -1, -1);
    }
    else{
        fprintf(stderr, "Invalid axis\n");
        exit(1);
    }
    return r;
}

Matrix *predict_binary_classification(Matrix *m, REAL threshold){
	Matrix *yatt;

C/nn.h  view on Meta::CPAN

Matrix *sub_matrices(Matrix *A, Matrix *B);

Matrix *mul_matrices(Matrix *A, Matrix *B);

Matrix *div_matrices(Matrix *A, Matrix *B);

Matrix *dot(Matrix *A, Matrix *B, int A_t, int B_t);

Matrix *slice(Matrix *m, int x0, int x1, int y0, int y1);

Matrix *mini_batch(Matrix *m, int start, int size, int axis);
Matrix *sum(Matrix *m, Axis axis);

Matrix *div_matrices(Matrix *A, Matrix *B);

Matrix *broadcasting(Matrix *A, Matrix *B, Axis axis, REAL f(REAL, REAL));

REAL real_mul(REAL a, REAL b);
REAL get_max(Matrix*);
Matrix* matrix_sum(Matrix*, REAL);

LICENSE  view on Meta::CPAN

    you changed the files and the date of any change; and

    b) cause the whole of any work that you distribute or publish, that
    in whole or in part contains the Program or any part thereof, either
    with or without modifications, to be licensed at no charge to all
    third parties under the terms of this General Public License (except
    that you may choose to grant warranty protection to some or all
    third parties, at your option).

    c) If the modified program normally reads commands interactively when
    run, you must cause it, when started running for such interactive use
    in the simplest and most usual way, to print or display an
    announcement including an appropriate copyright notice and a notice
    that there is no warranty (or else, saying that you provide a
    warranty) and that users may redistribute the program under these
    conditions, and telling the user how to view a copy of this General
    Public License.

    d) You may charge a fee for the physical act of transferring a
    copy, and you may at your option offer warranty protection in
    exchange for a fee.

LICENSE  view on Meta::CPAN

                     END OF TERMS AND CONDITIONS

        Appendix: How to Apply These Terms to Your New Programs

  If you develop a new program, and you want it to be of the greatest
possible use to humanity, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.

  To do so, attach the following notices to the program.  It is safest to
attach them to the start of each source file to most effectively convey
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 19yy  <name of author>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

LICENSE  view on Meta::CPAN

    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA


Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

    Gnomovision version 69, Copyright (C) 19xx name of author
    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the
appropriate parts of the General Public License.  Of course, the
commands you use may be called something other than `show w' and `show
c'; they could even be mouse-clicks or menu items--whatever suits your

XS/ML.xs.inc  view on Meta::CPAN

	unsigned long w;

	CODE:
        	RETVAL = newSVnv(sigmoid_cost((Matrix*)x, (Matrix*)y, (Matrix*)w));

	OUTPUT:
		RETVAL


SV *
_mini_batch(m, start, s, axis)
    unsigned long m;
    int start;
    int s;
    int axis;

    PREINIT:
        Matrix* r;

    CODE:
        r = mini_batch((Matrix*)m, start, s, axis);
        RETVAL = newSVnv((unsigned long)r);

    OUTPUT:
        RETVAL

SV *
_accuracy(y, yatt)
	unsigned long y;
	unsigned long yatt;

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

sub sigmoid_cost {
    my ($x, $y, $weights) = @_;
    return _sigmoid_cost($x->matrix_id, $y->matrix_id, $weights->matrix_id);
}


=head2 mini-batch

=cut
sub mini_batch {
    my ($self, $start, $size, $axis) = @_;
    $axis = 0 unless defined $axis; #default
    return _bless _mini_batch($self->matrix_id, $start, $size, $axis);
}


=head2 prediction

=cut
sub prediction {
    my ($self, %opts) = @_;
		my $t = exists $opts{threshold} ? $opts{threshold} : 0.50;
		return _bless _predict_binary_classification($self->matrix_id, $t);

t/03-mini-batch.t  view on Meta::CPAN

#!perl

use Math::Lapack::Matrix;
use Math::Lapack::Expr;
use AI::ML::Expr;

my $m = Math::Lapack::Matrix->random(50,1000);
my $nr_tests = 0;
my $axis = 0;
my $size = 200;
my $start = 0;

for my $v (0..4){
    my $a = mini_batch($m, $start, $size, $axis);
    is($a->rows, 50, "Right number of rows\n");
    is($a->columns, 200, "Right number of columns\n");
    $start += $size;    
}

my $m_1 = Math::Lapack::Matrix->random(1000,20);
$start = 0;
$axis = 1;
for my $i (0..4){
    my $b = mini_batch($m_1, $start, $size, $axis);
    is($b->rows, 200, "Right number of rows\n");
    is($b->columns, 20, "Right number of columns\n");
    $start += $size;    
}
    
print "1..$nr_tests\n";

sub float {
  $nr_tests++;
  my ($a, $b, $explanation) = @_;
  if (abs($a-$b) > 0.000001){
    print "not ";
    $explanation .= " ($a vs $b)";



( run in 0.667 second using v1.01-cache-2.11-cpan-0d8aa00de5b )