/// <summary>
        ///     The SSE error with the current weights.
        /// </summary>
        /// <returns></returns>
        private double CalculateError()
        {
            var result = new ErrorCalculation();

            for (int i = 0; i < _trainingLength; i++)
            {
                _pair = _indexableTraining[i];
                IMLData actual = _network.Compute(_pair.Input);
                result.UpdateError(actual, _pair.Ideal, _pair.Significance);
            }

            return result.CalculateSSE();
        }
Пример #2
0
        /// <summary>
        /// Called internally to compute each output neuron.
        /// </summary>
        /// <param name="outputNeuron">The output neuron to compute.</param>
        private void InternalCompute(int outputNeuron)
        {
            int row = 0;
            var error = new ErrorCalculation();
            EngineArray.Fill(derivative, 0);

            // Loop over every training element
            foreach (var pair in training)
            {
                var networkOutput = network.Compute(pair.Input);

                double e = pair.Ideal.Data[outputNeuron] - networkOutput[outputNeuron];
                error.UpdateError(networkOutput[outputNeuron], pair.Ideal[outputNeuron]);

                int currentWeight = 0;

                // loop over the output weights
                int outputFeedCount = network.GetLayerTotalNeuronCount(network.LayerCount - 2);
                for (int i = 0; i < network.OutputCount; i++)
                {
                    for (int j = 0; j < outputFeedCount; j++)
                    {
                        double jc;

                        if (i == outputNeuron)
                        {
                            jc = ComputeDerivative(pair.Input, outputNeuron,
                                                   currentWeight, _dStep,
                                                   networkOutput[outputNeuron], row);
                        }
                        else
                        {
                            jc = 0;
                        }

                        gradients[currentWeight] += jc*e;
                        derivative[currentWeight] += jc;
                        currentWeight++;
                    }
                }

                // Loop over every weight in the neural network
                while (currentWeight < network.Flat.Weights.Length)
                {
                    double jc = ComputeDerivative(
                        pair.Input, outputNeuron, currentWeight,
                        _dStep,
                        networkOutput[outputNeuron], row);
                    derivative[currentWeight] += jc;
                    gradients[currentWeight] += jc*e;
                    currentWeight++;
                }

                row++;
            }

            UpdateHessian(derivative);

            sse += error.CalculateSSE();
        }