LB Booster
General >> General Board >> perseptron
http://lbb.conforums.com/index.cgi?board=general&action=display&num=1454505613

perseptron
Post by bluatigro on Feb 3rd, 2016, 12:20pm

this is a try at a lerning perseptron
perseptron :
- a neural net of 1 neuron

i m using a genetic algoritm to get it to lern

later it wil be a neural net

i know that f() is trivial
but i have to start somewere
Code:
global maxin , hoog
maxin = 1
hoog = 2 ^ 24
dim cel( 200 , maxin ) , w( 200 , maxin + 1 )
dim fout( 200 ) , ry( 200 )
''set weights at random
for i = 0 to 200
  ry( i ) = i
  for j = 0 to maxin + 1
    w( i , j ) = int( rnd(0) * hoog )
  next j
next i
for gen = 0 to 100
  ''let all cels live
  for i = 0 to 200
    fout( i ) = 0
    for x = -10 to 10
      for y = -10 to 10
        cel( i , 0 ) = x
        cel( i , 1 ) = y
        fout( i ) = fout( i ) _
        + abs( sign( cel.uit( i ) ) - f( x , y ) )
      next y
    next x
  next i
  ''sort cels on fitnes
  for h = 1 to 200
    for l = 0 to h - 1
      if fout(ry(l)) > fout(ry(h)) then
        i = ry(l)
        ry(l)=ry(h)
        ry(h)= i
      end if
    next l
  next h
  print gen , fout( ry( 0 ) )
  ''let best cels make next generation
  for i = 20 to 200
    a = int( rnd(0) * 20 )
    b = int( rnd(0) * 20 )
    call cel.mix ry( i ) , ry( a ) , ry( b )
    if rnd(0) < 0.5 then
      call cel.mutate ry( i )
    end if
  next i
next gen
q = 0
''test the best cel
for i = 0 to 99
  x = int( rnd(0) * 20 ) - 10
  y = int( rnd(0) * 20 ) - 10
  cel( ry( 0 ) ) = x
  cel( ry( 0 ) ) = y
  if cel.uit( ry( 0 ) ) <> f( x , y ) then
    q = q + 1
  end if
  print cel.uit( ry( 0 ) ) , f( x , y ) _
  , "error = " ; q ; " %"
next i
end

function sign( x )
  uit = 0
  if x < 0 then uit = -1
  if x > 0 then uit = 1
  sign = uit
end function
function f( x , y )
  uit = -1
  if x < y then uit = 1
  f = uit
end function
function cel.uit( no )
  sum = w( no , maxin + 1 ) / hoog - .5
  for i = 0 to maxin
    sum = sum + cel(no,i)*w(no,i)/hoog - .5
  next i
  cel.uit = sum
end function
sub cel.mutate no
  dw = int( rnd(0) * ( maxin + 1 ) )
  m = int( rnd(0) * 24 )
  w( no , dw ) = w( no , dw ) xor 2 ^ m
end sub
sub cel.mix uit , a , b
  for i = 0 to maxin
    w( uit , i ) = 0
    for m = 0 to 24
      if rnd(0) < .5 then
        w(uit,i)=w(uit,i)+w(a,i)and 2^m
      else
        w(uit,i)=w(uit,i)+w(b,i)and 2^m
      end if
    next m
  next i
end sub
 

Re: perseptron
Post by michael on Feb 15th, 2016, 08:18am

You need to add SCAN in your program.. Liberty freezes and I am unsure of the function of this program

Re: perseptron
Post by bluatigro on Feb 18th, 2016, 08:36am

update :
- boolean code added
- some errors removed

error :
- fout( ry( 0 ) ) isnt getting smaller

@ michael :
- i m trying to lern how to make a Neural Net
- first i have to praktice whit a perseptron
- the perseptron is lerning f() whit a Genetic Algoritm
- i dont know [ jet ] how to backprop the NN

GA :
- 1 : create random dna
- 2 : let dna make output
- 3 : sort dna on fitness
- 4 : best dna gets child
- 5 : mutate some childs
- 6 : goto 2 if error > wished and generation < max

Code:
global maxin , hoog
maxin = 1
hoog = 2 ^ 24
dim cel( 200 , maxin ) , w( 200 , maxin + 1 )
dim fout( 200 ) , ry( 200 )
''set weights at random
for i = 0 to 200
  ry( i ) = i
  for j = 0 to maxin + 1
    w( i , j ) = int( rnd(0) * hoog )
  next j
next i
for gen = 0 to 10
  scan
  ''let all cels live
  for i = 0 to 200
    fout( i ) = 0
    for x = -10 to 10
      for y = -10 to 10
        cel( i , 0 ) = x
        cel( i , 1 ) = y
        fout( i ) = fout( i ) _
        + abs( sign( cel.uit( i ) ) - f( x , y ) )
      next y
    next x
  next i
  ''sort cels on fitnes
  for h = 1 to 200
    for l = 0 to h - 1
      if fout(ry(l)) > fout(ry(h)) then
        i = ry(l)
        ry(l)=ry(h)
        ry(h)= i
      end if
    next l
  next h
  print gen , fout( ry( 0 ) )
  ''let best cels make next generation
  for i = 20 to 200
    a = int( rnd(0) * 20 )
    b = int( rnd(0) * 20 )
    call cel.mix ry( i ) , ry( a ) , ry( b )
    if rnd(0) < 0.5 then
      call cel.mutate ry( i )
    end if
  next i
next gen
q = 0
''test the best cel
for i = 0 to 999
  x = int( rnd(0) * 20 ) - 10
  y = int( rnd(0) * 20 ) - 10
  cel( ry( 0 ) , 0 ) = x
  cel( ry( 0 ) , 1 ) = y
  if sign( cel.uit( ry( 0 ) ) ) <> f( x , y ) then
    q = q + 1
  end if
  print sign( cel.uit( ry( 0 ) ) ) , f( x , y ) _
  , "error = " ; q/10 ; " %"
next i
end

function sign( x )
  sign = ( x < 0 ) - ( x > 0 )
''  uit = 0
''  if x < 0 then uit = -1
''  if x > 0 then uit = 1
''  sign = uit
end function
function f( x , y )
  f = ( x >= y ) - ( x < y )
''  uit = -1
''  if x < y then uit = 1
''  f = uit
end function
function cel.uit( no )
  sum = w( no , maxin + 1 ) / hoog - .5
  for i = 0 to maxin
    sum = sum + cel(no,i)*w(no,i)/hoog - .5
  next i
  cel.uit = sum
end function
sub cel.mutate no
  dw = int( rnd(0) * ( maxin + 1 ) )
  m = int( rnd(0) * 24 )
  w( no , dw ) = w( no , dw ) xor 2 ^ m
end sub
sub cel.mix uit , a , b
  for i = 0 to maxin
    w( uit , i ) = 0
    for m = 0 to 24
      if rnd(0) < .5 then
        w(uit,i)=w(uit,i)+(w(a,i)and 2^m)
      else
        w(uit,i)=w(uit,i)+(w(b,i)and 2^m)
      end if
    next m
  next i
end sub