12-28-2005, 10:14 PM
[syntax="qbasic"]DEFINT A-Z
Declare Function sig(x As Double) As Double
Declare Sub DoNet()
Declare Sub TrainNet()
Declare Sub RunNet()
Declare Sub InitNet()
Const MAX_NODES = 20 'Maximum number of nuerons in one layer
Const NUM_IL = MAX_NODES 'Number of Nodes in Input Layer
Const NUM_HL = MAX_NODES 'Number of Nodes in Hidden Layer
Const NUM_OL = 10
Const IMG_WIDTH = 12
Const IMG_HEIGHT = 12
Const IMG_TOTAL = IMG_WIDTH * IMG_HEIGHT
Const LR = 0.025
Type Node
wt(1 To MAX_NODES) As Double 'Weights
act As Double 'Output of this Node
th As Double 'Threshold
e As Double 'Error
End Type
Dim Shared il(1 To NUM_IL) As Node 'Input Layer
Dim Shared hl(1 To NUM_HL) As Node 'Hidden Layer
Dim Shared ol(1 To NUM_OL) As Node 'Output Layer
'Digit Images
Dim Shared img(0 To 9,1 To 12, 1 To 12) As Byte
Dim Shared curImg(1 To IMG_TOTAL) As Byte
'Begin Main
'Load the Images
For i = 0 To 9
For y = 1 To 12
For x = 1 To 12
Read img(i,x,y)
Next x
Next y
Next i
End
Sub DoNet()
Dim sum As Double
'Run through nodes in IL
For i = 1 To NUM_IL
sum = 0
For j = 1 To IMG_TOTAL
sum = sum + il(i).wt(j) * curImg(j)
il(i).act = sigmoid(sum - il(i).th)
Next j
Next i
'Run through nodes in HL
For i = 1 To NUM_HL
sum = 0
For j = 1 To NUM_IL
sum = sum + hl(i).wt(j) * il(j).act
hl(i).act = sigmoid(sum - hl(i).th)
Next j
Next i
'Run through nodes in OL
For i = 1 To NUM_OL
sum = 0
For j = 1 To NUM_HL
sum = sum + ol(i).wt(j) * hl(j).act
ol(i).act = sigmoid(sum - ol(i).th)
Next j
Next i
End Function
Sub InitNet()
Dim rand As Double
Randomize Timer
'Input Layer
For i = 1 To NUM_IL
For j = 1 To IMG_TOTAL
il(i).wt(j) = (Rnd(1)/4)
Next j
Next i
'Hidden Layer
For i = 1 To NUM_HL
For j = 1 To NUM_IL
hl(i).wt(j) = (Rnd(1)/4)
Next j
Next i
'Output Layer
For i = 1 To NUM_OL
For j = 1 To NUM_HL
ol(i).wt(j) = (Rnd(1)/4)
Next j
Next i
End Sub
Sub TrainNet()
Dim numCycles As Integer
Dim current As Integer
Dim j As Integer
Dim sum As Double
Input "How many training cycles", numCycles
For i = 1 To numCycles
j = 1
current = Int(Rnd(1) * 10)
For y = 1 To IMG_WIDTH
For x = 1 To IMG_HEIGHT
curImg(j) = img(current, x, y)
j = j + 1
Next x
Next y
RunNet
'Calculate OL Error
For j = 1 To NUM_OL
If j = current Then
ol(j).e = (1 - ol(j).a) * ol(j).a * (1 - ol(j).a)
Else
ol(j).e = (0 - ol(j).a) * ol(j).a * (1 - ol(j).a)
End If
Next j
'Calculate HL Error
For j = 1 To NUM_HL
sum = 0
For k = 1 To NUM_OL
sum = sum + ol(k).e * ol(k).wt(j)
hl(j).e = hl(j).act * (1 - hl(j).act) * sum
Next k
Next j
'Adjust Weights
For j = 1 To NUM_OL
For k = 1 To NUM_HL
ol(j).wt(k) = ol(j).wt(k) + LR * ol(j).e * hl(k).act
ol(j).th = ol(j).th - LR * ol(j).e
Next k
Next j
For j = 1 To NUM_HL
For k = 1 To NUM_IL
hl(j).wt(k) = hl(j).wt(k) + LR * hl(j).e * il(k).act
hl(j).th = hl(j).th - LR * hl(j).e
Next k
Next j
Print ".";
Next i
End Sub
Sub RunNet()
Dim quit As Byte
Dim num As Byte
Input "Which number do you want to test?", num
For y = 1 To IMG_WIDTH
For x = 1 To IMG_HEIGHT
curImg(j) = img(num, x, y)
j = j + 1
Next x
Next y
DoNet
For i = 1 To NUM_OL
If ol(i).act > 0.9 Then
Print "The number is: ";
Print i
Exit For
End If
Next i
End Sub
Function sig(x As Double)
sig = 1/(1 + Exp(-x))
End Function[/syntax]
I'm getting an error on line 51.
It should have been declared already. I have as a SHARED variable too. Why isn't it recognizing it?
P.S. I have removed the DATA statements. They would've add another 100 or so lines.
Declare Function sig(x As Double) As Double
Declare Sub DoNet()
Declare Sub TrainNet()
Declare Sub RunNet()
Declare Sub InitNet()
Const MAX_NODES = 20 'Maximum number of nuerons in one layer
Const NUM_IL = MAX_NODES 'Number of Nodes in Input Layer
Const NUM_HL = MAX_NODES 'Number of Nodes in Hidden Layer
Const NUM_OL = 10
Const IMG_WIDTH = 12
Const IMG_HEIGHT = 12
Const IMG_TOTAL = IMG_WIDTH * IMG_HEIGHT
Const LR = 0.025
Type Node
wt(1 To MAX_NODES) As Double 'Weights
act As Double 'Output of this Node
th As Double 'Threshold
e As Double 'Error
End Type
Dim Shared il(1 To NUM_IL) As Node 'Input Layer
Dim Shared hl(1 To NUM_HL) As Node 'Hidden Layer
Dim Shared ol(1 To NUM_OL) As Node 'Output Layer
'Digit Images
Dim Shared img(0 To 9,1 To 12, 1 To 12) As Byte
Dim Shared curImg(1 To IMG_TOTAL) As Byte
'Begin Main
'Load the Images
For i = 0 To 9
For y = 1 To 12
For x = 1 To 12
Read img(i,x,y)
Next x
Next y
Next i
End
Sub DoNet()
Dim sum As Double
'Run through nodes in IL
For i = 1 To NUM_IL
sum = 0
For j = 1 To IMG_TOTAL
sum = sum + il(i).wt(j) * curImg(j)
il(i).act = sigmoid(sum - il(i).th)
Next j
Next i
'Run through nodes in HL
For i = 1 To NUM_HL
sum = 0
For j = 1 To NUM_IL
sum = sum + hl(i).wt(j) * il(j).act
hl(i).act = sigmoid(sum - hl(i).th)
Next j
Next i
'Run through nodes in OL
For i = 1 To NUM_OL
sum = 0
For j = 1 To NUM_HL
sum = sum + ol(i).wt(j) * hl(j).act
ol(i).act = sigmoid(sum - ol(i).th)
Next j
Next i
End Function
Sub InitNet()
Dim rand As Double
Randomize Timer
'Input Layer
For i = 1 To NUM_IL
For j = 1 To IMG_TOTAL
il(i).wt(j) = (Rnd(1)/4)
Next j
Next i
'Hidden Layer
For i = 1 To NUM_HL
For j = 1 To NUM_IL
hl(i).wt(j) = (Rnd(1)/4)
Next j
Next i
'Output Layer
For i = 1 To NUM_OL
For j = 1 To NUM_HL
ol(i).wt(j) = (Rnd(1)/4)
Next j
Next i
End Sub
Sub TrainNet()
Dim numCycles As Integer
Dim current As Integer
Dim j As Integer
Dim sum As Double
Input "How many training cycles", numCycles
For i = 1 To numCycles
j = 1
current = Int(Rnd(1) * 10)
For y = 1 To IMG_WIDTH
For x = 1 To IMG_HEIGHT
curImg(j) = img(current, x, y)
j = j + 1
Next x
Next y
RunNet
'Calculate OL Error
For j = 1 To NUM_OL
If j = current Then
ol(j).e = (1 - ol(j).a) * ol(j).a * (1 - ol(j).a)
Else
ol(j).e = (0 - ol(j).a) * ol(j).a * (1 - ol(j).a)
End If
Next j
'Calculate HL Error
For j = 1 To NUM_HL
sum = 0
For k = 1 To NUM_OL
sum = sum + ol(k).e * ol(k).wt(j)
hl(j).e = hl(j).act * (1 - hl(j).act) * sum
Next k
Next j
'Adjust Weights
For j = 1 To NUM_OL
For k = 1 To NUM_HL
ol(j).wt(k) = ol(j).wt(k) + LR * ol(j).e * hl(k).act
ol(j).th = ol(j).th - LR * ol(j).e
Next k
Next j
For j = 1 To NUM_HL
For k = 1 To NUM_IL
hl(j).wt(k) = hl(j).wt(k) + LR * hl(j).e * il(k).act
hl(j).th = hl(j).th - LR * hl(j).e
Next k
Next j
Print ".";
Next i
End Sub
Sub RunNet()
Dim quit As Byte
Dim num As Byte
Input "Which number do you want to test?", num
For y = 1 To IMG_WIDTH
For x = 1 To IMG_HEIGHT
curImg(j) = img(num, x, y)
j = j + 1
Next x
Next y
DoNet
For i = 1 To NUM_OL
If ol(i).act > 0.9 Then
Print "The number is: ";
Print i
Exit For
End If
Next i
End Sub
Function sig(x As Double)
sig = 1/(1 + Exp(-x))
End Function[/syntax]
I'm getting an error on line 51.
Code:
Array not dimensioned, before: '('
il(i).act = sigmoid(sum - il(i).th)
It should have been declared already. I have as a SHARED variable too. Why isn't it recognizing it?
P.S. I have removed the DATA statements. They would've add another 100 or so lines.
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]