Liberty BASIC Community Forum
« Neural Net ? »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 6:09pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Neural Net ?  (Read 143 times)
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 936
xx Neural Net ?
« Thread started on: Dec 10th, 2017, 04:18am »

this is a try at a NN in LB

error :
syntacs error ?
Code:
        u = a * errorc * linput(i,k)
 

Code:
'' bluatigro 2 dec 2017
'' ann lib

'' don't change this
global layers
global size
global inmax
global uitmax
'' you can change this
layers = 4
size = 4
inmax = 3
uitmax = 1
'' dont change this
  dim invoer( inmax )
  dim wish( uitmax )
  dim uit( layers , size )
  dim g( layers , size )
  dim wg( layers , size )
  dim dv( in( layers , size , size ) )
  dim w( in( layers , size , size ) )
  for l = 0 to layers
    linput(l,0) = 1
    for c = 0 to size
      for i = 0 to sise
        dv(in(l,c,i)) = rnd(0)
        w(in(l,c,i)) = rnd(0)
      next i
    next c
  next l
  dim linput( layers , size )

'' you can change this
for epog = 0 to 1000
  fout = 0
  invoer( 0 ) = 1
  for a = 0 to 1
    invoer( 1 ) = a
    for b = 0 to 1
      invoer( 2 ) = b
      for c = 0 to 1
        invoer( 3 ) = c
        wish( 1 ) = a xor b xor c
        fout = fout + train( .1 , .1 )
      next c
    next b
  next a
  print "epog : " ; epog , "fout : " ; fout
next epog
print "input" , "output" , "wish"
  invoer( 0 ) = 1
  for a = 0 to 1
    invoer( 1 ) = a
    for b = 0 to 1
      invoer( 2 ) = b
      for c = 0 to 1
        invoer( 3 ) = c
        call propegate
        print a;b;c,uit(layers,1),a xor b xor c
      next c
    next b
  next a

print "[ end sim ]"
end

'' only change if you are sure

function in( layer , cel , i )
  in = layer * size * size + cel * size + i
end function

sub calculate layer
  for i = 0 to size
    adder = 0.0
    for j = 0 to size
      adder = adder _
              + w( in( layer , i , j ) ) _
              * linput( layer , j )
      adder = adder + wg( layer , i ) _
              * g( layer , i )
    next j
    uit( layer , i ) = 1.0 _
    / ( 1.0 + exp( (0-adder) / size ) )
  next i
end sub

sub propegate
  if inmax > size then
    print "ERROR : inmax > size !!!"
    end
  end if
  for i = 0 to inmax
    uit( 0 , i ) = invoer( i )
  next i
  call calculate 0
  for i = 1 to layers
    for j = 0 to size
      u = uit( i - 1 , j )
      linput( i , j ) = u
    next j
    call calculate i
  next i
end sub

function train( a , m )
  if uitmax > size then
    print "ERROR : uitmax > size !!!"
    end
  end if
  call propegate
  for i = 0 to uitmax
    uitput = uit( layers , i )
    errorg = errorg + ( wish( i ) - uitput ) ^ 2
  next i
  for i = layers to 0 step -1
    for j = 0 to size
      uitput = uit( i , j )
      errorc = uitput * ( 1 - uitput ) * adder
      for k = 0 to size
        delta = dv(in(i,j,k))
        u = a * errorc * linput(i,k)
        udelta = u + delta * m
        w(in(i,j,k)) = w(in(i,j,k)) + udelta
        dv(in(i,j,k)) = udelta
        csum = csum + w(in(i,j,k)) * errorc
      next k
      wg(i,j) = wg(i,j) + a * errorc * g(i,j)
      adder = csum
      csum = 0
    next j
  next i
  train = errorg / 2
end function

 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1732
xx Re: Neural Net ?
« Reply #1 on: Dec 10th, 2017, 05:33am »

just move
Code:
dim linput( layers , size ) 

ahead of loop that uses it.
User IP Logged

damned Dog in the Manger
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 936
xx Re: Neural Net ?
« Reply #2 on: Dec 11th, 2017, 03:47am »

@ tsh73 :
thanks for help

the last code shoot give a out of array error
on that spot

update :
replaced line as told

error :
same error

Code:
'' bluatigro 2 dec 2017
'' ann lib

'' don't change this
global layers
global size
global inmax
global uitmax
'' you can change this
layers = 4
size = 4
inmax = 3
uitmax = 1
'' dont change this
  dim invoer( inmax )
  dim wish( uitmax )
  dim uit( layers , size )
  dim g( layers , size )
  dim wg( layers , size )
  dim dv( in( layers , size , size ) )
  dim w( in( layers , size , size ) )
  dim lin( layers , size )

  for l = 0 to layers
    lin(l,0) = 1
    for c = 0 to size
      for i = 0 to sise
        dv(in(l,c,i)) = rnd(0)
        w(in(l,c,i)) = rnd(0)
      next i
    next c
  next l

'' you can change this
for epog = 0 to 1000
  fout = 0
  invoer( 0 ) = 1
  for a = 0 to 1
    invoer( 1 ) = a
    for b = 0 to 1
      invoer( 2 ) = b
      for c = 0 to 1
        invoer( 3 ) = c
        wish( 1 ) = a xor b xor c
        fout = fout + train( .1 , .1 )
      next c
    next b
  next a
  print "epog : " ; epog , "fout : " ; fout
next epog
print "input" , "output" , "wish"
  invoer( 0 ) = 1
  for a = 0 to 1
    invoer( 1 ) = a
    for b = 0 to 1
      invoer( 2 ) = b
      for c = 0 to 1
        invoer( 3 ) = c
        call propegate
        print a;b;c,uit(layers,1),a xor b xor c
      next c
    next b
  next a

print "[ end sim ]"
end

'' only change if you are sure

function in( layer , cel , i )
  in = layer * size * size + cel * size + i
end function

sub calculate layer
  for i = 0 to size
    adder = 0.0
    for j = 0 to size
      adder = adder _
              + w( in( layer , i , j ) ) _
              * lin( layer , j )
      adder = adder _
              + wg( layer , i ) _
              * g( layer , i )
    next j
    uit( layer , i ) = 1.0 _
    / ( 1.0 + exp( (0-adder) / size ) )
  next i
end sub

sub propegate
  if inmax > size then
    print "ERROR : inmax > size !!!"
    end
  end if
  for i = 0 to inmax
    uit( 0 , i ) = invoer( i )
  next i
  call calculate 0
  for i = 1 to layers
    for j = 0 to size
      u = uit( i - 1 , j )
      lin( i , j ) = u
    next j
    call calculate i
  next i
end sub

function train( a , m )
  if uitmax > size then
    print "ERROR : uitmax > size !!!"
    end
  end if
  call propegate
  for i = 0 to uitmax
    uitput = uit( layers , i )
    errorg = errorg + ( wish( i ) - uitput ) ^ 2
  next i
  for i = layers to 0 step -1
    for j = 0 to size
      uitput = uit( i , j )
      errorc = uitput * ( 1 - uitput ) * adder
      for k = 0 to size
        delta = dv(in(i,j,k))
        u = a * errorc * lin(i,k)
        udelta = u + delta * m
        w(in(i,j,k)) = w(in(i,j,k)) + udelta
        dv(in(i,j,k)) = udelta
        csum = csum + w(in(i,j,k)) * errorc
      next k
      wg(i,j) = wg(i,j) + a * errorc * g(i,j)
      adder = csum
      csum = 0
    next j
  next i
  train = errorg / 2
end function
 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1732
xx Re: Neural Net ?
« Reply #3 on: Dec 11th, 2017, 07:48am »

I dunno what is "same error"
but I get "float underflow" from last code.
(after this output
Code:
epog : 0      fout : 2.10298822
epog : 1      fout : 2.10787305
epog : 2      fout : 2.10787305
epog : 3      fout : 2.10787305
epog : 4      fout : 2.10787305
epog : 5      fout : 2.10787305
epog : 6      fout : 2.10787305
epog : 7      fout : 2.10787305
epog : 8      fout : 2.10787305
epog : 9      fout : 2.10787305
epog : 10     fout : 2.10787305
epog : 11     fout : 2.10787305 

)
Then run in light debug,
on error debug highlights this line
Code:
udelta = u + delta * m 


Now I really have no idea what it supposed to do.
May be there is some "cut" needed so it didn't get "float underflow"?
(like, Code:
if abs(something) < 1e-10 then 'time to stop 
)
« Last Edit: Dec 11th, 2017, 07:49am by tsh73 » User IP Logged

damned Dog in the Manger
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls