AI-NeuralNet-Kohonen-Demo-RGB

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
AI/NeuralNet/Kohonen/Demo/RGB version 0.01
==========================================
 
        $_ = AI::NeuralNet::Kohonen::Demo::RGB->new(
                        map_dim => 39,
                        epochs  => 9,
                        table   => "R G B"
                                  ."1 0 0"
                                  ."0 1 0"
                                  ."0 0 1",
        );
        $_->train;
        exit;
 
A sub-class of "AI::NeuralNet::Kohonen" that Impliments extra methods

RGB.pm  view on Meta::CPAN

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
=head1 NAME
 
AI::NeuralNet::Kohonen::Demo::RGB - Colour-based demo
 
=head1 SYNOPSIS
 
        use AI::NeuralNet::Kohonen::Demo::RGB;
        $_ = AI::NeuralNet::Kohonen::Demo::RGB->new(
                display_scale => 20,
                display => 'hex',
                map_dim => 39,
                epochs  => 9,
                table   => "R G B"
                      ."1 0 0"
                      ."0 1 0"
                      ."0 0 1",
        );
        $_->train;
        exit;

RGB.pm  view on Meta::CPAN

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use Tk;
use Tk qw/DoOneEvent DONT_WAIT/;
 
#
# Used only by &tk_train
#
sub tk_show { my $self=shift;
        for my $x (0..$self->{map_dim_x}){
                for my $y (0..$self->{map_dim_y}){
                        my $colour = sprintf("#%02x%02x%02x",
                                (int (255 * $self->{map}->[$x]->[$y]->{weight}->[0])),
                                (int (255 * $self->{map}->[$x]->[$y]->{weight}->[1])),
                                (int (255 * $self->{map}->[$x]->[$y]->{weight}->[2])),
                        );
                        if ($self->{display} and $self->{display} eq 'hex'){
                                my $xo = ($y % 2) * ($self->{display_scale}/2);
                                my $yo = 0;
 
                                $self->{c}->create(
                                        polygon => [
                                                $xo + ((1+$x)*$self->{display_scale} ),
                                                $yo + ((1+$y)*$self->{display_scale} ),

RGB.pm  view on Meta::CPAN

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                                );
                        }
                }
        }
        return 1;
}
 
 
=head1 METHOD train
 
Over-rides the base class to provide TK displays of the map
 
=cut
 
sub train { my ($self,$epochs) = (shift,shift);
        my $label_txt;
 
        $epochs = $self->{epochs} unless defined $epochs;
        $self->{display_scale} = 10 if not defined   $self->{display_scale};
 
        $self->{mw} = MainWindow->new(
                -width  => 200+($self->{map_dim_x} * $self->{display_scale}),
                -height => 200+($self->{map_dim_y} * $self->{display_scale}),
        );
    my $quit_flag = 0;
    my $quit_code = sub {$quit_flag = 1};
    $self->{mw}->protocol('WM_DELETE_WINDOW' => $quit_code);
 
        $self->{c} = $self->{mw}->Canvas(
                -width  => 50+($self->{map_dim_x} * $self->{display_scale}),
                -height => 50+($self->{map_dim_y} * $self->{display_scale}),
                -relief => 'ridge',
                -border => 5,
        );
        $self->{c}->pack(-side=>'top');
 
        my $l = $self->{mw}->Label(-text => ' ',-textvariable=>\$label_txt);
        $l->pack(-side=>'left');
 
        # Replaces Tk's MainLoop
    for (0..$self->{epochs}) {

test.pl  view on Meta::CPAN

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
                [1,2,3]
        ],
);
ok( ref $_->{input}, 'ARRAY');
ok( $_->{input}->[0]->[0],1);
ok( $_->{input}->[0]->[1],2);
ok( $_->{input}->[0]->[2],3);
 
 
$_ = AI::NeuralNet::Kohonen::Demo::RGB->new(
        map_dim => 39,
        epochs => 3,
        table=>
"R G B
1 0 0
0 1 0
0 0 1
",
);
ok( ref $_->{input}, 'ARRAY');
ok( $_->{input}->[0]->[0],1);
ok( $_->{input}->[0]->[1],0);
ok( $_->{input}->[0]->[2],0);
ok( $_->{weight_dim}, 2);
 
$_->train;
 
$_ = AI::NeuralNet::Kohonen::Demo::RGB->new(
        display_scale => 10,
        display => 'hex',
        map_dim => 39,
        epochs => 9,
        table=>
"R G B
1 0 0
0 1 0
0 0 1
",
);
ok( ref $_->{input}, 'ARRAY');
ok( $_->{input}->[0]->[0],1);



( run in 1.126 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )