Exemplo n.º 1
0
        /// <summary>
        ///   Creates a new <see cref="GridSearch{TModel, TRange, TLearner, TInput, TOutput}"/> combined with
        ///   <see cref="CrossValidation{TModel, TInput, TOutput}"/> algorithms.
        /// </summary>
        ///
        /// <typeparam name="TModel">The type of the machine learning model whose parameters should be searched.</typeparam>
        /// <typeparam name="TLearner">The type of the learning algorithm used to learn <typeparamref name="TModel"/>.</typeparam>
        ///
        /// <param name="ranges">The range of parameters to consider during search.</param>
        /// <param name="learner">A function that can create a <typeparamref name="TModel"/> given training parameters.</param>
        /// <param name="loss">A function that can measure how far model predictions are from the expected ground-truth.</param>
        /// <param name="fit">A function that specifies how to create a new model using the teacher learning algorirhm.</param>
        /// <param name="folds">The number of folds in the k-fold cross-validation. Default is 10.</param>
        ///
        /// <example>
        ///   <code source="Unit Tests\Accord.Tests.MachineLearning\GridSearchTest.cs" region="doc_learn_strongly_typed" />
        /// </example>
        ///
        /// <returns>A grid-search algorithm that has been configured with the given parameters.</returns>
        ///
        public static GridSearch <CrossValidationResult <TModel, TInput, TOutput>, CrossValidation <TModel, TInput, TOutput>, TInput, TOutput> CrossValidate <TModel, TLearner>(
            GridSearchRange[] ranges,
            Func <GridSearchParameterCollection, DataSubset <TInput, TOutput>, TLearner> learner,
            ComputeLoss <TOutput, TModel> loss,
            LearnNewModel <TLearner, TInput, TOutput, TModel> fit, // necessary to auto-detect TModel,
            int folds = 10)
            where TModel : class, ITransform <TInput, TOutput>
            where TLearner : ISupervisedLearning <TModel, TInput, TOutput>
        {
            GridSearch <CrossValidationResult <TModel, TInput, TOutput>, CrossValidation <TModel, TInput, TOutput>, TInput, TOutput> gs = null;

            gs = Create <CrossValidationResult <TModel, TInput, TOutput>, CrossValidation <TModel, TInput, TOutput> >(ranges,

                                                                                                                      learner: (p) => new CrossValidation <TModel, TInput, TOutput>()
            {
                K       = folds,
                Learner = (ss) => learner(p, ss),
                Fit     = (teacher, x, y, w) => fit((TLearner)teacher, x, y, w), // necessary to auto-detect TModel
                Loss    = (expected, actual, r) => loss(expected, actual, r.Model),
            },

                                                                                                                      fit: (teacher, x, y, w) =>
            {
                IParallel parallel = teacher as IParallel;
                if (teacher != null)
                {
                    parallel.ParallelOptions = gs.ParallelOptions;
                }
                return(teacher.Learn(x, y, w));
            },

                                                                                                                      loss: (actual, expected, model) => model.Validation.Mean
                                                                                                                      );

            return(gs);
        }