Author Topic: Artificial neural network and back propagation  (Read 8524 times)

0 Members and 1 Guest are viewing this topic.

Offline The Jazz Man

  • Newbie
  • Posts: 19
    • View Profile
Artificial neural network and back propagation
« on: April 06, 2021, 06:29:16 pm »
.
« Last Edit: May 17, 2021, 01:07:45 am by The Jazz Man »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Artificial neural network and back propagation
« Reply #1 on: April 06, 2021, 06:42:49 pm »
Looks good, if have trouble try -1 * x as I had to with my own plot program:

compare: https://en.wikipedia.org/wiki/Sigmoid_function

to

  [ You are not allowed to view this attachment ]  


Welcome to forum @The Jazz Man
« Last Edit: April 06, 2021, 06:46:20 pm by bplus »

Offline The Jazz Man

  • Newbie
  • Posts: 19
    • View Profile
Re: Artificial neural network and back propagation
« Reply #2 on: April 06, 2021, 07:03:38 pm »
.
« Last Edit: May 17, 2021, 01:08:13 am by The Jazz Man »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Artificial neural network and back propagation
« Reply #3 on: April 06, 2021, 07:11:10 pm »
I put your code up in IDE, immediately it red lines GOTO 453 on line 203.

Are all those arrays you are using 10 items or less? or maybe you will need to DIM a few arrays?

Also I don't see any values for variables for top limits of your For Loops.
« Last Edit: April 06, 2021, 07:12:34 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Artificial neural network and back propagation
« Reply #4 on: April 06, 2021, 07:22:06 pm »
OH hey are you translating a Morristown NJ program?

Programs written before there was an ELSE for IF... THEN...

They had to use GOTO every time you turn a corner! LOL

AND ALWAYS ALL CAPITALS :)
« Last Edit: April 06, 2021, 07:24:34 pm by bplus »

Offline The Jazz Man

  • Newbie
  • Posts: 19
    • View Profile
Re: Artificial neural network and back propagation
« Reply #5 on: April 06, 2021, 07:39:08 pm »
.
« Last Edit: May 17, 2021, 01:08:56 am by The Jazz Man »

Offline The Jazz Man

  • Newbie
  • Posts: 19
    • View Profile
