_Title "NN 1" 'by B+ started 2018-09-08 my first attempt with training a Neural Net
'2018-09-09 retest this updating the Bias Weight with the others, though I suspect it will stir up a storm?
'need a place to store inputs x, y, B for bias
'well just use loops
'dim and init weights
Const LR
= .2 'Learning Rate don't overcorrect errors Mode = 1
ArrayStart = 1 'Start range of x, y
ArrayEnd = 60 'End range of x, y
SQ = 10 'for graphic squares drawing to watch Perceptron Learn Y > X
'setup weights
Bias = 1
For i
= ArrayStart
To ArrayEnd
'61 x 61 squares of side 10 pixels fit in 610 x 610 pixel area
'train 1000 random points per frame 10 frames per second and show progress of learning hopefully
cnt = 0
For i
= 1 To 1000 'train 10% of points at a time rx
= Int(Rnd * ArrayEnd
) + 1: ry
= Int(Rnd * ArrayEnd
) + 1 '60 x 60 field so can show circles of radius 10 Train rx, ry
For y
= ArrayStart
To ArrayEnd
For x
= ArrayStart
To ArrayEnd
If TrainThis%
(x
, y
) - Perceptron%
(x
, y
) Then 'wrong! Perceptron Else 'good job Perceptron! colr
= _RGB32(0, 200, 0): cnt
= cnt
+ 1 If TrainThat%
(x
, y
) - Perceptron%
(x
, y
) Then 'wrong! Perceptron Else 'good job Perceptron! colr
= _RGB32(0, 200, 0): cnt
= cnt
+ 1 Line (x
* SQ
+ 2, y
* SQ
+ 2)-Step(SQ
- 3, SQ
- 3), colr
, BF
correct
= Int(cnt
* 10000 / (61 * 61)) / 100 Mode = 2
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!"
mBox "Over a 96.5% chance you're pregnant!", "OMG!"
sum = x * WX(x) + y * WY(y) + Bias * BiasWeight 'sum the inputs times weight
Perceptron% = Sign%(sum) 'apply the activation function to the sum for output
'this sub trains one randomly chosen Perceptron weight set
'adjust Perceptrons weights until get good results
'1. provide Perceptron with Inputs for which the correct answer is known
'2. have perceptron guess the answer
'3. Compute the error
'4. Adjust weights according to error
'5. repeat until shaped up
'so what are we going to train , oh there it is
Guess = Perceptron%(rx, ry)
Correct = TrainThis%(rx, ry)
Correct = TrainThat%(rx, ry)
Errror = Correct - Guess 'either 0 when Guess = Correct -2 or 2 when not
WX(rx) = WX(rx) + rx * Errror * LR
WY(ry) = WY(ry) + ry * Errror * LR
BiasWeight = BiasWeight + 1 * Errror * LR
If y
>= x
Then TrainThis%
= 1 Else TrainThis%
= -1 'the x = y function is the line between true and false
If x
= ArrayStart
Or x
= ArrayStart
+ 1 Or x
= ArrayEnd
- 1 Or x
= ArrayEnd
Then TrainThat% = 1
ElseIf y
= ArrayStart
Or y
= ArrayStart
+ 1 Or y
= ArrayEnd
- 1 Or y
= ArrayEnd
Then TrainThat% = 1
TrainThat% = 1
TrainThat% = 1
TrainThat% = -1
'title$ limit is 57 chars, all lines are 58 chars max
' version bak 2018-09-07_10P
'first screen dimensions items to restore at exit
'screen snapshot
'setup t$() to store strings with ti as index, linit 58 chars per line max, b$ is for build
ReDim t$
(0): ti
= 0: limit
= 58: b$
= "" '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
tail$ = "": ff = 0
For j
= Len(b$
) To 1 Step -1 'backup until find a space, save the tail end for next line ff = 1 'found space flag
tail$ = d$ + tail$ 'the tail grows!
b$ = b$ + c$ 'just keep building the line
t$(ti) = b$
bxH = ti + 3: bxW = limit + 2
'draw message box
'now for the action
'convert to pixels the top left corner of box at moment
bxW = bxW * 8: bxH = bxH * 16
tlx = (sw - bxW) / 2: tly = (sh - bxH) / 2
lastx = tlx: lasty = tly
'now allow user to move it around or just read it
If mx
>= tlx
And mx
<= tlx
+ bxW
And my
>= tly
And my
<= tly
+ 16 Then 'mouse down on title bar grabx = mx - tlx: graby = my - tly
If mx
- grabx
>= 0 And mx
- grabx
<= sw
- bxW
And my
- graby
>= 0 And my
- graby
<= sh
- bxH
Then 'attempt to speed up with less updates
If ((lastx
- (mx
- grabx
)) ^ 2 + (lasty
- (my
- graby
)) ^ 2) ^ .5 > 10 Then tlx = mx - grabx: tly = my - graby
lastx = tlx: lasty = tly
'put things back