Ejemplo n.º 1
0
        /// <summary>
        /// Perform training iterations.
        /// </summary>
        /// <param name="train">The learning algorithm.</param>
        /// <param name="maxIterations">The max number of iterations.</param>
        /// <param name="targetScore">The target score.</param>
        /// <param name="shouldMinimize">True, if we should minimize.</param>
        public void PerformIterations(ILearningMethod train, int maxIterations, double targetScore, bool shouldMinimize)
        {
            int  iterationNumber = 0;
            bool done            = false;

            do
            {
                iterationNumber++;

                train.Iteration();

                if (train.Done)
                {
                    done = true;
                }
                else if (iterationNumber >= maxIterations)
                {
                    done = true;
                }
                else if (shouldMinimize && train.LastError < targetScore)
                {
                    done = true;
                }
                else if (!shouldMinimize && train.LastError > targetScore)
                {
                    done = true;
                }

                Console.WriteLine("Iteration #" + iterationNumber + ", Score=" + train.LastError + ", " + train.Status);
            } while (!done);

            train.FinishTraining();
            Console.WriteLine("Final score: " + train.LastError);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Perform training iterations.
        /// </summary>
        /// <param name="train">The learning algorithm.</param>
        /// <param name="maxIterations">The max number of iterations.</param>
        /// <param name="targetScore">The target score.</param>
        /// <param name="shouldMinimize">True, if we should minimize.</param>
        public void PerformIterations(ILearningMethod train, int maxIterations, double targetScore, bool shouldMinimize)
        {
            int iterationNumber = 0;
            bool done = false;

            do
            {
                iterationNumber++;

                train.Iteration();

                if (train.Done)
                {
                    done = true;
                }
                else if (iterationNumber >= maxIterations)
                {
                    done = true;
                }
                else if (shouldMinimize && train.LastError < targetScore)
                {
                    done = true;
                }
                else if (!shouldMinimize && train.LastError > targetScore)
                {
                    done = true;
                }

                Console.WriteLine("Iteration #" + iterationNumber + ", Score=" + train.LastError + ", " + train.Status);
            } while (!done);

            train.FinishTraining();
            Console.WriteLine("Final score: " + train.LastError);
        }
Ejemplo n.º 3
0
        public void PerformIterationsClassifyEarlyStop(ILearningMethod train,
                                                       IClassificationAlgorithm model,
                                                       IList <BasicData> validationData,
                                                       int tolerate)
        {
            var iterationNumber   = 0;
            var done              = false;
            var bestError         = double.PositiveInfinity;
            var badIterations     = 0;
            var bestParams        = new double[model.LongTermMemory.Length];
            var bestTrainingError = 0.0;

            do
            {
                iterationNumber++;

                train.Iteration();
                var validationError = DataUtil.CalculateClassificationError(validationData, model);

                if (validationError < bestError)
                {
                    badIterations     = 0;
                    bestError         = validationError;
                    bestTrainingError = train.LastError;
                    Array.Copy(model.LongTermMemory, bestParams, bestParams.Length);
                }
                else
                {
                    badIterations++;
                }

                if (train.Done)
                {
                    done = true;
                }
                else if (badIterations > tolerate)
                {
                    done = true;
                }
                else if (double.IsNaN(train.LastError))
                {
                    Console.WriteLine("Weights unstable, stopping training.");
                    done = true;
                }

                Console.WriteLine("Iteration #" + iterationNumber
                                  + ", training error=" + train.LastError
                                  + ", validation # Incorrect= %" + validationError * 100.0
                                  + ", " + train.Status);
            } while (!done);

            train.FinishTraining();
            Console.WriteLine("Best training error: " + bestTrainingError);
            Console.WriteLine("Restoring weights to best iteration");
            Array.Copy(bestParams, model.LongTermMemory, bestParams.Length);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Train and stop when the validation set does not improve anymore.
        /// </summary>
        /// <param name="train">The trainer to use.</param>
        /// <param name="model">The model that is trained.</param>
        /// <param name="validationData">The validation data.</param>
        /// <param name="tolerate">Number of iterations to tolerate no improvement to the validation error.</param>
        /// <param name="errorCalc">The error calculation method.</param>
        public void PerformIterationsEarlyStop(ILearningMethod train,
                                               IRegressionAlgorithm model,
                                               IList <BasicData> validationData,
                                               int tolerate,
                                               IErrorCalculation errorCalc)
        {
            var iterationNumber = 0;
            var done            = false;
            var bestError       = double.PositiveInfinity;
            var badIterations   = 0;

            do
            {
                iterationNumber++;

                train.Iteration();
                var validationError = DataUtil.CalculateRegressionError(validationData, model, errorCalc);

                if (validationError < bestError)
                {
                    badIterations = 0;
                    bestError     = validationError;
                }
                else
                {
                    badIterations++;
                }

                if (train.Done)
                {
                    done = true;
                }
                else if (validationError > bestError && badIterations > tolerate)
                {
                    done = true;
                }
                else if (double.IsNaN(train.LastError))
                {
                    Console.WriteLine("Training failed.");
                    done = true;
                }

                Console.WriteLine("Iteration #" + iterationNumber
                                  + ", Iteration Score=" + train.LastError
                                  + ", Validation Score=" + validationError
                                  + ", " + train.Status);
            } while (!done);

            train.FinishTraining();
            Console.WriteLine("Final score: " + train.LastError);
        }
Ejemplo n.º 5
0
        private void PerformTest(ILearningMethod train)
        {
            Assert.IsFalse(train.Done);

            Assert.IsNotNull(train.Status);
            train.Iteration();
            double startError = train.LastError;

            for (int i = 0; i < 1000 && !train.Done; i++)
            {
                train.Iteration();
            }

            // make sure one last iteration does not blow up(if done was true)
            train.Iteration();

            train.FinishTraining();
            Assert.IsTrue((train.LastError < startError) || Math.Abs(train.LastError) < 1);
        }
Ejemplo n.º 6
0
        private void PerformTest(ILearningMethod train)
        {

            Assert.IsFalse(train.Done);

            Assert.IsNotNull(train.Status);
            train.Iteration();
            double startError = train.LastError;

            for (int i = 0; i < 1000 && !train.Done; i++)
            {
                train.Iteration();
            }

            // make sure one last iteration does not blow up(if done was true)
            train.Iteration();

            train.FinishTraining();
            Assert.IsTrue((train.LastError < startError) || Math.Abs(train.LastError) < 1);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Train and stop when the validation set does not improve anymore.
        /// </summary>
        /// <param name="train">The trainer to use.</param>
        /// <param name="model">The model that is trained.</param>
        /// <param name="validationData">The validation data.</param>
        /// <param name="tolerate">Number of iterations to tolerate no improvement to the validation error.</param>
        /// <param name="errorCalc">The error calculation method.</param>
        public void PerformIterationsEarlyStop(ILearningMethod train,
            IRegressionAlgorithm model,
            IList<BasicData> validationData,
            int tolerate,
            IErrorCalculation errorCalc)
        {
            var iterationNumber = 0;
            var done = false;
            var bestError = double.PositiveInfinity;
            var badIterations = 0;

            do
            {
                iterationNumber++;

                train.Iteration();
                var validationError = DataUtil.CalculateRegressionError(validationData, model, errorCalc);

                if (validationError < bestError)
                {
                    badIterations = 0;
                    bestError = validationError;
                }
                else
                {
                    badIterations++;
                }

                if (train.Done)
                {
                    done = true;
                }
                else if (validationError > bestError && badIterations > tolerate)
                {
                    done = true;
                }
                else if (double.IsNaN(train.LastError))
                {
                    Console.WriteLine("Training failed.");
                    done = true;
                }

                Console.WriteLine("Iteration #" + iterationNumber
                                  + ", Iteration Score=" + train.LastError
                                  + ", Validation Score=" + validationError
                                  + ", " + train.Status);
            } while (!done);

            train.FinishTraining();
            Console.WriteLine("Final score: " + train.LastError);
        }
Ejemplo n.º 8
0
        public void PerformIterationsClassifyEarlyStop(ILearningMethod train,
            IClassificationAlgorithm model,
            IList<BasicData> validationData,
            int tolerate)
        {
            var iterationNumber = 0;
            var done = false;
            var bestError = double.PositiveInfinity;
            var badIterations = 0;
            var bestParams = new double[model.LongTermMemory.Length];
            var bestTrainingError = 0.0;

            do
            {
                iterationNumber++;

                train.Iteration();
                var validationError = DataUtil.CalculateClassificationError(validationData, model);

                if (validationError < bestError)
                {
                    badIterations = 0;
                    bestError = validationError;
                    bestTrainingError = train.LastError;
                    Array.Copy(model.LongTermMemory, bestParams, bestParams.Length);
                }
                else
                {
                    badIterations++;
                }

                if (train.Done)
                {
                    done = true;
                }
                else if (badIterations > tolerate)
                {
                    done = true;
                }
                else if (double.IsNaN(train.LastError))
                {
                    Console.WriteLine("Weights unstable, stopping training."); 
                    done = true;
                }

                Console.WriteLine("Iteration #" + iterationNumber
                                  + ", training error=" + train.LastError
                                  + ", validation # Incorrect= %" + validationError*100.0
                                  + ", " + train.Status);
            } while (!done);

            train.FinishTraining();
            Console.WriteLine("Best training error: " + bestTrainingError);
            Console.WriteLine("Restoring weights to best iteration");
            Array.Copy(bestParams, model.LongTermMemory, bestParams.Length);
        }