Liberty BASIC Community Forum
« AI : neural net trou GA »

Welcome Guest. Please Login or Register.
Mar 29th, 2017, 04:17am


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


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: AI : neural net trou GA  (Read 106 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: 857
xx AI : neural net trou GA
« Thread started on: Jun 20th, 2016, 03:32am »


this is a try at a counting NN
it lerns trou a genetic algoritm
[ i dont understand backprop jet ]

error :
er( ry( 0 ) ) stays 12
shoot be decreasing

Code:
dim in( 200 , 2 ) , h( 200 , 2 ) , uit( 200 , 2 ) , ry( 200 )
dim w( index( 200 , 2 , 2 ) ) , a( 7 ) , er( 200 )
for i = 0 to 7
  a( i ) = ( i + 1 ) and 7
next i
for i = 0 to 200
  ry( i ) = i
  for j = 0 to 2
    for k = 0 to 2
      w( index( i , j , k ) ) = rnd( 0 )
    next k
  next j
next i

for gen = 0 to 25
  for i = 0 to 200
    er( i ) = 0
    for in = 0 to 7
      for m = 0 to 2
        in( i , m ) = ( in and 2 ^ m ) / 2 ^ m
      next m
      call ff i
      er( i ) = er( i ) + fout( i , in )
    next in
  next i
  for h = 1 to 200
    for l = 0 to h - 1
      if er( ry( h ) ) > er( ry( l ) ) then
        q = ry( h )
        ry( h ) = ry( l )
        ry( l ) = q
      end if
    next l
  next h
  print gen , er( ry( 0 ) )
  for i = 20 to 200
    a = int( range( 0 , 20 ) )
    b = int( range( 0 , 20 ) )
    call mix a , b , i
    if rnd( 0 ) < 0.5 then call mutate i
  next i
next gen
print "[ game over ]"


end
sub mix a , b , uit
  for i = 0 to 2
    for j = 0 to 2
      if rnd(0) < 0.5 then
        w( index( uit , i , j ) ) = w( index( a , i , j ) )
      else
        w( index( uit , i , j ) ) = w( index( b , i , j ) )
      end if
    next j
  next i
end sub
sub mutate a
  i = range( 0 , 2 )
  j = range( 0 , 2 )
  w( index( a , i , j ) ) = rnd( 0 )
end sub
function fout( no , i )
  uit = 3
    for m = 0 to 2
      if uit( no , m ) * 2 ^ m = ( a( i ) and 2 ^ m ) then
        uit = uit - 1
      end if
    next m
  fout = uit
end function
sub ff no
  for i = 0 to 2
    sum = 0
    for j = 0 to 2
      sum = sum + in( no , j ) * w( index( no , i , j ) )
    next j
    h( no , i ) = sum
  next i
  for i = 0 to 2
    sum = 0
    for j = 0 to 2
      sum = sum + h( no , j ) * w( index( no , i , j ) )
    next j
    uit( no , i ) = is( sum )
  next i
end sub
function range( l , h )
  range = rnd( 0 ) * ( h - l ) + l
end function
function index( no , layer , in )
  index = no * 9 + layer * 3 + in
end function
function is( x )
  uit = 0
  if x >= 0 then uit = 1
  is = uit
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
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: 857
xx Re: AI : neural net trou GA
« Reply #1 on: Jun 20th, 2016, 03:39am »

update :
it lerns a litle now
Code:
dim in( 200 , 2 ) , h( 200 , 2 ) , uit( 200 , 2 ) , ry( 200 )
dim w( index( 200 , 2 , 2 ) ) , a( 7 ) , er( 200 )
for i = 0 to 7
  a( i ) = ( i + 1 ) and 7
next i
for i = 0 to 200
  ry( i ) = i
  for j = 0 to 2
    for k = 0 to 2
      w( index( i , j , k ) ) = rnd( 0 ) - rnd( 0 )
    next k
  next j
next i

for gen = 0 to 25
  for i = 0 to 200
    er( i ) = 0
    for in = 0 to 7
      for m = 0 to 2
        in( i , m ) = ( in and 2 ^ m ) / 2 ^ m
      next m
      call ff i
      er( i ) = er( i ) + fout( i , in )
    next in
  next i
  for h = 1 to 200
    for l = 0 to h - 1
      if er( ry( h ) ) < er( ry( l ) ) then
        q = ry( h )
        ry( h ) = ry( l )
        ry( l ) = q
      end if
    next l
  next h
  print gen , er( ry( 0 ) )
  for i = 20 to 200
    a = int( range( 0 , 20 ) )
    b = int( range( 0 , 20 ) )
    call mix ry( a ) , ry( b ) , i
    if rnd( 0 ) < 0.5 then call mutate i
  next i
next gen
print "[ game over ]"


end
sub mix a , b , uit
  for i = 0 to 2
    for j = 0 to 2
      if rnd(0) < 0.5 then
        w( index( uit , i , j ) ) = w( index( a , i , j ) )
      else
        w( index( uit , i , j ) ) = w( index( b , i , j ) )
      end if
    next j
  next i
end sub
sub mutate a
  i = range( 0 , 2 )
  j = range( 0 , 2 )
  w( index( a , i , j ) ) = rnd( 0 ) - rnd( 0 )
end sub
function fout( no , i )
  uit = 3
    for m = 0 to 2
      if uit( no , m ) * 2 ^ m = ( a( i ) and 2 ^ m ) then
        uit = uit - 1
      end if
    next m
  fout = uit
end function
sub ff no
  for i = 0 to 2
    sum = 0
    for j = 0 to 2
      sum = sum + in( no , j ) * w( index( no , i , j ) )
    next j
    h( no , i ) = sum
  next i
  for i = 0 to 2
    sum = 0
    for j = 0 to 2
      sum = sum + h( no , j ) * w( index( no , i , j ) )
    next j
    uit( no , i ) = is( sum )
  next i
end sub
function range( l , h )
  range = rnd( 0 ) * ( h - l ) + l
end function
function index( no , layer , in )
  index = no * 9 + layer * 3 + in
end function
function is( x )
  uit = 0
  if x >= 0 then uit = 1
  is = uit
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
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