Re: Artificial neural network and back propagation
« Reply #6 on: April 06, 2021, 07:43:25 pm »
.
« Last Edit: May 17, 2021, 01:09:10 am by The Jazz Man »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Artificial neural network and back propagation
« Reply #7 on: April 06, 2021, 08:31:19 pm »
Well good luck here is my little foray into Neural Nets, pattern recognition:
Code: QB64: [Select]
  1. _Title "NN 1" 'by B+ started 2018-09-08 my first attempt with training a Neural Net
  2.  
  3. '2018-09-09 retest this updating the Bias Weight with the others, though I suspect it will stir up a storm?
  4.  
  5. Const WW = 800
  6. Const WH = 620
  7. Screen _NewImage(WW, WH, 32)
  8. _ScreenMove 300, 60
  9.  
  10. 'need a place to store inputs x, y, B for bias
  11. 'well just use loops
  12. 'dim and init weights
  13. Const LR = .2 'Learning Rate don't overcorrect errors
  14. Dim Shared BiasWeight As Single, Bias As Integer, ArrayStart As Integer, ArrayEnd As Integer, SQ As Integer, Mode As Integer
  15. Dim i As Integer, rx As Integer, ry As Integer, colr As Long, cnt As Integer, x As Integer, y As Integer, correct As Single
  16. Mode = 1
  17. ArrayStart = 1 'Start range of x, y
  18. ArrayEnd = 60 'End range of x, y
  19. SQ = 10 'for graphic squares drawing to watch Perceptron Learn Y > X
  20.  
  21. 'setup weights
  22. Dim Shared WX(ArrayStart To ArrayEnd) As Single, WY(ArrayStart To ArrayEnd) As Single
  23. Bias = 1
  24. BiasWeight = Rnd * 2 - 1
  25. For i = ArrayStart To ArrayEnd
  26.     WX(i) = Rnd * 2 - 1
  27.     WY(i) = Rnd * 2 - 1
  28.  
  29. '61 x 61 squares of side 10 pixels fit in 610 x 610 pixel area
  30. 'train 1000 random points per frame 10 frames per second and show progress of learning hopefully
  31. While _KeyDown(27) = 0
  32.     Cls
  33.     If Mode = 1 Then _Title "Training Pattern #1: Y >= X" Else _Title "Training Pattern #2: Framed!"
  34.     cnt = 0
  35.     For i = 1 To 1000 'train 10% of points at a time
  36.         rx = Int(Rnd * ArrayEnd) + 1: ry = Int(Rnd * ArrayEnd) + 1 '60 x 60 field so can show circles of radius 10
  37.         Train rx, ry
  38.     Next
  39.     For y = ArrayStart To ArrayEnd
  40.         For x = ArrayStart To ArrayEnd
  41.             If Mode = 1 Then
  42.                 If TrainThis%(x, y) = -1 Then Line (x * SQ, y * SQ)-Step(SQ, SQ), _RGB32(0, 0, 255), B
  43.                 If TrainThis%(x, y) = 1 Then Line (x * SQ, y * SQ)-Step(SQ, SQ), _RGB32(255, 255, 255), B
  44.                 If TrainThis%(x, y) - Perceptron%(x, y) Then 'wrong! Perceptron
  45.                     colr = _RGB32(255, 0, 0)
  46.                 Else 'good job Perceptron!
  47.                     colr = _RGB32(0, 200, 0): cnt = cnt + 1
  48.                 End If
  49.             Else
  50.                 If TrainThat%(x, y) = -1 Then Line (x * SQ, y * SQ)-Step(SQ, SQ), _RGB32(0, 0, 255), B
  51.                 If TrainThat%(x, y) = 1 Then Line (x * SQ, y * SQ)-Step(SQ, SQ), _RGB32(255, 255, 255), B
  52.                 If TrainThat%(x, y) - Perceptron%(x, y) Then 'wrong! Perceptron
  53.                     colr = _RGB32(255, 0, 0)
  54.                 Else 'good job Perceptron!
  55.                     colr = _RGB32(0, 200, 0): cnt = cnt + 1
  56.                 End If
  57.             End If
  58.             Line (x * SQ + 2, y * SQ + 2)-Step(SQ - 3, SQ - 3), colr, BF
  59.         Next
  60.     Next
  61.     correct = Int(cnt * 10000 / (61 * 61)) / 100
  62.     _PrintString (650, 10), "Correct:" + Str$(correct) + "%"
  63.     If Mode = 1 Then
  64.         _PrintString (640, 30), "White Frame Y >= X"
  65.         _PrintString (640, 50), " Blue Frame Y <  X"
  66.     Else
  67.         _PrintString (620, 30), "White Frame Train +1"
  68.         _PrintString (620, 50), " Blue Frame Train -1"
  69.         _PrintString (640, 180), "Wait for it... ;)"
  70.     End If
  71.     _PrintString (640, 70), "Green Fill Correct"
  72.     _PrintString (640, 90), "Red Fill Incorrect"
  73.     _PrintString (620, 130), "Training 1000 random"
  74.     _PrintString (620, 146), "points in 10th second."
  75.     If correct > 95.51 And Mode = 1 Then
  76.         Mode = 2
  77.         mBox "Hmm... there seems to be a real battle at the border. OK 95.5% has been exceeded, let's see how fast this retrains to a new pattern...", "OK Now Test New Training Set!"
  78.     ElseIf Mode = 2 And correct > 96.5 Then
  79.         mBox "Over a 96.5% chance you're pregnant!", "OMG!"
  80.         System
  81.     End If
  82.     _Display
  83.     _Limit 10
  84.  
  85. Function Perceptron% (x As Integer, y As Integer)
  86.     Dim sum As Single
  87.     sum = x * WX(x) + y * WY(y) + Bias * BiasWeight 'sum the inputs times weight
  88.     Perceptron% = Sign%(sum) 'apply the activation function to the sum for output
  89.  
  90. Function Sign% (n As Single) 'very simple activation function
  91.     If n < 0 Then Sign% = -1 Else Sign% = 1
  92.  
  93. 'this sub trains one randomly chosen Perceptron weight set
  94. Sub Train (rx As Integer, ry As Integer)
  95.     'adjust Perceptrons weights until get good results
  96.     '1. provide Perceptron with Inputs for which the correct answer is known
  97.     '2. have perceptron guess the answer
  98.     '3. Compute the error
  99.     '4. Adjust weights according to error
  100.     '5. repeat until shaped up
  101.  
  102.     'so what are we going to train , oh there it is
  103.     Dim Guess As Integer, Correct As Integer, Errror As Single, OK$
  104.     Guess = Perceptron%(rx, ry)
  105.     If Mode = 1 Then
  106.         Correct = TrainThis%(rx, ry)
  107.     Else
  108.         Correct = TrainThat%(rx, ry)
  109.     End If
  110.     Errror = Correct - Guess 'either 0 when Guess = Correct  -2 or 2 when not
  111.     If Errror Then
  112.         WX(rx) = WX(rx) + rx * Errror * LR
  113.         WY(ry) = WY(ry) + ry * Errror * LR
  114.         BiasWeight = BiasWeight + 1 * Errror * LR
  115.     End If
  116.  
  117. Function TrainThis% (x As Integer, y As Integer)
  118.     If y >= x Then TrainThis% = 1 Else TrainThis% = -1 'the x = y function is the line between true and false
  119.  
  120. Function TrainThat% (x As Integer, y As Integer) 'draw a frame
  121.     If x = ArrayStart Or x = ArrayStart + 1 Or x = ArrayEnd - 1 Or x = ArrayEnd Then
  122.         TrainThat% = 1
  123.     ElseIf y = ArrayStart Or y = ArrayStart + 1 Or y = ArrayEnd - 1 Or y = ArrayEnd Then
  124.         TrainThat% = 1
  125.     ElseIf x >= 29 And x <= 32 And y >= 12 And y <= 49 Then
  126.         TrainThat% = 1
  127.     ElseIf y >= 29 And y <= 32 And x >= 12 And x <= 49 Then
  128.         TrainThat% = 1
  129.     Else
  130.         TrainThat% = -1
  131.     End If
  132.  
  133. 'title$ limit is 57 chars, all lines are 58 chars max
  134. ' version bak 2018-09-07_10P
  135. Sub mBox (m$, title$)
  136.  
  137.     'first screen dimensions items to restore at exit
  138.     Dim curScrn As Long, backScrn As Long, mbx As Long 'some handles
  139.     Dim ti As Integer, limit As Integer 'ti = text index for t$(), limit is number of chars per line
  140.     Dim i As Integer, j As Integer, ff As _Bit, add As _Byte 'index, flag and
  141.     Dim bxH As Integer, bxW As Integer 'first as cells then as pixels
  142.     Dim mb As Integer, mx As Integer, my As Integer, mi As Integer, grabx As Integer, graby As Integer
  143.     Dim tlx As Integer, tly As Integer 'top left corner of message box
  144.     Dim lastx As Integer, lasty As Integer, r As Integer
  145.     Dim b$, c$, tail$, d$
  146.     sw = _Width
  147.     sh = _Height
  148.     fg = _DefaultColor
  149.     bg = _BackgroundColor
  150.     'screen snapshot
  151.     curScrn = _Dest
  152.     backScrn = _NewImage(sw, sh, 32)
  153.     _PutImage , curScrn, backScrn
  154.  
  155.     'setup t$() to store strings with ti as index, linit 58 chars per line max, b$ is for build
  156.     ReDim t$(0): ti = 0: limit = 58: b$ = ""
  157.     For i = 1 To Len(m$)
  158.         c$ = Mid$(m$, i, 1)
  159.         'are there any new line signals, CR, LF or both? take CRLF or LFCR as one break but dbl LF or CR means blank line
  160.         Select Case c$
  161.             Case Chr$(13) 'load line
  162.                 If Mid$(m$, i + 1, 1) = Chr$(10) Then i = i + 1
  163.                 t$(ti) = b$: b$ = "": ti = ti + 1: ReDim _Preserve t$(ti)
  164.             Case Chr$(10)
  165.                 If Mid$(m$, i + 1, 1) = Chr$(13) Then i = i + 1
  166.                 t$(ti) = b$: b$ = "": ti = ti + 1: ReDim _Preserve t$(ti)
  167.             Case Else
  168.                 If c$ = Chr$(9) Then c$ = Space$(4): add = 4 Else add = 1
  169.                 If Len(b$) + add > limit Then
  170.                     tail$ = "": ff = 0
  171.                     For j = Len(b$) To 1 Step -1 'backup until find a space, save the tail end for next line
  172.                         d$ = Mid$(b$, j, 1)
  173.                         If d$ = " " Then
  174.                             t$(ti) = Mid$(b$, 1, j - 1): b$ = tail$ + c$: ti = ti + 1: ReDim _Preserve t$(ti)
  175.                             ff = 1 'found space flag
  176.                             Exit For
  177.                         Else
  178.                             tail$ = d$ + tail$ 'the tail grows!
  179.                         End If
  180.                     Next
  181.                     If ff = 0 Then 'no break? OK
  182.                         t$(ti) = b$: b$ = c$: ti = ti + 1: ReDim _Preserve t$(ti)
  183.                     End If
  184.                 Else
  185.                     b$ = b$ + c$ 'just keep building the line
  186.                 End If
  187.         End Select
  188.     Next
  189.     t$(ti) = b$
  190.     bxH = ti + 3: bxW = limit + 2
  191.  
  192.     'draw message box
  193.     mbx = _NewImage(60 * 8, (bxH + 1) * 16, 32)
  194.     _Dest mbx
  195.     Color _RGB32(60, 40, 25), _RGB32(225, 225, 255)
  196.     Locate 1, 1: Print Left$(Space$((bxW - Len(title$) - 3) / 2) + title$ + Space$(bxW), bxW)
  197.     Color _RGB32(225, 225, 255), _RGB32(200, 0, 0)
  198.     Locate 1, bxW - 2: Print " X "
  199.     Color _RGB32(60, 40, 25), _RGB32(255, 160, 90)
  200.     Locate 2, 1: Print Space$(bxW);
  201.     For r = 0 To ti
  202.         Locate 1 + r + 2, 1: Print Left$(" " + t$(r) + Space$(bxW), bxW);
  203.     Next
  204.     Locate 1 + bxH, 1: Print Space$(limit + 2);
  205.  
  206.     'now for the action
  207.     _Dest curScrn
  208.  
  209.     'convert to pixels the top left corner of box at moment
  210.     bxW = bxW * 8: bxH = bxH * 16
  211.     tlx = (sw - bxW) / 2: tly = (sh - bxH) / 2
  212.     lastx = tlx: lasty = tly
  213.     'now allow user to move it around or just read it
  214.     While _KeyDown(27) = 0 And _KeyDown(13) = 0 And _KeyDown(32) = 0
  215.         Cls
  216.         _PutImage , backScrn
  217.         _PutImage (tlx, tly), mbx, curScrn
  218.         _Display
  219.         While _MouseInput: Wend
  220.         mx = _MouseX: my = _MouseY: mb = _MouseButton(1)
  221.         If mb Then
  222.             If mx >= tlx And mx <= tlx + bxW And my >= tly And my <= tly + 16 Then 'mouse down on title bar
  223.                 If mx >= tlx + bxW - 24 Then Exit While
  224.                 grabx = mx - tlx: graby = my - tly
  225.                 Do While mb 'wait for release
  226.                     mi = _MouseInput: mb = _MouseButton(1)
  227.                     mx = _MouseX: my = _MouseY
  228.                     If mx - grabx >= 0 And mx - grabx <= sw - bxW And my - graby >= 0 And my - graby <= sh - bxH Then
  229.                         'attempt to speed up with less updates
  230.                         If ((lastx - (mx - grabx)) ^ 2 + (lasty - (my - graby)) ^ 2) ^ .5 > 10 Then
  231.                             tlx = mx - grabx: tly = my - graby
  232.                             Cls
  233.                             _PutImage , backScrn
  234.                             _PutImage (tlx, tly), mbx, curScrn
  235.                             lastx = tlx: lasty = tly
  236.                             _Display
  237.                         End If
  238.                     End If
  239.                     _Limit 400
  240.                 Loop
  241.             End If
  242.         End If
  243.         _Limit 400
  244.     Wend
  245.     'put things back
  246.     Color _RGB32(255, 255, 255), _RGB32(0, 0, 0): Cls
  247.     _PutImage , backScrn
  248.     _Display
  249.     Color fg, bg
  250.     _FreeImage backScrn
  251.     _FreeImage mbx
  252.     _KeyClear
  253.  
  254.  
