AI-Perceptron

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME
    AI::Perceptron - example of a node in a neural network.

SYNOPSIS
     use AI::Perceptron;

     my $p = AI::Perceptron->new
               ->num_inputs( 2 )
               ->learning_rate( 0.04 )
               ->threshold( 0.02 )
               ->weights([ 0.1, 0.2 ]);

     my @inputs  = ( 1.3, -0.45 );   # input can be any number
     my $target  = 1;                # output is always -1 or 1
     my $current = $p->compute_output( @inputs );

     print "current output: $current, target: $target\n";

     $p->add_examples( [ $target, @inputs ] );

     $p->max_iterations( 10 )->train or
       warn "couldn't train in 10 iterations!";

     print "training until it gets it right\n";
     $p->max_iterations( -1 )->train; # watch out for infinite loops

DESCRIPTION
    This module is meant to show how a single node of a neural network
    works.

    Training is done by the *Stochastic Approximation of the
    Gradient-Descent* model.

MODEL
    Model of a Perceptron

                  +---------------+
     X[1] o------ |W[1]      T    |
     X[2] o------ |W[2] +---------+         +-------------------+
      .           | .   |   ___   |_________|    __  Squarewave |_______\  Output
      .           | .   |   \     |    S    | __|    Generator  |       /
      .           | .   |   /__   |         +-------------------+
     X[n] o------ |W[n] |   Sum   |
                  +-----+---------+

                 S  =  T + Sum( W[i]*X[i] )  as i goes from 1 -> n
            Output  =  1 if S > 0; else -1

    Where "X[n]" are the perceptron's *inputs*, "W[n]" are the *Weights*
    that get applied to the corresponding input, and "T" is the *Threshold*.

    The *squarewave generator* just turns the result into a positive or
    negative number.

    So in summary, when you feed the perceptron some numeric inputs you get
    either a positive or negative output depending on the input's weights
    and a threshold.

TRAINING
    Usually you have to train a perceptron before it will give you the
    outputs you expect. This is done by giving the perceptron a set of
    examples containing the output you want for some given inputs:

        -1 => -1, -1
        -1 =>  1, -1
        -1 => -1,  1
         1 =>  1,  1

    If you've ever studied boolean logic, you should recognize that as the
    truth table for an "AND" gate (ok so we're using -1 instead of the
    commonly used 0, same thing really).

    You *train* the perceptron by iterating over the examples and adjusting
    the *weights* and *threshold* by some value until the perceptron's
    output matches the expected output of each example:

        while some examples are incorrectly classified
            update weights for each example that fails

    The value each weight is adjusted by is calculated as follows:

        delta[i] = learning_rate * (expected_output - output) * input[i]

    Which is know as a negative feedback loop - it uses the current output



( run in 0.544 second using v1.01-cache-2.11-cpan-f56aa216473 )