GridSearchTInput, TOutputCreateTModel, TLearner Method (GridSearchRange, CreateLearnerFromParameterTLearner, GridSearchParameterCollection, ComputeLossTOutput, TModel, LearnNewModelTLearner, TInput, TOutput, TModel) |
Namespace: Accord.MachineLearning.Performance
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, Object, ITransform<TInput, TOutput> where TLearner : Object, ISupervisedLearning<TModel, TInput, TOutput>
// Ensure results are reproducible Accord.Math.Random.Generator.Seed = 0; // Example binary data double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] xor = // xor labels { -1, 1, 1, -1 }; // Instantiate a new Grid Search algorithm for Kernel Support Vector Machines var gridsearch = GridSearch<double[], int>.Create( ranges: new GridSearchRange[] { new GridSearchRange("complexity", new double[] { 0.00000001, 5.20, 0.30, 0.50 } ), new GridSearchRange("degree", new double[] { 1, 10, 2, 3, 4, 5 } ), new GridSearchRange("constant", new double[] { 0, 1, 2 } ) }, learner: (p) => new SequentialMinimalOptimization<Polynomial> { Complexity = p["complexity"], Kernel = new Polynomial((int)p["degree"].Value, p["constant"]) }, // Define how the model should be learned, if needed fit: (teacher, x, y, w) => teacher.Learn(x, y, w), // Define how the performance of the models should be measured loss: (actual, expected, m) => new ZeroOneLoss(expected).Loss(actual) ); // If needed, control the degree of CPU parallelization gridsearch.ParallelOptions.MaxDegreeOfParallelism = 1; // Search for the best model parameters var result = gridsearch.Learn(inputs, xor); // Get the best SVM generated during the search SupportVectorMachine<Polynomial> svm = result.BestModel; // Get an estimate for its error: double bestError = result.BestModelError; // Get the best values for its parameters: double bestC = result.BestParameters["complexity"].Value; double bestDegree = result.BestParameters["degree"].Value; double bestConstant = result.BestParameters["constant"].Value;