Esempio n. 1
0
        /// <summary>
        /// Prepare the left side and determine the cellshape.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        private CalculationArguments PrepareVariables(AType left, AType right)
        {
            // Error if the arguments:
            //  - are boxes
            //  - or not of the same type and:
            //    - not numbers
            //    - or one of them is a Null
            if (left.IsBox || right.IsBox ||
                (left.Type != right.Type &&
                 !(Utils.DifferentNumberType(left, right) || left.Type == ATypes.ANull || right.Type == ATypes.ANull)
                ))
            {
                throw new Error.Type(TypeErrorText);
            }

            CalculationArguments arguments = new CalculationArguments()
            {
                Interval  = left,
                CellShape = (left.Rank > 1 && left.Length > 0) ? left[0].Shape : new List <int>()
            };

            if (right.Rank < arguments.CellShape.Count)
            {
                throw new Error.Rank(RankErrorText);
            }

            return(arguments);
        }
Esempio n. 2
0
        /// <summary>
        /// Prepare the left side and determine the cellshape.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        private CalculationArguments PrepareVariables(AType left, AType right)
        {
            // Error if the arguments:
            //  - are boxes
            //  - or not of the same type and:
            //    - not numbers
            //    - or one of them is a Null
            if (left.IsBox || right.IsBox ||
                (left.Type != right.Type &&
                !(Utils.DifferentNumberType(left, right) || left.Type == ATypes.ANull || right.Type == ATypes.ANull)
            ))
            {
                throw new Error.Type(TypeErrorText);
            }

            CalculationArguments arguments = new CalculationArguments()
            {
                Interval = left,
                CellShape = (left.Rank > 1 && left.Length > 0) ? left[0].Shape : new List<int>()
            };

            if (right.Rank < arguments.CellShape.Count)
            {
                throw new Error.Rank(RankErrorText);
            }

            return arguments;
        }
Esempio n. 3
0
        public void Learn(
            SampleList samples, CalculationArguments arguments)
        // samples = yjks
        {
            arguments.reporter?.WriteStart($"Learning the network using a subset of {samples.Count} random samples...");
            Stopwatch timer = new Stopwatch();

            timer.Start();

            int nSamples      = samples.Count; // number of sample rows
            int nCoefficients = CoefficientCount();
            // Current biasses and weights of the neurons in this network:
            Single1D coefficients = new Single1D(nCoefficients);
            // The derivatives of the cost with respect to the biasses and weights:
            Single1D derivatives = new Single1D(nCoefficients);
            Single1D velocities  = new Single1D(nCoefficients);

            velocities.Clear();
            MeasurementList measurements = new MeasurementList(nSamples, Last.Count);

            GetCoefficients(coefficients, 0);
            Minimization minimization = new Minimization()
            {
                MaxIter = arguments.settings.MaxIter,
                Eps     = arguments.settings.Epsilon,
                Tol     = arguments.settings.Tolerance,
            };
            float finalCost = minimization.MomentumBasedGradientDescent(coefficients, derivatives, velocities,
                                                                        (iter) =>
            {
                SetCoefficients(coefficients, 0);
                arguments.reporter?.ReportCoefficients(coefficients);
                float cost = GetCostAndDerivatives(samples, derivatives, measurements, arguments);
                arguments.reporter?.ReportCostAndDerivatives(cost, derivatives, measurements);
                return(cost);
            }, arguments.settings.LearningRate, arguments.settings.MomentumCoefficient);

            arguments.reporter?.WriteEnd($"The network has learned in {timer.Elapsed.TotalSeconds} s, and the final cost value is {finalCost:F4}.");
        }
