Esempio n. 1
0
 CrossValidate <TInput, TOutput, TRange, TModel, TLearner>(
     TRange ranges,
     Func <TRange, DataSubset <TInput, TOutput>, TLearner> learner,
     ComputeLoss <TOutput, TModel> loss,
     LearnNewModel <TLearner, TInput, TOutput, TModel> fit, // necessary to auto-detect TModel,
     TInput[] x, TOutput[] y, int folds = 10)
     where TModel : class, ITransform <TInput, TOutput>
     where TLearner : ISupervisedLearning <TModel, TInput, TOutput>
 {
     return(GridSearch <TInput, TOutput> .CrossValidate(ranges, learner, loss, fit, folds));
 }
Esempio n. 2
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>
 /// <typeparam name="TInput">The type of the input data. Default is double[].</typeparam>
 /// <typeparam name="TOutput">The type of the output data. Default is int.</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>
 /// <param name="x">The input data to be used during training.</param>
 /// <param name="y">The output data to be used during training.</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 <TInput, TOutput, 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,
     TInput[] x, TOutput[] y, int folds = 10)
     where TModel : class, ITransform <TInput, TOutput>
     where TLearner : ISupervisedLearning <TModel, TInput, TOutput>
 {
     return(GridSearch <TInput, TOutput> .CrossValidate(ranges, learner, loss, fit, folds));
 }
Esempio n. 3
0
 /// <summary>
 ///   Creates a new <see cref="GridSearch{TModel, TRange, TLearner, TInput, TOutput}"/> algorithm.
 /// </summary>
 ///
 /// <typeparam name="TModel">The type of the machine learning model whose parameters should be searched.</typeparam>
 /// <typeparam name="TRange">The type that specifies how ranges of the parameter values are represented.</typeparam>
 /// <typeparam name="TLearner">The type of the learning algorithm used to learn <typeparamref name="TModel"/>.</typeparam>
 /// <typeparam name="TInput">The type of the input data. Default is double[].</typeparam>
 /// <typeparam name="TOutput">The type of the output data. Default is int.</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="x">The input data to be used during training.</param>
 /// <param name="y">The output data to be used during training.</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 <TModel, TRange, TLearner, TInput, TOutput> Create <TInput, TOutput, TRange, TModel, TLearner>(
     TRange ranges,
     CreateLearnerFromParameter <TLearner, TRange> learner,
     ComputeLoss <TOutput, TModel> loss,
     LearnNewModel <TLearner, TInput, TOutput, TModel> fit,
     TInput[] x, TOutput[] y)
     where TModel : class, ITransform <TInput, TOutput>
     where TLearner : ISupervisedLearning <TModel, TInput, TOutput>
 {
     return(GridSearch <TInput, TOutput> .Create(ranges, learner, loss, fit));
 }
Esempio n. 4
0
 /// <summary>
 ///   Creates a new <see cref="GridSearch{TModel, TRange, TLearner, TInput, TOutput}"/> algorithm.
 /// </summary>
 ///
 /// <typeparam name="TInput">The type of the input data. Default is double[].</typeparam>
 /// <typeparam name="TOutput">The type of the output data. Default is int.</typeparam>
 /// <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="x">The input data to be used during training.</param>
 /// <param name="y">The output data to be used during training.</param>
 /// <param name="weights">The weights of each instance in the trianing data.</param>
 ///
 /// <example>
 ///   <code source="Unit Tests\Accord.Tests.MachineLearning\GridSearchTest.cs" region="doc_create" />
 /// </example>
 ///
 /// <returns>A grid-search algorithm that has been configured with the given parameters.</returns>
 ///
 public static GridSearchResult <TModel, TInput, TOutput> Create <TInput, TOutput, TModel, TLearner>(
     GridSearchRange[] ranges,
     CreateLearnerFromParameter <TLearner, GridSearchParameterCollection> learner,
     ComputeLoss <TOutput, TModel> loss,
     LearnNewModel <TLearner, TInput, TOutput, TModel> fit,
     TInput[] x, TOutput[] y, double[] weights = null)
     where TModel : class, ITransform <TInput, TOutput>
     where TLearner : ISupervisedLearning <TModel, TInput, TOutput>
 {
     return(GridSearch <TInput, TOutput> .Create(ranges, learner, loss, fit).Learn(x, y, weights));
 }
Esempio n. 5
0
 /// <summary>
 ///   Creates a new <see cref="GridSearch{TModel, TRange, TLearner, TInput, TOutput}"/> algorithm.
 /// </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>
 ///
 /// <example>
 ///   <code source="Unit Tests\Accord.Tests.MachineLearning\GridSearchTest.cs" region="doc_create" />
 /// </example>
 ///
 /// <returns>A grid-search algorithm that has been configured with the given parameters.</returns>
 ///
 public static GridSearch <TModel, TLearner, TInput, TOutput> Create <TModel, TLearner>(
     GridSearchRange[] ranges,
     CreateLearnerFromParameter <TLearner, GridSearchParameterCollection> learner,
     ComputeLoss <TOutput, TModel> loss,
     LearnNewModel <TLearner, TInput, TOutput, TModel> fit)
     where TModel : class, ITransform <TInput, TOutput>
     where TLearner : ISupervisedLearning <TModel, TInput, TOutput>
 {
     return(new GridSearch <TModel, TLearner, TInput, TOutput>()
     {
         ParameterRanges = new GridSearchRangeCollection(ranges),
         Learner = learner,
         Fit = fit,
         Loss = loss
     });
 }
Esempio n. 6
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);
        }
Esempio n. 7
0
 /// <summary>
 ///   Creates a new <see cref="CrossValidation{TModel, TLearner, TInput, TOutput}"/> algorithm.
 /// </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>
 /// <typeparam name="TInput">The type of the input data. Default is double[].</typeparam>
 /// <typeparam name="TOutput">The type of the output data. Default is int.</typeparam>
 ///
 /// <param name="k">The number of folds in the k-fold cross-validation. Default is 10.</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="x">The input data to be used during training.</param>
 /// <param name="y">The output data to be used during training.</param>
 ///
 /// <example>
 ///   <code source="Unit Tests\Accord.Tests.MachineLearning\CrossValidationTest.cs" region="doc_learn" />
 ///   <code source="Unit Tests\Accord.Tests.MachineLearning\CrossValidationTest.cs" region="doc_learn_hmm" />
 ///   <code source="Unit Tests\Accord.Tests.MachineLearning\DecisionTrees\DecisionTreeTest.cs" region="doc_cross_validation" />
 /// </example>
 ///
 /// <returns>A grid-search algorithm that has been configured with the given parameters.</returns>
 ///
 public static CrossValidation <TModel, TLearner, TInput, TOutput> Create <TModel, TLearner, TInput, TOutput>(int k,
                                                                                                              CreateLearnerFromSubset <TLearner, TInput, TOutput> learner,
                                                                                                              LearnNewModel <TLearner, TInput, TOutput, TModel> fit,
                                                                                                              ComputeLoss <TOutput, SetResult <TModel> > loss,
                                                                                                              TInput[] x, TOutput[] y)
     where TModel : class, ITransform <TInput, TOutput>
     where TLearner : class, ISupervisedLearning <TModel, TInput, TOutput>
 {
     return(new CrossValidation <TModel, TLearner, TInput, TOutput>()
     {
         K = k,
         Learner = learner,
         Fit = fit,
         Loss = loss,
     });
 }