Matrix and Neural Network in Perl6

Doing some really simple research on Neural Network, i came across that blog post: . Reading that blog post is a good simple introduction to Neural Network, with Concrete example, implemented in Python.

After that, i thought, why not try that with Perl6, however, things were not that simple. Python has some pretty nice libraries for matrix calculation, like numpy, Perl6 on the other hand, does not yet have so many libraries.

Implementing a simple neural network will not be as simple as in Python. With the Neural Network sample from the first part of the previously quoted post, i started to work on a simple library to do some Matrices calculation, Math::Matrix.

For now, the Library only contains simple operations, like addition, subtraction, transposition, negative, determinant. The next step will be to start to work on LU decomposition, and further on.

First thing first, the Neural Network from the previous post would translate to:

use Math::Matrix;

my $training-input =[[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]);
my $training-output =[[0], [0], [1], [1]]);
my $synaptic_weights = [[2.rand() -1] xx 3] );

sub sigmoid( $x ) {
    return 1 / (1 + (-$x).exp);

sub derivative_sigmoid( $x ) {
    return $x * ( 1 - $x );

sub train( $training-input, $training-output ) {
    my $output = think( $training-input );
    my $error = $training-output - $output;
    my $delta = $output.apply( &derivative_sigmoid );
    my $adjust = $training-input.T() dot ($delta*$error);
    $synaptic_weights += $adjust;

sub think( $input ) {
    my $inter = ($input dot $synaptic_weights);
    return $inter.apply( &sigmoid );

train( $training-input,  $training-output ) for ^100;

my $result = think( [ [1,0,0], ] ) );

say "Result : "~$result[0][0];

For the Neural Network part, as i am not an expert at all, and jsut shamelessly translated one from Python, i would not give any advices and instead just redirect you to the original source , Concerning the module Math::Matrix itself, Perl6 has shaped array that could be used for matrices:

my int @array[3,3] = (1,2,3),(4,5,6),(7,8,9);

However, i stumbled on some not yet implemented part of perl6, for instance, with the previously declared shaped array, trying to display the second row:

say @array[1];

give the following error message:

Partially dimensioned views of arrays not yet implemented. Sorry.
  in block <unit> at <unknown file> line 1

For now the implementation is a bit “raw”, however, the simple Neural Network use has an example is working:

» perl6 neural-network.pl6
Result : 0.989911426264572

Have fun with Perl6, and Happy new year!

Leave a comment

About Pierre VIGIER

user-pic I blog about Perl.