我试图实现一个神经网络来分析来自MNIST数据集的条目(这是一个七)。在多个时期发生反向传播之后,我继续将1和2交替作为输出。我怀疑这是我自制的sigmoid功能的问题,但我不确定。如果有人有任何改进和/或修复代码的建议,那就太好了。代码如下:
Module Module1
Dim lr As Single
Dim final_outputs(1, 1) As Single
Dim inodes As Single
Dim hnodes As New Single
Dim onodes As New Single
Dim ihweight(1, 1) As Double
Dim howeight(1, 1) As Double
Dim inputs_grid(1, 1) As Double
Dim targets_grid(1, 1) As Double
Dim text_numbers(1000) As String
Dim specify As Short
Dim scaled_inputs(0, 784) As Decimal
Class neural
Sub init(inputnodes As Single, hiddennodes As Single, outputnodes As Single, learningrate As Single)
inodes = inputnodes
hnodes = hiddennodes
onodes = outputnodes
lr = learningrate
End Sub
Sub def_matrices()
Dim x, y As Integer
Dim random As New random
For x = 0 To (inodes - 1)
For y = 0 To (hnodes - 1)
ihweight(x, y) = random.Next(-50, 50) / 500
Next
Next
For x = 0 To (hnodes - 1)
For y = 0 To (onodes - 1)
howeight(x, y) = random.Next(-50, 50) / 500
Next
Next
End Sub
Sub targets()
For i = 0 To (onodes - 1)
targets_grid(0, i) = 0
Next
targets_grid(0, specify) = 0.99
For i = 0 To (onodes - 1)
Console.WriteLine(targets_grid(0, i))
Next
End Sub
Sub train()
Dim i, j As Integer
Dim temp As Single
Dim hidden_inputs(0, hnodes) As Single
Dim hidden_outputs(0, hnodes) As Single
Dim final_inputs(0, onodes) As Single
Dim final_outputs(0, onodes) As Single
Dim output_errors(0, onodes) As Single
Dim hidden_errors(0, hnodes) As Single
Dim transpose(inodes, 0) As Single
Dim final_calc(0, 0) As Single
For i = 0 To (hnodes - 1)
For j = 0 To (inodes - 1)
temp = scaled_inputs(0, j) * ihweight(j, i)
hidden_inputs(0, i) = hidden_inputs(0, i) + temp
Next
Next
For i = 0 To (hnodes - 1)
hidden_outputs(0, i) = sigmoid(hidden_inputs(0, i))
Next
For i = 0 To (onodes - 1)
For j = 0 To (hnodes - 1)
temp = hidden_outputs(0, j) * howeight(j, i)
final_inputs(0, i) = final_inputs(0, i) + temp
Next
Next
For i = 0 To (onodes - 1)
final_outputs(0, i) = sigmoid(final_inputs(0, i))
Next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 0 To (onodes - 1)
output_errors(0, i) = targets_grid(0, i) - final_outputs(0, i)
Next
Dim temp_matrix(inodes, hnodes)
For i = 0 To (hnodes - 1)
For j = 0 To (onodes - 1)
howeight(i, j) = temp_matrix(j, i)
Next
Next
For i = 0 To (onodes - 1)
For j = 0 To (hnodes - 1)
temp = output_errors(0, i) * temp_matrix(i, j)
hidden_errors(0, i) = hidden_errors(0, i) + temp
Next
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim temp_calc(0, hnodes)
For i = 0 To (onodes - 1)
temp = final_outputs(0, i) * (1 - final_outputs(0, i))
temp_calc(0, i) = temp
Next
For i = 0 To (onodes - 1)
temp = output_errors(0, i) * temp_calc(0, i)
temp_calc(0, i) = temp
Next
For i = 0 To (hnodes - 1)
transpose(i, 0) = hidden_outputs(0, i)
Next
For i = 0 To (onodes - 1)
For j = 0 To (hnodes - 1)
temp = transpose(j, 0) * temp_calc(0, i)
temp_matrix(j, i) = temp
Next
Next
For i = 0 To (onodes - 1)
For j = 0 To (hnodes - 1)
howeight(j, i) = howeight(j, i) + (lr * temp_matrix(j, i))
Next
Next
''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 0 To (hnodes - 1)
temp = hidden_outputs(0, i) * (1 - hidden_outputs(0, i))
temp_calc(0, i) = temp
Next
For i = 0 To (hnodes - 1)
temp = hidden_errors(0, i) * temp_calc(0, i)
temp_calc(0, i) = temp
Next
For i = 0 To (inodes - 1)
transpose(i, 0) = scaled_inputs(0, i)
Next
For i = 0 To (hnodes - 1)
For j = 0 To (inodes - 1)
temp = transpose(j, 0) * temp_calc(0, i)
temp_matrix(j, i) = temp
Next
Next
For i = 0 To (hnodes - 1)
For j = 0 To (inodes - 1)
ihweight(j, i) = ihweight(j, i) + (lr * temp_matrix(j, i))
Next
Next
End Sub
Sub query()
Dim i, j As Integer
Dim temp As Single
Dim hidden_inputs(0, hnodes) As Single
Dim hidden_outputs(0, hnodes) As Single
Dim final_inputs(0, onodes) As Single
Dim final_outputs(0, onodes) As Single
For i = 0 To (hnodes - 1)
For j = 0 To (inodes - 1)
temp = scaled_inputs(0, j) * ihweight(j, i)
hidden_inputs(0, i) = hidden_inputs(0, i) + temp
Next
Next
For i = 0 To (hnodes - 1)
hidden_outputs(0, i) = sigmoid(hidden_inputs(0, i))
Next
For i = 0 To (onodes - 1)
For j = 0 To (hnodes - 1)
temp = hidden_outputs(0, j) * howeight(j, i)
final_inputs(0, i) = final_inputs(0, i) + temp
Next
Next
For i = 0 To (onodes - 1)
final_outputs(0, i) = sigmoid(final_inputs(0, i))
Next
Console.WriteLine(" ")
For i = 0 To (onodes - 1)
Console.WriteLine(final_outputs(0, i))
Next
Console.ReadKey()
End Sub
Function sigmoid(inputs As Single)
Dim finish As New Single
finish = 1 / 1 + 2.71828183 ^ (-inputs)
Return finish
End Function
End Class
Sub Main()
Dim neural As New neural
Dim text_1d(1000) As String
neural.init(784, 200, 10, 0.1)
ReDim ihweight(inodes, hnodes)
ReDim howeight(hnodes, onodes)
ReDim inputs_grid(0, inodes)
ReDim targets_grid(0, onodes)
ReDim final_outputs(0, onodes)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Dim text As New List(Of String)
'Dim fileToLoad As New System.IO.StreamReader("C:\Users\lopes_000\Desktop\mnist_test_10.txt")
'While Not fileToLoad.EndOfStream
' text.Add(fileToLoad.ReadAllLines)
'End While
'fileToLoad.Close()
Dim text As String
text = "7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,84,185,159,151,60,36,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,222,254,254,254,254,241,198,198,198,198,198,198,198,198,170,52,0,0,0,0,0,0,0,0,0,0,0,0,67,114,72,114,163,227,254,225,254,254,254,250,229,254,254,140,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,66,14,67,67,67,59,21,236,254,106,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,83,253,209,18,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22,233,255,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,129,254,238,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,59,249,254,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,133,254,187,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,205,248,58,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,126,254,182,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,75,251,240,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,19,221,254,166,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,203,254,219,35,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,38,254,254,77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,31,224,254,115,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,133,254,254,52,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,61,242,254,254,52,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,121,254,254,219,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,121,254,207,18,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
text_1d = text.Split(","c)
Dim text_numbers(784) As Short
For i = 0 To 784
text_numbers(i) = CShort(text_1d(i))
Next
specify = text_numbers(0)
For i = 1 To 784
scaled_inputs(0, i) = (text_numbers(i) / 225.0 * 0.99) + 0.01
Next
For i = 0 To 783
scaled_inputs(0, i) = scaled_inputs(0, i + 1)
Next
Dim counter As Integer = 0
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
neural.def_matrices()
neural.targets()
For i = 0 To 99
neural.train()
neural.query()
Next
Dim number As Integer
For i = 0 To 9
If final_outputs(0, i) > counter Then
counter = final_outputs(0, i)
number = i
End If
Next
Console.WriteLine(number)
Console.ReadKey()
End Sub
End Module
答案 0 :(得分:0)
如您所知,您的S形函数(特别是操作顺序)似乎存在问题
Function sigmoid(inputs As Single)
Dim finish As New Single
finish = 1 / 1 + 2.71828183 ^ (-inputs)
return finish
End Function
您的结束变量计算为
finish = 1 + 2.71828183 ^ (-inputs)
添加一些括号
finish = 1 / (1 + 2.71828183 ^ (-inputs))
你的sigmoid功能应该有效。