Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Hopfield Neural Network in perl6

by liz (Monsignor)
on Jun 30, 2018 at 07:45 UTC ( #1217647=note: print w/replies, xml ) Need Help??

in reply to Hopfield Neural Network in perl6

This looks pretty cool!

If you allow me to make some remarks: it looks like a very C-ish/Perl 5-ish piece of Perl 6 code.

And I think there is one error in it:

method BUILD($inputneuron, $outputneuron, $y1 = 1000000.rand) +{ $.weight = $y1; }

The $.weight is equivalent to self.weight. You can only use the accessor as a mutator if the attribute is marked is rw. So either you can change the assignment to $!weight = $y1 (directly accessing the attribute), or you can add is rw to the attribute declaration: has $.weight is rw. The choice is really whether you want the weight to be assignable from the outside or not and/or you want subclasses to be able to define their own "weight" method or not.

With regards to C-ism / Perl 5-isms: it feels to me that the following loop

loop (my $i = 0; $i < @.inputsynapses.length; $i++) { if (@.inputsynapses[$i].weight * @.inputsynapses[$i].outputneuron. +input >= 0) { @.inputsynapses[$i].outputneuron.input = 1; } else { @.inputsynapses[$i].outputneuron.input = 0; } }

could be simplified to:

.outputneuron.input = +(.weight * .outputneuron.input >= 0) for @.inputsynapses;

This will iterate over all of the inputsynapses without needing to index. The +( ) is what transforms a Boolean True / False to 1 or 0.

Hope this helps

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1217647]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-10-27 13:32 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (256 votes). Check out past polls.