The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AI::NeuralNet::SOM - Perl extension for Kohonen Maps

SYNOPSIS

  use AI::NeuralNet::SOM;
  my $nn = new AI::NeuralNet::SOM (output_dim => "5x6",
                                     input_dim  => 3);
  $nn->initialize;
  $nn->train (30, 
    [ 3, 2, 4 ], 
    [ -1, -1, -1 ],
    [ 0, 4, -3]);

  print $nn->as_data;

DESCRIPTION

This package is a stripped down implementation of the Kohonen Maps (self organizing maps). It is NOT meant as demonstration or for use together with some visualisation software. And while it is not (yet) optimized for speed, some consideration has been given that it is not overly slow.

Particular emphasis has be given that the package plays nicely with others. So no use of files, no arcane dependencies, etc.

Scenario

The basic idea is that the neural network consists of a 2-dimensional array of N-dimensional vectors. When the training is started these vectors may be complete random, but over time the network learns from the sample data, also N-dimensional vectors.

Slowly, the vectors in the network will try to approximate the sample vectors fed in. If in the sample vectors there were clusters, then these clusters will be neighbourhoods within the rectangle.

Technically, you have reduced your dimension from N to 2.

INTERFACE

Constructor

The constructor takes arguments:

output_dim : (mandatory, no default)

A string of the form "3x4" defining the X and the Y dimensions.

input_dim : (mandatory, no default)

A positive integer specifying the dimension of the sample vectors (and hence that of the vectors in the grid).

learning_rate: (optional, default 0.1)

This is a magic number which influence how strongly the vectors in the grid can be influenced. Higher movement can mean faster learning if the clusters are very pronounced. If not, then the movement is like noise and the convergence is not good. To mediate that effect, the learning rate is reduced over the iterations.

Example:

    my $nn = new AI::NeuralNet::SOM (output_dim => "5x6",
                                     input_dim  => 3);

Methods

initialize

$nn->initialize

You need to initialize all vectors in the map.

By default, the vectors will be initialized with random values, so all point chaotically into different directions. This may not be overly clever as it may slow down the convergence process unnecessarily.

TODO: provide more flexibility to initialize with eigenvectors

train

$nn->train ( $epochs, @samples )

The training uses the sample vectors to make the network learn. Each vector is simply a reference to an array of values.

The epoch parameter controls how often the process is repeated.

Example:

   $nn->train (30, 
               [ 3, 2, 4 ],
               [ -1, -1, -1 ], 
               [ 0, 4, -3]);

TODO: expose distance

radius

$radius = $nn->radius

Returns the initial radius of the map.

map

$m = $nn->map

This method returns the 2-dimensional array of vectors in the grid (as a reference to an array of references to arrays of vectors.

Example:

   my $m = $nn->map;
   for my $x (0 .. 5) {
       for my $y (0 .. 4){
           warn "vector at $x, $y: ". Dumper $m->[$x]->[$y];
       }
   }
as_string

print $nn->as_string

This methods creates a pretty-print version of the current vectors.

as_data

print $nn->as_data

This methods creates a string containing the raw vector data, row by row. This can be fed into gnuplot, for instance.

SEE ALSO

http://www.ai-junkie.com/ann/som/som1.html

AUTHOR

Robert Barta, <rho@devc.at>

COPYRIGHT AND LICENSE

Copyright (C) 2007 by Robert Barta

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.