| 1 | package AI::NeuralNet::Kohonen::Node; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use vars qw/$VERSION $TRACE/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | $VERSION = 0.12; # 05 May 2006; no warnings 'numeric' inserted | |||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | $TRACE = 1; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | AI::NeuralNet::Kohonen::Node - a node for AI::NeuralNet::Kohonen | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | Implimentation of a node in a SOM - see | ||||||||||||||||||||||||||||||||||||||||||
| 14 | L<AI::NeuralNet::Kohonen>. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Carp qw/cluck carp confess croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 CONSTRUCTOR (new) | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | Returns a new C<Node> object. If no wieghts | ||||||||||||||||||||||||||||||||||||||||||
| 25 | are supplied, the node's weights are randomized | ||||||||||||||||||||||||||||||||||||||||||
| 26 | with real nubmers. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =item dim | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | The number of dimensions of this node's weights. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | Do not supply if you are supplying C<weight>. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =item weight | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | Optional: a reference to an array containing the | ||||||||||||||||||||||||||||||||||||||||||
| 38 | weight for this node. Supplying this allows the | ||||||||||||||||||||||||||||||||||||||||||
| 39 | constructor to work out C<dim>, above. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | =item values | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | The values of the vector. Use C<x> for unknown values. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =item missing_mask | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | Used to donate missing input in the node. Default is C<x>. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 798 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 798 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 798 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 798 | my $self = bless \%args,$class; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 798 | ▶ | $self->{missing_mask} = 'x' unless defined $self->{missing_mask}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 798 | ▶ | if (not defined $self->{weight}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 797 | ▶ | if (not defined $self->{dim}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | cluck "No {dim} or {weight}!"; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 796 | $self->{weight} = []; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 796 | for my $w (0..$self->{dim}){ | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2388 | $self->{weight}->[$w] = rand; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ▶ | } elsif (not ref $self->{weight} or ref $self->{weight} ne 'ARRAY') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
not ref $self->{"weight"} or ref $self->{"weight"} ne "ARRAY"
MC/DC0%0/2
not ref $self->{"weight"} or ref $self->{"weight"} ne "ARRAY"
not ref $self->{"weight"}
ref $self->{"weight"} ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $self->{"weight"}B= ref $self->{"weight"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | cluck "{weight} should be an array reference!"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | $self->{dim} = $#{$self->{weight}}; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 797 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 METHOD distance_from | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Find the distance of this node from the target. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | Accepts: the target vector as an array reference. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | Returns: the distance. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | __________________ | ||||||||||||||||||||||||||||||||||||||||||
| 86 | / i=n 2 | ||||||||||||||||||||||||||||||||||||||||||
| 87 | Distance = / E ( V - W ) | ||||||||||||||||||||||||||||||||||||||||||
| 88 | \/ i=0 i i | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | Where C<V> is the current input vector, and | ||||||||||||||||||||||||||||||||||||||||||
| 91 | C<W> is this node's weight vector. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 2311 | ▶ | sub distance_from { my ($self,$target) = (shift,shift); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distance_from: called CC=7 SCAR=22.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 2311 | ▶ | if (not defined $target or not ref $target or ref $target ne 'AI::NeuralNet::Kohonen::Input'){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not defined $target && ref $target or ref $target ne "AI::NeuralNet::Kohonen::Input"
defined $target && ref $target
MC/DC0%0/3
not defined $target && ref $target or ref $target ne "AI::NeuralNet::Kohonen::Input"
defined $target
ref $target
ref $target ne "AI::NeuralNet::Kohonen::Input"
Truth table25%1/4
not(A && B) or C
A= defined $targetB= ref $targetC= ref $target ne "AI::NeuralNet::Kohonen::Input"
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | cluck "distance_from requires a target ::Input object!"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 2311 | ▶ | if ($#{$target->{values}} != $self->{dim}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | croak "distance_from requires the target's {value} field dim match its own {dim}!\n" | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ."(".($#{$target->{values}})." v {".$self->{dim}."} ) "; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 2311 | my $distance = 0; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 2311 | ▶ | for (my $i=0; $i<=$self->{dim}; ++$i){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 6933 | ▶ | next if $target->{values}->[$i] eq $self->{missing_mask}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | $distance += ( | ||||||||||||||||||||||||||||||||||||||||||
| 109 | ( $target->{values}->[$i] - $self->{weight}->[$i] ) | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 6933 | * ( $target->{values}->[$i] - $self->{weight}->[$i] ) | |||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 2311 | return sqrt($distance); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 METHOD distance_effect | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | Calculates the effect on learning of distance from a given point | ||||||||||||||||||||||||||||||||||||||||||
| 120 | (intended to be the BMU). | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | Accepts: | ||||||||||||||||||||||||||||||||||||||||||
| 123 | the distance of this node from the given point; | ||||||||||||||||||||||||||||||||||||||||||
| 124 | the radius of the neighbourhood of affect around the given point. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | Returns: | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | ( 2 ) | ||||||||||||||||||||||||||||||||||||||||||
| 129 | ( distance ) | ||||||||||||||||||||||||||||||||||||||||||
| 130 | THETA(t) = exp ( - ----------- ) | ||||||||||||||||||||||||||||||||||||||||||
| 131 | ( 2 ) | ||||||||||||||||||||||||||||||||||||||||||
| 132 | ( 2 sigma (t) ) | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | Where C<distance> is the distance of the node from the BMU, | ||||||||||||||||||||||||||||||||||||||||||
| 135 | and C<sigma> is the width of the neighbourhood as calculated | ||||||||||||||||||||||||||||||||||||||||||
| 136 | elsewhere (see L<AI::NeuralNet::Kohonen/FINDING THE NEIGHBOURS OF THE BMU>). THETA also | ||||||||||||||||||||||||||||||||||||||||||
| 137 | decays over time. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | The time C<t> is always that of the calling object, and is not referenced here. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 54 | ▶ | sub distance_effect { my ($self,$distance,$sigma) = (shift,shift,shift); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distance_effect: called CC=3 SCAR=12.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 54 | ▶ | confess "Wrong args" unless defined $distance and defined $sigma; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $distance and defined $sigma
MC/DC0%0/2
defined $distance and defined $sigma
defined $distance
defined $sigma
Truth table33%1/3
A and B
A= defined $distanceB= defined $sigma
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 54 | return exp (-($distance*$distance) / 2 * ($sigma*$sigma)) | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||||||||||||||||