A small implementation of an Artificial Neural Network using Hopfield neurons, synapses and a simple training system :
unit module ann; use ann::HopfieldSynaps; class HopfieldNeuron is export { has @.inputsynapses; has @.outputsynapses; has $.input; method BUILD($y1 = 1000000.rand) { $.input = $y1; } method fire() { ### with training update weights loop (my $i = 0; $i < @.inputsynapses.length; $i++) { if (@.inputsynapses[$i].weight * @.inputsynaps +es[$i].outputneuron.input >= 0) { @.inputsynapses[$i].outputneuron.input + = 1; } else { @.inputsynapses[$i].outputneuron.input + = 0; } } } }
unit module ann; use ann::HopfieldNeuron; class HopfieldSynaps is export { has $.weight; has $.inputneuron; has $.outputneuron; method BUILD($inputneuron, $outputneuron, $y1 = 1000000.rand) +{ $.weight = $y1; } };
unit module ann; use ann::HopfieldNeuron; use ann::HopfieldSynaps; class HopfieldNN is export { has @.neurons; method BUILD($size) { @.neurons = (); loop (my $n = 0; $n < $size; $n++) { push (@.neurons, HopfieldNeuron.new()); } loop (my $m = 0; $m < $size; $m++) { loop (my $j = 0; $j < $size; $j++) { push(@.neurons[$j].inputsynapses, Hopf +ieldSynaps.new()); @.neurons[$j].inputsynapses[$j].output +neuron = @.neurons[$m]; } } loop (my $i = 0; $i < $size; $i++) { loop (my $j = 0; $j < $size; $j++) { push(@.neurons[$j].outputsynapses, Hop +fieldSynaps.new()); @.neurons[$j].outputsynapses[$j].outpu +tneuron = @.neurons[$i]; } } } ### repeat this to train the network method start(@inputs) { ### the inputs length is less than the full neuron lis +t ### the first neurons made in the constructor are the +inputs ### of the network loop (my $i = 0; $i < @inputs.length; $i++) { @.neurons[$i].input = @inputs[$i]; } loop (my $j = 0; $j < @.neurons.length; $j++) { @.neurons[$j].fire(); } }
method start2(@inputs) { ### without any traning, first neurons are for the inp +ut pattern loop (my $n = 0; $n < @inputs.length; $n++) { @.neurons[$n].input = @inputs[$n]; } loop (my $i = 0; $i < @.neurons.length; $i++) { loop (my $j = 0; $j < @.neurons.length; $j++) +{ loop (my $k = 0; $k < @.neurons.length +; $k++) { if ($k == $j) { next; }; @.neurons[$i].inputsynapses[$j].weight + += (2 * @.neurons[$i].inputsynapses[$j].outputneuron.input - 1) * (2 + * @.neurons[$i].inputsynapes[$k].outputneuron.input -1); } } } } };

In reply to Hopfield Neural Network in perl6 by holyghost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.