Esempio n. 4
0
        // ----------------------------------------------------------------------------------------
        #region Network

        public float GetCostAndDerivatives(
            SampleList samples, Single1D derivatives, MeasurementList measurements,
            CalculationArguments arguments)
        {
            CostFunctionEnum costFunction = arguments.settings.CostFunction;
            float            lambda       = arguments.settings.Lambda;
            int   nSamples = samples.Count;
            int   nCoeffs  = derivatives.Count;
            float cost     = 0f;
            Layer last     = Last;

            for (int i = 0; i < nCoeffs; i++)
            {
                derivatives[i] = 0f;
            }
            for (int iSample = 0; iSample < nSamples; iSample++)
            {
                arguments.ThrowIfCancellationRequested();
                Sample   sample      = samples[iSample];
                Single1D measurement = measurements[iSample];
                Input.SetActivations(sample.Inputs, 0);
                FeedForward(true);
                last.GetActivations(measurement, 0);
                cost += CostFunction(measurement, sample.Requirements, costFunction);
                int weightCount = CountWeight();
                cost += 0.5f * lambda * SumWeightSqr() / weightCount; // regularization
                last.CalculateDeltas(sample.Requirements, costFunction);
                FeedBackward(true);
                AddDerivatives(derivatives, 0, lambda / weightCount);
                arguments.reporter?.ReportProgress(iSample, nSamples);
            }
            arguments.reporter?.ReportProgress(0, nSamples);
            cost /= nSamples;
            for (int i = 0; i < nCoeffs; i++)
            {
                derivatives[i] /= nSamples;
            }
            return(cost);
        }
Esempio n. 5
0
        /// <summary>
        /// Classify element to the corresponding group.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="arguments">Instead of class variables.</param>
        /// <returns></returns>
        private AType Classify(AType element, CalculationArguments arguments)
        {
            int index;

            if (arguments.Interval.IsArray)
            {
                AType intervalArray = arguments.Interval;

                for (index = 0; index < intervalArray.Length; index++)
                {
                    if (element.CompareTo(intervalArray[index]) <= 0)
                    {
                        break;
                    }
                }
            }
            else
            {
                index = (element.CompareTo(arguments.Interval) <= 0) ? 0 : arguments.Interval.Length;
            }

            return(AInteger.Create(index));
        }
Esempio n. 6
0
        /// <summary>
        /// If the cell rank > 1, first step is check the shape of actual cell equal to determined cell shape.
        /// If different we throw Length error. The second step is classify the cell.
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="arguments">Instead of class variables.</param>
        /// <returns></returns>
        private AType MultipleItemsWalking(AType cell, CalculationArguments arguments)
        {
            if (arguments.CellShape.Count == cell.Shape.Count)
            {
                if (!arguments.CellShape.SequenceEqual(cell.Shape))
                {
                    throw new Error.Length(LengthErrorText);
                }

                return(Classify(cell, arguments));
            }
            else
            {
                AType result = AArray.Create(ATypes.AInteger);

                foreach (AType item in cell)
                {
                    result.AddWithNoUpdate(MultipleItemsWalking(item, arguments));
                }
                result.UpdateInfo();

                return(result);
            }
        }
Esempio n. 7
0
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            CalculationArguments arguments = PrepareVariables(left, right);

            return(MultipleItemsWalking(right, arguments));
        }
Esempio n. 8
0
        /// <summary>
        /// Classify element to the corresponding group.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="arguments">Instead of class variables.</param>
        /// <returns></returns>
        private AType Classify(AType element, CalculationArguments arguments)
        {
            int index;

            if (arguments.Interval.IsArray)
            {
                AType intervalArray = arguments.Interval;

                for (index = 0; index < intervalArray.Length; index++)
                {
                    if (element.CompareTo(intervalArray[index]) <= 0)
                    {
                        break;
                    }
                }
            }
            else
            {
                index = (element.CompareTo(arguments.Interval) <= 0) ? 0 : arguments.Interval.Length;
            }

            return AInteger.Create(index);
        }
Esempio n. 9
0
        /// <summary>
        /// If the cell rank > 1, first step is check the shape of actual cell equal to determined cell shape.
        /// If different we throw Length error. The second step is classify the cell.
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="arguments">Instead of class variables.</param>
        /// <returns></returns>
        private AType MultipleItemsWalking(AType cell, CalculationArguments arguments)
        {
            if (arguments.CellShape.Count == cell.Shape.Count)
            {
                if (!arguments.CellShape.SequenceEqual(cell.Shape))
                {
                    throw new Error.Length(LengthErrorText);
                }

                return Classify(cell, arguments);
            }
            else
            {
                AType result = AArray.Create(ATypes.AInteger);

                foreach (AType item in cell)
                {
                    result.AddWithNoUpdate(MultipleItemsWalking(item, arguments));
                }
                result.UpdateInfo();

                return result;
            }
        }