Exemplo n.º 1
0
        private void AccumulateHiddenLayer(double[] features, int layerIndex)
        {
            double[]  layerGradient      = Values[layerIndex];
            double[]  layerInput         = layerIndex == 0 ? features : Network.LastEvaluation[layerIndex - 1];
            double[]  layerOutput        = Network.LastEvaluation[layerIndex];
            double[]  nextLayerGradient  = Values[layerIndex + 1];
            Neuron[]  nextLayerNeurons   = Network.Layers[layerIndex + 1].Neurons;
            IFunction activationFunction = Network.Layers[layerIndex].ActivationFunction;

            for (int i = 0; i < layerGradient.Length; i++)
            {
                double error = 0;

                for (int j = 0; j < nextLayerNeurons.Length; j++)
                {
                    error += nextLayerNeurons[j].Weights[i] * nextLayerGradient[j];
                }

                layerGradient[i] = error * activationFunction.Derivative(layerOutput[i]);
                double[] layerAccumulation = Accumulation[layerIndex][i];

                for (int j = 0; j < layerAccumulation.Length - 1; j++)
                {
                    layerAccumulation[j] += layerGradient[i] * layerInput[j];
                }

                layerAccumulation[layerAccumulation.Length - 1] += layerGradient[i];
            }
        }
        public static bool TryToFindRoot(IFunction f, double start, double end, double guess, out double x) {
            ValidateArg.IsNotNull(f, "f");
            System.Diagnostics.Debug.Assert(start >= f.ParStart && end <= f.ParEnd);
            System.Diagnostics.Debug.Assert(start <= guess && end >= guess);
            int numberOfBoundaryCrossings = 0;
            const int maxNumberOfBoundaryCrossings = 10;
            int numberOfTotalReps = 0;
            const int maxNumberOfTotalReps = 100;
            x = guess;

            double dx;
            bool abort = false;
            do {

                var fp = f.Derivative(x);
                if (Math.Abs(fp) < ApproximateComparer.Tolerance) {
                    abort = true;
                    break;
                }

                dx = -f[x] / fp;
                x += dx;
                if (x < start - ApproximateComparer.DistanceEpsilon) {
                    x = start;
                    numberOfBoundaryCrossings++;
                } else if (x > end + ApproximateComparer.DistanceEpsilon) {
                    x = end;
                    numberOfBoundaryCrossings++;
                }

                numberOfTotalReps++;

                abort = numberOfBoundaryCrossings >= maxNumberOfBoundaryCrossings ||
                  numberOfTotalReps >= maxNumberOfTotalReps || dx == 0;

            } while (Math.Abs(dx) >= ApproximateComparer.Tolerance && !abort);

            if (abort) {
                //may be the initial guess was just OK
                if (Math.Abs(f[guess]) < ApproximateComparer.DistanceEpsilon) {
                    x = guess;
                    return true;
                }
                return false;
            } 
            if (x < start) 
                x = start;
            else if (x > end )
                x = end;
            
            return true;

        }
Exemplo n.º 3
0
        protected override void DoActivationDx(IFunction function, Tensor dy, Tensor dx)
        {
            var sizePerBatch = Size / Batch;

            Parallel.For(0, Batch, b =>
            {
                var start = sizePerBatch * b;
                var end   = sizePerBatch * b + sizePerBatch;
                for (int i = start; i < end; i++)
                {
                    dx[i] = function.Derivative(this[i]) * dy[i];
                }
            });
        }
Exemplo n.º 4
0
        private static bool TryGetExitPoint(
            IFunction function,
            Transform transform,
            float x,
            float y,
            bool isAsymptote,
            out Vector2 point)
        {
            point = new Vector2(x, y);
            int derivativeSign = isAsymptote ? Math.Sign(function.Derivative(x)) : 0;

            // Check if we are leaving the graph. If so, find the intersection the edge
            // of the graph so we can have nice clean edges.
            if (y > transform.Top)
            {
                if (isAsymptote)
                {
                    if (derivativeSign > 0)
                    {
                        point = new Vector2(x, transform.Top);
                    }
                    else
                    {
                        point = new Vector2(x, transform.Bottom);
                    }
                }

                return(true);
            }

            if (y < transform.Bottom)
            {
                if (isAsymptote)
                {
                    if (derivativeSign > 0)
                    {
                        point = new Vector2(x, transform.Top);
                    }
                    else
                    {
                        point = new Vector2(x, transform.Bottom);
                    }
                }

                return(true);
            }

            return(false);
        }
Exemplo n.º 5
0
        private void AccumulateOutputLayer(double[] features, double[] output, double[] labels)
        {
            double[]  layerGradient      = Values[Values.Length - 1];
            double[]  layerInput         = Network.LastEvaluation[Values.Length - 2];
            IFunction activationFunction = Network.Layers.Last().ActivationFunction;

            for (int i = 0; i < layerGradient.Length; i++)
            {
                double error = -(labels[i] - output[i]);
                layerGradient[i] = error * activationFunction.Derivative(output[i]);

                double[] layerGradient2 = Accumulation[Values.Length - 1][i];

                for (int j = 0; j < layerGradient2.Length - 1; j++)
                {
                    layerGradient2[j] = layerGradient[i] * layerInput[j];
                }

                layerGradient2[layerGradient2.Length - 1] += layerGradient[i];
            }
        }