« Last Edit: April 06, 2021, 08:32:32 pm by bplus »

Offline The Jazz Man

  • Newbie
  • Posts: 19
    • View Profile
Re: Artificial neural network and back propagation
« Reply #8 on: April 06, 2021, 09:29:18 pm »
.
« Last Edit: May 17, 2021, 01:15:47 am by The Jazz Man »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Artificial neural network and back propagation
« Reply #9 on: April 07, 2021, 12:47:45 am »
@bplus
cheers for the example Bplus.
it appears that you are a much more advance programmer than me, it will take me a while to wrap my brain around this.
thank you for trying to help.
cheers

Yes there is a fine mBox (message box) subroutine in there, but the results of my experiment with neural nets was dismal (the code originally attempted digit recognition) but hated to see all that code go to waste so I tried for a funny. :)

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Artificial neural network and back propagation
« Reply #10 on: April 07, 2021, 10:50:58 am »
Hello Jazz Man

May I ask a few questions on the overall flow of your program? No problem if you'd prefer not to go into anything that you may wish not to get into.

- I gather the user of your game would not only pick which horse will win but also is there $$$$ bet which determines an ultimate value of money won?
- It would appear the program is a horse race of 3 horses(?), the winner is ??? the one which won the race or the one with the most value or prize money won by the person who bet??
- If the horse winning and value of the win are the same thing, then is the "Back Propagation" and Leaning Rate directed at improving the results of the two losing horses and higher winnings?
- Does each horse start the race with an equal weighting, then in hidden layer 2 a random weight is placed on each of them???
- Hidden Layer 3, The Error Layer, is determining if the horse picked to win did in fact win, or is it monitoring a pattern in the random values?

