Neural network

Neural network

Postby MOISES » Sat Aug 21, 2021 10:09 am

This is a very interesting sample created by Dan:

Code: Select all  Expand view
// neural network
procedure main
  ogg:={{"dog",0},{"mouse",0},{"snake",0},{"sparrow",0}}
  azi:={{"has fur",0},{"has 4 legs",0},{"gnaws",0},{"slithers",0},{"has wings",0}}
  dom:={0,0,0,0,0}
  rules:={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}
  nquestions=5
  nobjects=4
  clear
  if file ("rules.txt")
   nHandle=fopen("rules.txt",0)
   mr:=freadstr(nHandle,1000)
   fclose(nHandle)
   ? mr
   z=1
   for j=1 to nobjects
   for k=1 to nquestions
     rules[j,k]=val(token(mr,",",z))
     ++z
   next j
   next k
  endif
  do while .t.
     answer=" "
     score=0
     for j=1 to nobjects
        ogg[j,2]=0
     next j

     ? "think of an animal among dog, mouse, snake and sparrow and press enter (x ends)"
     accept to answer
     if answer="x"
        exit
     endif
     for j=1 to nquestions
        ? azi[j,1] +"? "
        accept to answer
        dom[j]=if(upper(answer)="Y",1,0)
     next j
     // adesso calcola la variabile di decisione
     for j=1 to nobjects
        for k=1 to nquestions
           ogg[j,2]=ogg[j,2]+dom[k]*rules[j,k]
        next k
     next j
     // guarda chi ha raggiunto il punteggio maggiore
     maxscore=0
     nogg=0
     for j=1 to nobjects
         if ogg[j,2]>=maxscore
            maxscore=ogg[j,2]
            nogg=j
         endif
     next j
     ? "it is a " + ogg[nogg,1] + " ?"
     accept to answer
     if answer="N" .or. answer="n"
        ? "What was it?"
        for j=1 to nobjects
           ? str(j) + " " + ogg[j,1]
        next j
        ? "(animal number):"
        accept to answer
        nogg=val(answer)
        //newpunt=0
        for j=1 to nobjects
           ? dom[j]
           // aggiorna matrice per oggetti diversi da quello giusto
           if ogg[j,2]>=maxscore .and. j!=nogg
              for k=1 to nquestions
                  rules[j,k]=rules[j,k]-dom[k]
              next k
           endif
           if j=nogg
             for k=1 to nquestions
                rules[nogg,k]=rules[nogg,k]+dom[k]
             next k
           endif
        next j
     endif
     ou=space(12)
     for k=1 to nquestions
       ou+=padr(azi[k,1],12)
     next
     ? ou
     for j=1 to nobjects
         ou=padr(ogg[j,1],12) //+" "
         for k=1 to nquestions
            ou=ou+ str(rules[j,k])+" "
         next k
         ? ou
     next j
  enddo
  clear
  ? "save matrix ?"
  accept to answer
  if upper(answer)="S"
     fcreate("rules.txt")
     nHandle:=fcreate("rules.txt")
     separ=","
     ndati=0
     for j=1 to nobjects
     for k=1 to nquestions
       cdato=str(rules[j,k])
       fwrite (nhandle,cdato)
       ++ndati
       if ndati<nobjects*nquestions
        fwrite (nhandle,separ)
       endif
     next k
     next j
     fclose(nHandle)
  endif
RETURN


Best regards,
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
MOISES
 
Posts: 838
Joined: Wed Aug 22, 2007 10:09 am

Re: Neural network

Postby Otto » Sun Aug 22, 2021 7:22 am

Good morning,
Thank you for the example. Would you be so kind as to give us some introductory explanations?
Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6313
Joined: Fri Oct 07, 2005 7:07 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 85 guests