Ejemplo n.º 1
0
        /// <summary>
        ///     Process one training set element.
        /// </summary>
        /// <param name="errorCalc">The error calculation to use.</param>
        /// <param name="input">The network input.</param>
        /// <param name="ideal">The ideal values.</param>
        public void Process(IErrorCalculation errorCalc, double[] input, double[] ideal)
        {
            _network.Compute(input, _actual);

            errorCalc.UpdateError(_actual, ideal, 1.0);

            // Calculate error for the output layer.
            var outputLayerIndex = _network.Layers.Count - 1;
            var outputActivation = _network.Layers[outputLayerIndex].Activation;

            errorFunction.CalculateError(
                outputActivation, _layerSums, _layerOutput,
                ideal, _actual, _layerDelta, 0, 1.0);

            // Apply regularization, if requested.
            if (_owner.L1 > AIFH.DefaultPrecision ||
                _owner.L1 > AIFH.DefaultPrecision)
            {
                var lp = new double[2];
                CalculateRegularizationPenalty(lp);
                for (var i = 0; i < _actual.Length; i++)
                {
                    var p = lp[0] * _owner.L1 + lp[1] * _owner.L2;
                    _layerDelta[i] += p;
                }
            }

            // Propagate backwards (chain rule from calculus).
            for (var i = _network.Layers.Count - 1; i > 0; i--)
            {
                var layer = _network.Layers[i];
                layer.ComputeGradient(this);
            }
        }
