예제 #1
0
    private void Backpropagation(List <float> wantedOutputs)
    {
        for (int i = 0; i < m_outputPerceptrons.Count; i++)
        {
            Perceptron perceptron = m_outputPerceptrons[i];
            float      state      = perceptron.State;

            float error = state * (1 - state) * (wantedOutputs[i] - state);
            perceptron.AdjustWeight(error);
        }

        for (int layerIndex = m_hiddenLayers.Count - 1; layerIndex >= 0; layerIndex--)
        {
            List <Perceptron> perceptronList = m_hiddenLayers[layerIndex].hiddenPerceptrons;

            for (int i = 0; i < perceptronList.Count; i++)
            {
                Perceptron perceptron = perceptronList[i];
                float      state      = perceptron.State;

                List <Perceptron> nextList;

                if (layerIndex < m_hiddenLayers.Count - 1)
                {
                    nextList = m_hiddenLayers[layerIndex + 1].hiddenPerceptrons;
                }
                else
                {
                    nextList = m_outputPerceptrons;
                }

                float sum = 0;
                for (int j = 0; j < nextList.Count; j++)
                {
                    sum += nextList[j].GetIncomingWeight(perceptron) * nextList[j].Error;
                }

                float error = state * (1 - state) * sum;
                perceptron.AdjustWeight(error);
            }
        }
    }