Code: Select all
; code edited, see last post

Code: Select all
; code edited, see last post


Code: Select all
; Neural network Pattern recognition example by Pedro Gil (Balrog Software)
; First we initialize the variables
; Edited to solve some bugs and make better graphics
Enumeration
#Window_0
EndEnumeration
;- Gadget Constants
;
Enumeration
#Pattern
#Frame3D_0
#Teach
#TeachX
#Text_0
#XNb
#Recognise
#Information
EndEnumeration
CreateImage(2,66,66)
CreateImage(3,220,170)
Procedure Open_Window_0()
If OpenWindow(#Window_0, 0, 0, 315, 205, "Neural Network", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
ImageGadget(#Pattern, 10, 20, 66, 66, ImageID(2))
FrameGadget(#Frame3D_0, 5, 5, 305, 195, "Neural Network Pattern Recognition")
ButtonGadget(#Teach, 10, 95, 70, 20, "Teach")
ButtonGadget(#TeachX, 10, 120, 70, 20, "Teach X")
TextGadget(#Text_0, 15, 150, 15, 15, "X:")
StringGadget(#XNb, 35, 145, 45, 20, "", #PB_String_Numeric)
ButtonGadget(#Recognise, 10, 170, 70, 20, "Recognise")
ButtonImageGadget(#Information, 85, 20, 220, 175, ImageID(3))
EndIf
EndProcedure
MaxNeuralNetNumber = 1
MaxInputNumber = 256
InputNumbers = 256
InputComb = Pow(MaxInputNumber, 2)
Dim Image.b(16, 16)
Dim W.f(MaxNeuralNetNumber, MaxInputNumber); this is the weights
Dim Net(MaxNeuralNetNumber); this is the output for the neurals network
Dim Output(MaxNeuralNetNumber); if the Net is greater than 0 the output gets 1
Dim Inputs(MaxNeuralNetNumber, MaxInputNumber); this set the inputs for the neural networks
Dim InputImportance.s(MaxNeuralNetNumber, Inputcomb); input importance is for the supervised learning
Dim Error.f(50000)
Dim Correct.f(50000)
; and say to the neural network that this input
; must gets an output
W(1, 0)=1 ; we set a first values for neural networks
For Weights = 1 To InputNumbers ; exist also W(1,0) but this line is only to make more easy
W(1, Weights)=Random(5)-3 ; calculations
Next Weights
Open_Window_0()
Gosub MakeInfo
Info=0
Repeat
WEvent=WindowEvent()
If WEvent=#PB_Event_CloseWindow
Quit=1
ElseIf WEvent=#PB_Event_Gadget
GEvent=EventGadget()
If GEvent=#Teach
TeachRep=1
Teach=#True
Gosub Recognise
Teach=#False
ElseIf GEvent=#Recognise
TeachRep=1
Gosub Recognise
ElseIf GEvent=#TeachX
TeachRep=Val(GetGadgetText(#XNb))
Teach=#True
Gosub Recognise
Teach=#False
ElseIf GEvent=#Information
Info+1
If Info=3
Info=0
EndIf
Gosub MakeInfo
EndIf
EndIf
Until Quit=1
End
Recognise:
Ta.f = 0.9 ; Ta.f is the learning constant, and must be between 0 and 1
; you can think that when this value was higher would be
; better, because you can think that it can learn more faster
; but usually not, a high value can make that weights of the
; neural networks change very faster, and dont get the
; optimal value.
For Rep = 1 To TeachRep
; Create a 16x16 image with a random figure (box or ellipse), with noise
CreateImage(1, 16, 16)
If StartDrawing(ImageOutput(1))
FrontColor(RGB(255, 255, 255))
BackColor(RGB(0, 0, 0))
DrawingMode(4)
Fig = Random(1)
If Fig = 1
Ellipse(7 + Random(2), 7 + Random(2), 5 + Random(2), 5 + Random(2))
ElseIf Fig=0
Box(1 + Random(3), 1 + Random(3), 9 + Random(3), 9+ Random(3))
; Else
; For d=0 To Random(1)
; LineXY(Random(16),Random(16),Random(16),Random(16))
; Next
EndIf
For t = 0 To Random(16*2)
If Random(1)
FrontColor(RGB(255, 255, 255))
Else
FrontColor(RGB(0, 0, 0))
EndIf
Plot(Random(15), Random(15))
Next t
; Set inputs of Neural Network with each pixel of image
cnt = 0
For y = 0 To 15
For x = 0 To 15
c = Point(x, y)
If c = 0
Image(x, y)=0
Else
Image(x, y)=1
EndIf
cnt + 1
Inputs(1, cnt)=Image(x, y)
Next x
Next y
StopDrawing()
EndIf
CreateImage(2,66,66)
If StartDrawing(ImageOutput(2))
DrawImage(ImageID(1),1,1,64,64)
StopDrawing()
EndIf
SetGadgetState(#Pattern, ImageID(2))
Net(1)=W(1, 0) ; and we get an output for the net
For In = 1 To InputNumbers
Net(1)+(Inputs(1, In)*W(1, In))
Next In
; **
If Net(1)=>0 ; and if Net its greater than 0 we get an output
Output(1)=#True
Else
Output(1)=#False
EndIf
If Fig=1;) Or (Output(1)=0 And Fig<>1)
InputImportance=1
Else
InputImportance=0
EndIf
If Output(1)=1
Figure.s = "Ellipse"
Else
Figure.s = "Non-Ellipse"
EndIf
; execute to modify weights, if you want to iterate without teach to neural network this code is not executed
If Teach=#True
Increment = InputImportance - Output(1) ; get the difference of the output of the net and the correct output
For In = 1 To InputNumbers
W(1, In)=W(1, In)+Ta.f * Increment * Inputs(1, In) ; and get the new weights
Next In
W(1, 0)=W(1, 0)+Ta.f * Increment
EndIf
If (Output(1)=1 And Fig=1) Or (Output(1)=0 And Fig<>1)
Correct + 1
Else
Wrong + 1
EndIf
AllPatterns + 1
Error(AllPatterns)=100*Wrong/AllPatterns
Correct(AllPatterns)=100*Correct/AllPatterns
Gosub MakeInfo
Next Rep
Return
MakeInfo:
If Info=0
CreateImage(3,220,170)
If StartDrawing(ImageOutput(3))
a.f=0
For x=0 To 255
cl=48*(W(1,x))+127
If cl>255
cl=255
ElseIf cl<0
cl=0
EndIf
LineXY(4+(x/1.2),96-(Sin(a.f)*64),110,140, RGB(cl,0,0))
a.f+(3.141592/256)
Next x
Circle(110,140,4, RGB(0,200,0))
DrawingMode(4)
Circle(110,140,5, RGB(0,100,0))
DrawingMode(1)
DrawText(110-TextWidth(Figure)/2,150, Figure, RGB(200,200,200))
DrawText(110-TextWidth("Input weights")/2,10, "Input weights", RGB(0,200,64))
StopDrawing()
EndIf
SetGadgetAttribute(#Information, #PB_Button_Image, ImageID(3))
ElseIf Info=1
CreateImage(4,220,170)
If StartDrawing(ImageOutput(4))
FrontColor(RGB(200,200,200))
Line(9,59,0,102)
Line(7,59,5,0)
Line(7,161,5,0)
For x=1 To 201
pos=Round(x*AllPatterns/200,1)
If Error(Pos)>=0
Plot(9+x,160-(Error(pos)),RGB(255,0,0))
EndIf
If Correct(Pos)>=0
Plot(9+x,160-(Correct(pos)),RGB(0,255,0))
EndIf
Next x
DrawingMode(1)
DrawText(4,44, "100 %")
FrontColor(RGB(0,255,0))
DrawText(110-TextWidth("Correct: "+Str(Correct)+" ("+Str(Round(Correct(AllPatterns),0))+"%)")/2,2, "Correct: "+Str(Correct)+" ("+Str(Round(Correct(AllPatterns),0))+"%)")
FrontColor(RGB(255,0,0))
DrawText(110-TextWidth("Wrong: "+Str(Wrong)+" ("+Str(100-Round(Correct(AllPatterns),0))+"%)")/2,22, "Wrong: "+Str(Wrong)+" ("+Str(100-Round(Correct(AllPatterns),0))+"%)")
StopDrawing()
EndIf
SetGadgetAttribute(#Information, #PB_Button_Image, ImageID(4))
ElseIf Info=2
CreateImage(5,220,170)
If StartDrawing(ImageOutput(5))
FrontColor(RGB(200,200,200))
in=0
For y=0 To 15
For x=0 To 15
cl=48*(W(1,in))+127
in+1
If cl>255
cl=255
ElseIf cl<0
cl=0
EndIf
Box(46+x*8,32+y*8,8,8,RGB(cl,0,0))
Next x
Next y
DrawingMode(1)
FrontColor(RGB(0,255,0))
DrawText(110-TextWidth("Weights")/2,8, "Weights")
StopDrawing()
EndIf
SetGadgetAttribute(#Information, #PB_Button_Image, ImageID(5))
EndIf
Return







...giving me a headache right nowStarHawk wrote:A Neural Network is...