Ejemplo n.º 2
0
        public static double CalculateError(IErrorCalculation calc, double[][] actual, double[][] ideal)
        {

            // First we are going to calculate by passing in 1d arrays to
            // the error calculation.  This is the most common case.

            calc.Clear();

            Assert.AreEqual(double.PositiveInfinity, calc.Calculate(), 0.0001);

            for (int i = 0; i < actual.Length; i++)
            {
                double[] actualData = actual[i];
                double[] idealData = ideal[i];
                calc.UpdateError(actualData, idealData, 1.0);
            }
            Assert.AreEqual(20, calc.SetSize);
            double error1 = calc.Calculate();

            // Secondly we are going to calculate by passing individual
            // elements.  This is less common, but the error calculation
            // should result in the same as above.

            calc.Clear();

            Assert.AreEqual(double.PositiveInfinity, calc.Calculate(), 0.0001);

            for (int i = 0; i < actual.Length; i++)
            {
                double[] actualData = actual[i];
                double[] idealData = ideal[i];
                for (int j = 0; j < actualData.Length; j++)
                {
                    calc.UpdateError(actualData[j], idealData[j]);
                }
            }
            Assert.AreEqual(20, calc.SetSize);
            double error2 = calc.Calculate();

            // these two should always equal
            Assert.AreEqual(error1, error2, 0.0001);


            return error2;
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Train.  Single iteration.
        /// </summary>
        public void Iteration()
        {
            int rowCount      = _trainingData.Count;
            int inputColCount = _trainingData[0].Input.Length;

            Matrix <double> xMatrix = new DenseMatrix(rowCount, inputColCount + 1);
            Matrix <double> yMatrix = new DenseMatrix(rowCount, 1);

            for (int row = 0; row < _trainingData.Count; row++)
            {
                BasicData dataRow = _trainingData[row];
                int       colSize = dataRow.Input.Count();

                xMatrix[row, 0] = 1;
                for (int col = 0; col < colSize; col++)
                {
                    xMatrix[row, col + 1] = dataRow.Input[col];
                }
                yMatrix[row, 0] = dataRow.Ideal[0];
            }

            // Calculate the least squares solution
            QR qr = xMatrix.QR();
            Matrix <double> beta = qr.Solve(yMatrix);

            double sum = 0.0;

            for (int i = 0; i < inputColCount; i++)
            {
                sum += yMatrix[i, 0];
            }
            double mean = sum / inputColCount;

            for (int i = 0; i < inputColCount; i++)
            {
                double dev = yMatrix[i, 0] - mean;
                _sst += dev * dev;
            }

            Matrix <double> residuals = xMatrix.Multiply(beta).Subtract(yMatrix);

            _sse = residuals.L2Norm() * residuals.L2Norm();

            for (int i = 0; i < _algorithm.LongTermMemory.Length; i++)
            {
                _algorithm.LongTermMemory[i] = beta[i, 0];
            }

            // calculate error
            _errorCalculation.Clear();
            foreach (BasicData dataRow in _trainingData)
            {
                double[] output = _algorithm.ComputeRegression(dataRow.Input);
                _errorCalculation.UpdateError(output, dataRow.Ideal, 1.0);
            }
            _error = _errorCalculation.Calculate();
        }
Ejemplo n.º 4
0
        public static double CalculateError(IErrorCalculation calc, double[][] actual, double[][] ideal)
        {
            // First we are going to calculate by passing in 1d arrays to
            // the error calculation.  This is the most common case.

            calc.Clear();

            Assert.AreEqual(double.PositiveInfinity, calc.Calculate(), 0.0001);

            for (int i = 0; i < actual.Length; i++)
            {
                double[] actualData = actual[i];
                double[] idealData  = ideal[i];
                calc.UpdateError(actualData, idealData, 1.0);
            }
            Assert.AreEqual(20, calc.SetSize);
            double error1 = calc.Calculate();

            // Secondly we are going to calculate by passing individual
            // elements.  This is less common, but the error calculation
            // should result in the same as above.

            calc.Clear();

            Assert.AreEqual(double.PositiveInfinity, calc.Calculate(), 0.0001);

            for (int i = 0; i < actual.Length; i++)
            {
                double[] actualData = actual[i];
                double[] idealData  = ideal[i];
                for (int j = 0; j < actualData.Length; j++)
                {
                    calc.UpdateError(actualData[j], idealData[j]);
                }
            }
            Assert.AreEqual(20, calc.SetSize);
            double error2 = calc.Calculate();

            // these two should always equal
            Assert.AreEqual(error1, error2, 0.0001);


            return(error2);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Calculate the error with the specified error calculation.
        /// </summary>
        /// <param name="calc">The error calculation.</param>
        /// <returns>The error.</returns>
        public double CalculateError(IErrorCalculation calc)
        {
            calc.Clear();

            for (int row = 0; row < Actual.Length; row++)
            {
                calc.UpdateError(Actual[row], Ideal[row], 1.0);
            }

            return calc.Calculate();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Calculate the error with the specified error calculation.
        /// </summary>
        /// <param name="calc">The error calculation.</param>
        /// <returns>The error.</returns>
        public double CalculateError(IErrorCalculation calc)
        {
            calc.Clear();

            for (int row = 0; row < Actual.Length; row++)
            {
                calc.UpdateError(Actual[row], Ideal[row], 1.0);
            }

            return(calc.Calculate());
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Calculate error for regression.
        /// </summary>
        /// <param name="dataset">The dataset.</param>
        /// <param name="model">The model to evaluate.</param>
        /// <param name="calc">The error calculation.</param>
        /// <returns>The error.</returns>
        public static double CalculateRegressionError(IList <BasicData> dataset,
                                                      IRegressionAlgorithm model,
                                                      IErrorCalculation calc)
        {
            calc.Clear();
            foreach (var item in dataset)
            {
                var output = model.ComputeRegression(item.Input);
                calc.UpdateError(output, item.Ideal, 1.0);
            }

            return(calc.Calculate());
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public double CalculateScore(IMLMethod algo)
        {
            var ralgo = (IRegressionAlgorithm)algo;

            // evaulate
            _errorCalc.Clear();
            foreach (var pair in _trainingData)
            {
                var output = ralgo.ComputeRegression(pair.Input);
                _errorCalc.UpdateError(output, pair.Ideal, 1.0);
            }

            return(_errorCalc.Calculate());
        }
Ejemplo n.º 9
0
        /// <inheritdoc/>
        public double CalculateScore(IMLMethod algo)
        {
            IErrorCalculation ec = ErrorCalc.Create();

            IRegressionAlgorithm ralgo = (IRegressionAlgorithm)algo;

            // evaulate
            ec.Clear();
            foreach (BasicData pair in _trainingData)
            {
                double[] output = ralgo.ComputeRegression(pair.Input);
                ec.UpdateError(output, pair.Ideal, 1.0);
            }

            return(ec.Calculate());
        }
Ejemplo n.º 10
0
        /// <inheritdoc />
        public double CalculateScore(IMLMethod algo)
        {
            IErrorCalculation ec = ErrorCalc.Create();

            var ralgo  = (IRegressionAlgorithm)algo;
            var genome = (IGenome)ralgo;

            if (genome.Count > _maxLength)
            {
                return(double.PositiveInfinity);
            }

            // evaulate
            ec.Clear();
            foreach (BasicData pair in _trainingData)
            {
                double[] output = ralgo.ComputeRegression(pair.Input);
                ec.UpdateError(output, pair.Ideal, 1.0);
            }

            return(ec.Calculate());
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Process one training set element.
        /// </summary>
        /// <param name="errorCalc">The error calculation to use.</param>
        /// <param name="input">The network input.</param>
        /// <param name="ideal">The ideal values.</param>
        public void Process(IErrorCalculation errorCalc, double[] input, double[] ideal)
        {
            _network.Compute(input, _actual);

            errorCalc.UpdateError(_actual, ideal, 1.0);

            // Calculate error for the output layer.
            var outputLayerIndex = _network.Layers.Count - 1;
            var outputActivation = _network.Layers[outputLayerIndex].Activation;
            errorFunction.CalculateError(
                outputActivation, _layerSums, _layerOutput,
                ideal, _actual, _layerDelta, 0, 1.0);

            // Apply regularization, if requested.
            if (_owner.L1 > AIFH.DefaultPrecision
                || _owner.L1 > AIFH.DefaultPrecision)
            {
                var lp = new double[2];
                CalculateRegularizationPenalty(lp);
                for (var i = 0; i < _actual.Length; i++)
                {
                    var p = lp[0]*_owner.L1 + lp[1]*_owner.L2;
                    _layerDelta[i] += p;
                }
            }

            // Propagate backwards (chain rule from calculus).
            for (var i = _network.Layers.Count - 1; i > 0; i--)
            {
                var layer = _network.Layers[i];
                layer.ComputeGradient(this);
            }
        }