Exemple #1
0
        private double GetSquaredError(double[] expectedOutput)
        {
            double error     = 0.0;
            Layer  lastLayer = this.net[net.LayersCount - 1];

            double[]            currError = errors[net.LayersCount - 1];
            int                 limit     = net[net.LayersCount - 1].Size;
            IActivationFunction func      = net[0][0].ActivationFunction;

            for (int i = 0; i < limit; i++)
            {
                double err = expectedOutput[i] - lastLayer[i].Output;
                currError[i] = err * func.DerivativeFromY(lastLayer[i].Output);
                error       += err * err;
            }

            for (int i = net.LayersCount - 2; i >= 0; i--)
            {
                lastLayer = net[i];
                currError = errors[i];
                Layer    nextLayer = net[i + 1];
                double[] nextError = errors[i + 1];

                for (int j = 0; j < lastLayer.Size; j++)
                {
                    double sum = 0.0;
                    for (int k = 0; k < nextLayer.Size; k++)
                    {
                        sum += nextError[k] * nextLayer[k].Weights[j];
                    }
                    currError[j] = sum * func.DerivativeFromY(lastLayer[j].Output);
                }
            }
            return(error);
        }