예제 #1
0
파일: NetworkOld.cs 프로젝트: widovd/NEUNET
        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}.");
        }
예제 #2
0
파일: NetworkOld.cs 프로젝트: widovd/NEUNET
        // ----------------------------------------------------------------------------------------
        #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);
        }