vb.net神经网络学习率和动量混乱

时间:2015-07-22 20:50:17

标签: vb.net machine-learning artificial-intelligence neural-network backpropagation

我已将vb6神经网络移植到vb.net 2008.在原始代码中,指定了学习率1.5,甚至没有考虑Momentum。

原始代码可以很快解决XOR问题。

我已经读过,学习率应该在0.3左右,动量在0.9左右,我已经添加到我的代码中了。然而,网络需要太多的时期才能以与原始错误率相同的错误率来解决XOR。如果我为学习率和0动量指定1.5,则它与原始值相同。

我做错了什么?

以下是培训和运行程序的代码:

    Public Function Train(ByVal arrInput As Array, ByVal arrOutput As Array, Optional ByVal boolWrite As Boolean = False, Optional ByVal boolNewLine As Boolean = False) As Boolean
    If (arrInput.GetUpperBound(0) + 1) <> Network.Layers(0).NeuronCount Then 'Check if correct amount of input is given
        Return False
    End If

    If (arrOutput.GetUpperBound(0) + 1) <> Network.Layers(Network.LayerCount - 1).NeuronCount Then 'Check if correct amount of output is given
        Return False
    End If

    Call Run(arrInput, boolWrite, boolNewLine) 'Calculate values of all neurons and set the input

    'Calculate Deltas
    For intOutputNeurons As Integer = 0 To (Network.Layers(Network.LayerCount - 1).NeuronCount - 1) 'Output Layer
        Network.Layers(Network.LayerCount - 1).Neurons(intOutputNeurons).Delta = Network.Layers(Network.LayerCount - 1).Neurons(intOutputNeurons).Value * (1 - Network.Layers(Network.LayerCount - 1).Neurons(intOutputNeurons).Value) * (CInt(arrOutput.GetValue(intOutputNeurons)) - Network.Layers(Network.LayerCount - 1).Neurons(intOutputNeurons).Value)

        For intHiddenLayers As Integer = (Network.LayerCount - 2) To 1 Step -1 'Hidden Layers
            For intHiddenNeurons = 0 To (Network.Layers(intHiddenLayers).NeuronCount - 1)
                Network.Layers(intHiddenLayers).Neurons(intHiddenNeurons).Delta = Network.Layers(intHiddenLayers).Neurons(intHiddenNeurons).Value * (1 - Network.Layers(intHiddenLayers).Neurons(intHiddenNeurons).Value) * Network.Layers(intHiddenLayers + 1).Neurons(intOutputNeurons).Dendrites(intHiddenNeurons).Weight * Network.Layers(intHiddenLayers + 1).Neurons(intOutputNeurons).Delta
            Next intHiddenNeurons
        Next intHiddenLayers
    Next intOutputNeurons

    'Calculate Weights and Biases
    For intLayers = (Network.LayerCount - 1) To 1 Step -1 'Output and Hidden layers
        For intNeurons = 0 To (Network.Layers(intLayers).NeuronCount - 1)
            Dim dblOriginalBias As Double = Network.Layers(intLayers).Neurons(intNeurons).Bias
            Dim dblNewBias As Double = dblOriginalBias + (Network.LearningRate * 1 * Network.Layers(intLayers).Neurons(intNeurons).Delta)

            Dim dblBiasChange As Double = Network.Layers(intLayers).Neurons(intNeurons).BiasChange
            Dim dblBiasMomentum As Double = dblBiasChange * Network.Momentum

            Network.Layers(intLayers).Neurons(intNeurons).Bias = dblNewBias + dblBiasMomentum
            Network.Layers(intLayers).Neurons(intNeurons).BiasChange = dblOriginalBias - Network.Layers(intLayers).Neurons(intNeurons).Bias

            For intDendrites = 0 To (Network.Layers(intLayers).Neurons(intNeurons).DendriteCount - 1)
                Dim dblOriginalWeight As Double = Network.Layers(intLayers).Neurons(intNeurons).Dendrites(intDendrites).Weight
                Dim dblNewWeight As Double = dblOriginalWeight + (Network.LearningRate * Network.Layers(intLayers - 1).Neurons(intDendrites).Value * Network.Layers(intLayers).Neurons(intNeurons).Delta)

                Dim dblWeightChange As Double = Network.Layers(intLayers).Neurons(intNeurons).Dendrites(intDendrites).WeightChange
                Dim dblMomentum As Double = dblWeightChange * Network.Momentum

                Network.Layers(intLayers).Neurons(intNeurons).Dendrites(intDendrites).Weight = dblNewWeight + dblMomentum
                Network.Layers(intLayers).Neurons(intNeurons).Dendrites(intDendrites).WeightChange = dblOriginalWeight - Network.Layers(intLayers).Neurons(intNeurons).Dendrites(intDendrites).Weight
            Next intDendrites
        Next intNeurons
    Next intLayers

    Return True
End Function

    Public Function Run(ByVal arrInput As Array, Optional ByVal boolWrite As Boolean = False, Optional ByVal boolNewLine As Boolean = False) As Array
    Dim arrOutput As Double()

    If (arrInput.GetUpperBound(0) + 1) <> Network.Layers(0).NeuronCount Then
        Return Nothing
    End If

    For intLayers As Integer = 0 To (Network.LayerCount - 1) '0 to 2
        For intNeurons = 0 To Network.Layers(intLayers).NeuronCount - 1
            If intLayers = 0 Then 'Input layer
                Network.Layers(intLayers).Neurons(intNeurons).Value = CInt(arrInput.GetValue(intNeurons)) 'Set the value of input layer

            Else
                Network.Layers(intLayers).Neurons(intNeurons).Value = 0 'First set the value to zero

                For intNeuronsPrevLayer = 0 To Network.Layers(intLayers - 1).NeuronCount - 1
                    Network.Layers(intLayers).Neurons(intNeurons).Value += Network.Layers(intLayers - 1).Neurons(intNeuronsPrevLayer).Value * Network.Layers(intLayers).Neurons(intNeurons).Dendrites(intNeuronsPrevLayer).Weight 'Calculating the value
                Next intNeuronsPrevLayer

                Network.Layers(intLayers).Neurons(intNeurons).Value = Activation(Network.Layers(intLayers).Neurons(intNeurons).Value + Network.Layers(intLayers).Neurons(intNeurons).Bias) 'Calculating the real value of neuron
            End If
        Next intNeurons
    Next intLayers

    ReDim arrOutput(Network.Layers(Network.LayerCount - 1).NeuronCount - 1)

    For intNeuronsOutput = 0 To (Network.Layers(Network.LayerCount - 1).NeuronCount - 1)
        arrOutput(intNeuronsOutput) = Network.Layers(Network.LayerCount - 1).Neurons(intNeuronsOutput).Value 'The array of output result
    Next intNeuronsOutput

    If boolWrite = True Then
        objWriter.Write(CStr(arrOutput.GetValue(0)))

        If boolNewLine = True Then
            objWriter.Write(vbCrLf)
        Else
            objWriter.Write(vbTab)
        End If
    End If

    Return arrOutput
End Function

暂时无法发布图片,但想要了解...

Original(LN = 1.5,M = 0)@ 2000 Epochs:

VS

修改(LN = 0.3,M = 0.9)@ 32000时期:

获得大致相同的错误率。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

你必须坚持VB6而不是vb.net。 VB6编译器现在比C ++更强大。