Skip to content

Commit

Permalink
dimensional nonsense crap.
Browse files Browse the repository at this point in the history
  • Loading branch information
zpmorgan committed Aug 3, 2012
1 parent 67d333d commit 1e9a3c0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
30 changes: 22 additions & 8 deletions examples/digits/digits.pl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
sub imag_neuron{
my $foo = shift;
$foo = $foo->reshape(28,28);
$foo = $foo - $foo->min;
$foo /= $foo->max;
$foo = normlz $foo;
warn $foo->sum;
imag2d $foo;
}
sub normlz{
my $foo = shift;
$foo = $foo - $foo->min;
$foo /= $foo->max;
return $foo;
}
sub imag_theta1{
my $t1 = shift;
$t1 = $t1->transpose->reshape(28,28*10)->sever;
imag2d normlz $t1;
}


unless (-e "t10k-labels-idx1-ubyte.flex"){ die <<"NODATA";}
Expand All @@ -39,8 +49,6 @@ sub imag_neuron{
inputs => 784,
outputs => 10,
);
my $foo = $nerl->model->theta1->slice(4);
imag_neuron $foo;

sub y_to_vectors{
my $labels = shift;
Expand All @@ -49,9 +57,9 @@ sub y_to_vectors{
$y *= 2;
$y -= 1;
# die $y->slice("0:9,0:19");
return $y
return $y->transpose
}
for(1..44){
for(1..2){
$nerl->train_batch(
x => $images->slice("0:999"),
y => y_to_vectors $labels->slice("0:999"),
Expand All @@ -61,8 +69,14 @@ sub y_to_vectors{
y => y_to_vectors $labels->slice("100:199"),
);
}
$foo = $nerl->model->theta1->slice(4);
imag_neuron $foo;
print 'num correct:' . ( (
$nerl->classify( $images->slice("100:199"))->flat ==
$labels->slice("100:199")->flat
)->sum
);
imag_theta1 $nerl->model->theta1;
#$foo = $nerl->model->theta1->slice(4);
#imag_neuron ($foo);# * ($foo>1));

# a second __END__ :)
__END__
Expand Down
4 changes: 4 additions & 0 deletions lib/AI/Nerl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ sub _build_model{
}

# er, these go to trainer.
sub classify{
my $self = shift;
return $self->model->classify(@_);
}
sub spew_cost{
my $self = shift;
return $self->model->spew_cost(@_);
Expand Down
25 changes: 23 additions & 2 deletions lib/AI/Nerl/Model/Perceptron3.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,28 @@ sub BUILD{
#a dimensional transform from $inputs to $outputs
sub run{
my ($self, $x) = @_;
my $theta1 = $self->theta1;
my $theta2 = $self->theta2;
my $b1 = $self->b1;
my $b2 = $self->b2;

my $z2 = $theta1->transpose x $x;
$z2->transpose->inplace->plus($b1,0);
my $a2 = $self->_act->($z2);

my $z3 = $theta2->transpose x $a2;
$z3->transpose->inplace->plus($b2,0);
my $a3 = $self->_act->($z3);

return $a3;;
}
#a dimensional transform from $inputs to 1
sub classify{
my ($self, $x) = @_;
my $a3 = $self->run($x);
my $maxes = 4;
die $a3->slice("3:4");
die $a3->dims;
}

sub train_batch{
Expand All @@ -134,6 +152,7 @@ sub spew_cost{

my $z3 = $a2 x $theta2;
$z3 += $b2;
$z3 = $z3->transpose;
my $a3 = $self->_act->($z3);
#warn $a2->slice(":,3");

Expand All @@ -144,8 +163,8 @@ sub spew_cost{
#modify the b's,thetas
sub train{
my ($self, %args) = @_;
my $x = $args{x};
my $y = $args{y};
my $x = $args{x}; #dims: (cases,inputs)
my $y = $args{y}; #(cases,outputs)

my $passes = 1;
my $alpha = .2;
Expand All @@ -167,13 +186,15 @@ sub train{

my $z3 = $a2 x $theta2;
$z3 += $b2;
$z3 = $z3->transpose;
my $a3 = $self->_act->($z3);

#die $a3->dims; #(cats,n)
#so far so good.
#die $z3->slice("0:5,0:5");

my $d3 = -($y-$a3)*$self->_act_deriv->($z3);
$d3 = $d3->transpose;
# warn $self->_act_deriv->($z3)->slice("0:5,0:3");;
# warn $x->slice("10:18,10:18");;
my $d2 = ($d3 x $theta2->transpose) * $self->_act_deriv->($z2);
Expand Down

0 comments on commit 1e9a3c0

Please sign in to comment.