Exemplo 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);
        }
Exemplo n.º 2
0
        public void PerformTraining(ILearningMethod method, object model, IEnumerable validationData)
        {
            IRegressionAlgorithm regression = model as IRegressionAlgorithm;

            if (regression != null)
            {
                IErrorCalculation errorCalc;
                switch (ErrorCalculation)
                {
                case ErrorCaclulationType.MeanSquare:
                    errorCalc = new ErrorCalculationMSE();
                    break;

                case ErrorCaclulationType.RootMeanSquare:
                    errorCalc = new ErrorCalculationRMS();
                    break;

                case ErrorCaclulationType.SumOfSquares:
                    errorCalc = new ErrorCalculationSSE();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                new SimpleLearn().PerformIterationsEarlyStop(method, regression, (IList <BasicData>)validationData,
                                                             Tolerate, errorCalc);
            }
            else
            {
                IClassificationAlgorithm classification = (IClassificationAlgorithm)model;
                new SimpleLearn().PerformIterationsClassifyEarlyStop(method, classification,
                                                                     (IList <BasicData>)validationData, Tolerate);
            }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public object Train(object owner, IContextLookup globalVars)
        {
            object              theNetwork            = Network.EvaluateTyped(owner, globalVars);
            IEnumerable         theData               = Data.EvaluateTyped(owner, globalVars);
            ILearningMethodInfo theLearningMethodInfo = LearningMethod.EvaluateTyped(owner, globalVars);

            ILearningMethod learningMethod = theLearningMethodInfo.GetLearningMethod(owner, globalVars, theNetwork,
                                                                                     theData);
            IEnumerable theValidationData = ValidationData.EvaluateTyped(owner, globalVars);

            ITrainingInfo theTrainMode = TrainMode.EvaluateTyped(owner, globalVars);

            theTrainMode.PerformTraining(learningMethod, theNetwork, theValidationData);
            return(theNetwork);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
 public void PerformTraining(ILearningMethod method, object model, IEnumerable validationData)
 {
     new SimpleLearn().PerformIterations(method, MaxIterations, TargetScore, ShouldMinimize);
 }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
 public void TrainNetwork(Dataset trainingSet, ILearningMethod LearningMethod)
 {
     LearningMethod.TrainNetwork(this, trainingSet);
 }
Exemplo n.º 12
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);
        }