Exemplo n.º 6
0
        public double Solve(object[] args, IFunction f, out double iter)
        {
            double x0   = (double)args[0];
            double step = (double)args[1];
            double eps1 = (double)args[2];
            double eps2 = (double)args[3];

            iter = 0;
            if (x0 > 0)
            {
                return(double.NaN);
            }

            if (step <= 0)
            {
                return(double.NaN);
            }

            //Шаг 1 + 2.1
            List <double> x = new List <double>();

            x.Add(x0);
            int k = 0;

            do
            {
                if (f.Derivative(x[x.Count - 1]) < 0)
                {
                    x.Add(x[x.Count - 1] + Math.Pow(2, k) * step);
                }

                if (f.Derivative(x[x.Count - 1]) > 0)
                {
                    x.Add(x[x.Count - 1] - Math.Pow(2, k) * step);
                }

                k++;
            }while (f.Derivative(x[x.Count - 2]) * f.Derivative(x[x.Count - 1]) >= 0);

            //Шаг 2.2
            double x1 = x[x.Count - 2];
            double x2 = x[x.Count - 1];

            //Шаг 2.3
            double f1   = f.Func(x1);
            double f2   = f.Func(x2);
            double f1_d = f.Derivative(x1);
            double f2_d = f.Derivative(x2);

            double z = (3 * (f1 - f2)) / (x2 - x1) + f1_d + f2_d;

            double w = double.NaN;

            if (x1 < x2)
            {
                w = Math.Pow(Math.Pow(z, 2) - f1_d * f2_d, 0.5);
            }
            if (x1 > x2)
            {
                w = -Math.Pow(Math.Pow(z, 2) - f1_d * f2_d, 0.5);
            }

            double m = (f2_d + w - z) / (f2_d - f1_d + 2 * w);

            //Шаг 3
            double x_stationary = double.NaN;

            if (m < 0)
            {
                x_stationary = x2;
            }
            if (0 <= m && m <= 1)
            {
                x_stationary = x2 - m * (x2 - x1);
            }
            if (m > 1)
            {
                x_stationary = x2;
            }

            //Шаг 4
            while (f.Func(x_stationary) > f.Func(x1))
            {
                x_stationary = x_stationary + 0.5 * (x_stationary - x1);
            }

            //Шаг 5
            for (int c = 0; c < 500; c++)
            {
                if (f.Derivative(x_stationary) <= eps1 && Math.Abs((x_stationary - x1) / x_stationary) <= eps2)
                {
                    return(x_stationary);
                }

                if (f.Derivative(x_stationary) * f.Derivative(x1) < 0)
                {
                    x2 = x1;
                    x1 = x_stationary;
                }

                if (f.Derivative(x_stationary) * f.Derivative(x2) < 0)
                {
                    x1 = x_stationary;
                }

                //Переход на шаг 3
                x_stationary = double.NaN;
                if (m < 0)
                {
                    x_stationary = x2;
                }
                if (0 <= m && m <= 1)
                {
                    x_stationary = x2 - m * (x2 - x1);
                }
                if (m > 1)
                {
                    x_stationary = x2;
                }
                iter++;
            }

            return(x_stationary);
        }
Exemplo n.º 7
0
        public static bool TryToFindRoot(IFunction f, double start, double end, double guess, out double x)
        {
            ValidateArg.IsNotNull(f, "f");
            System.Diagnostics.Debug.Assert(start >= f.ParStart && end <= f.ParEnd);
            System.Diagnostics.Debug.Assert(start <= guess && end >= guess);
            int       numberOfBoundaryCrossings    = 0;
            const int maxNumberOfBoundaryCrossings = 10;
            int       numberOfTotalReps            = 0;
            const int maxNumberOfTotalReps         = 100;

            x = guess;

            double dx;
            bool   abort = false;

            do
            {
                var fp = f.Derivative(x);
                if (Math.Abs(fp) < ApproximateComparer.Tolerance)
                {
                    abort = true;
                    break;
                }

                dx = -f[x] / fp;
                x += dx;
                if (x < start - ApproximateComparer.DistanceEpsilon)
                {
                    x = start;
                    numberOfBoundaryCrossings++;
                }
                else if (x > end + ApproximateComparer.DistanceEpsilon)
                {
                    x = end;
                    numberOfBoundaryCrossings++;
                }

                numberOfTotalReps++;

                abort = numberOfBoundaryCrossings >= maxNumberOfBoundaryCrossings ||
                        numberOfTotalReps >= maxNumberOfTotalReps || dx == 0;
            } while (Math.Abs(dx) >= ApproximateComparer.Tolerance && !abort);

            if (abort)
            {
                //may be the initial guess was just OK
                if (Math.Abs(f[guess]) < ApproximateComparer.DistanceEpsilon)
                {
                    x = guess;
                    return(true);
                }
                return(false);
            }
            if (x < start)
            {
                x = start;
            }
            else if (x > end)
            {
                x = end;
            }

            return(true);
        }
Exemplo n.º 8
0
 private static Func <double, double> GetDistanceFunction(IFunction function, double x1, double y1)
 {
     return((x) => 2 * (-x1 + (function.Function(x) - y1) * function.Derivative(x) + x));
 }