I apologize in advance for these naive questions. I am interested in AI and have absolutely zero back ground in either programming or calculus. I've been to a horse race track only once in my life and I did bet and , beginners luck, I did bet on the right horse to win. But there was one guy in our group who made more money on the horse that came in third than I did for the win.

I have tried to follow your code for the calculation and application of Back Propagation and Leaning Rate. Both of these, I believe are triggered by the Error but not sure of that or what actually constitutes an error. 

I'll be following this tread - who knows you may find a real life application.

Offline The Jazz Man

  • Newbie
  • Posts: 19
    • View Profile
Re: Artificial neural network and back propagation
« Reply #11 on: April 17, 2021, 06:42:19 am »
.
« Last Edit: May 17, 2021, 01:09:48 am by The Jazz Man »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Artificial neural network and back propagation
« Reply #12 on: April 17, 2021, 04:54:10 pm »
Quote
and I hope that somebody will help me out with my plain math calculus request.

@The Jazz Man
What?

Offline The Jazz Man

  • Newbie
  • Posts: 19
    • View Profile
Re: Artificial neural network and back propagation
« Reply #13 on: April 19, 2021, 09:47:56 am »
.
« Last Edit: May 17, 2021, 01:10:02 am by The Jazz Man »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Artificial neural network and back propagation
« Reply #14 on: April 19, 2021, 12:02:20 pm »
Well all I can say for sure is the formula is correct as per my first reply and I my attempts with neural nets had not been satisfactory either.

You keep referring to math or calculus, it's really a matter of getting the algorithm for the training the neural net correct, more a computer science problem than a math one though Logic is foundation of both.