Deutsch   English   Français   Italiano  
<e29174276350508db8dfa29eee076e44@www.novabbs.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.nk.ca!rocksolid2!i2pn2.org!.POSTED!not-for-mail
From: melahi_ahmed@yahoo.fr (Ahmed)
Newsgroups: comp.lang.forth
Subject: Re: Neural networks from scratch in forth
Date: Mon, 2 Dec 2024 23:46:49 +0000
Organization: novaBBS
Message-ID: <e29174276350508db8dfa29eee076e44@www.novabbs.com>
References: <06eabe944364625b1eba7ea6e09791ad@www.novabbs.com> <e8095b4c566e1df3c7179a0534d2442b@www.novabbs.com> <3d8c2bb29b9275f81765aa62d9e43450@www.novabbs.com> <c9e86a45822fcdc4e858ed125a39633c@www.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
	logging-data="996409"; mail-complaints-to="usenet@i2pn2.org";
	posting-account="t+/9LUKLIiUqIe6reyFE7me/EcA/Gr17dRXgwnADesE";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$z9ai3D2jfouMgJDd6D7w4e/BmEbvgU8wpYV90ieoC4zCPBXiT/IH.
X-Rslight-Posting-User: 5f6b2e70af503e44dad56966aa15d35bdef29623
X-Spam-Checker-Version: SpamAssassin 4.0.0
Bytes: 7148
Lines: 308

On Mon, 2 Dec 2024 21:10:23 +0000, mhx wrote:

> Interesting ... I last looked at neural nets some 40 years ago (iForth
> full distribution dfwforth/examples/neural). At that time Jack Woehr
> was also busy it with it. There should be Forth files floating around.
>
> Here's my Little Red Riding Hood application, I think it can fit your
> framework.
>
> -marcel
>

Here is the code using the neural_networks.fs program:
This code is saved as lrrh.fs

--------------the code begins here-----------------

include neural_networks.fs

\ data
\ inputs
0 value BigEars
1 value BigEyes
2 value BigTeeth
3 value Kindly
4 value Wrinkled
5 value Handsome
\ Outputs
0 value RunAway
1 value Scream
2 value Look?
3 value Kiss
4 value Approach
5 value OfferFood
6 value Flirt

\ training data
: X 1e f, ;
: _ 0e f, ;

: Grandma    _ X _ X X _ ;
: Wolf       X X X _ _ _ ;
: Woodcutter X _ _ X _ X ;

: Love _ _ _ X X X _ ;
: Hate X X X _ _ _ _ ;
: Sex  _ _ _ _ X X X ;

3 >n_samples
create data1
Grandma Love
Wolf Hate
Woodcutter Sex

data1 >data

\ neuralnet
7 2 6 1 neuralnet: net1
' net1 is net
net_layers
' dlatan is act_func
' dllinear is act_func_ol

\
1000000 >epochs
1e-1 >eta
0e >beta
1 >display_step
1e-4 >tol
0e >rtol
true >init_net
false >adapt_eta

\ learning

: 0_1_outputs
    n_outputs @ 0 do
      outputs i floats + f@
      fround
      outputs i floats + f!
    loop
;

: .fact
     0.5e f> if
       cr
       case
         BigEars   of ." -- Big ears"              endof
         BigEyes   of ." -- Big eyes"              endof
         BigTeeth  of ." -- Big teeth"             endof
         Kindly    of ." -- A kindly appearance"   endof
         Wrinkled  of ." -- A wrinkled complexion" endof
         Handsome  of ." -- A handsome feller"     endof
                      ." -- something illegal?"
       endcase
     else
       drop
     then
;

: .action
     0.5e f> if
       cr
       case
         RunAway   of ." -- run away"                endof
         Scream    of ." -- scream"                  endof
         Look?     of ." -- look for the woodcutter" endof
         Kiss      of ." -- kiss on the cheek"       endof
         Approach  of ." -- approach"                endof
         OfferFood of ." -- offer food"              endof
         Flirt     of ." -- flirt"                   endof
                      ." -- it is something illegal?"
       endcase
     else
       drop
     then
;

: doLrrh-sensation
    cr ." The little girl digests the following facts :" cr
    n_inputs @ 0 do
      i inputs i floats + f@ .fact
    loop
    cr cr ." That is why she decides to: " cr
    n_outputs @ 0 do
      i outputs i floats + f@ .action
    loop
    cr
;

defer show-net
' doLrrh-sensation is show-net

defer do-it!
: doLrrh timer-reset
   learn
   cr .elapsed
;

' doLrrh is do-it!

\
: X 1e ;
: _ 0e ;

: person1   _ X _ X X _ ;
: person2    X X X _ _ _ ;
: person3   X _ _ X _ X ;

: Grandma_    _ X _ X X _ ;
: Wolf_       X X X _ _ _ ;
: Woodcutter_ X _ _ X _ X ;

: seen to_inputs forward_pass 0_1_outputs ;

\ do-it!
\ person seen show-net

-------the code ends here--------------

Here is a session for this program (with gfgorth):

include lrrh.fs

do-it!
Learning...
-----------
epochs| Cost
------+ ----
0    4.4188457512276
1    3.53222220254148
2    3.06114255073205
3    2.80622819639435
4    2.66162924195142
5    2.56800089024257
6    2.49016050909303
7    2.40496982172919
8    2.29464055287916
9    2.1441048550535
========== REMAINDER OF ARTICLE TRUNCATED ==========