Click or drag to resize
Accord.NET (logo)

GridSearchCreateTInput, TOutput, TModel, TLearner Method (GridSearchRange, CreateLearnerFromParameterTLearner, GridSearchParameterCollection, ComputeLossTOutput, TModel, LearnNewModelTLearner, TInput, TOutput, TModel, TInput, TOutput)

Namespace:  Accord.MachineLearning.Performance
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
public static GridSearch<TModel, TLearner, 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
)
where TModel : class, Object, ITransform<TInput, TOutput>
where TLearner : Object, ISupervisedLearning<TModel, TInput, TOutput>
Request Example View Source

Parameters

ranges
Type: Accord.MachineLearningGridSearchRange
The range of parameters to consider during search.
learner
Type: Accord.MachineLearning.PerformanceCreateLearnerFromParameterTLearner, GridSearchParameterCollection
A function that can create a TModel given training parameters.
loss
Type: Accord.MachineLearning.PerformanceComputeLossTOutput, TModel
A function that can measure how far model predictions are from the expected ground-truth.
fit
Type: Accord.MachineLearning.PerformanceLearnNewModelTLearner, TInput, TOutput, TModel
A function that specifies how to create a new model using the teacher learning algorirhm.
x
Type: TInput
The input data to be used during training.
y
Type: TOutput
The output data to be used during training.

Type Parameters

TInput
The type of the input data. Default is double[].
TOutput
The type of the output data. Default is int.
TModel
The type of the machine learning model whose parameters should be searched.
TLearner
The type of the learning algorithm used to learn TModel.

Return Value

Type: GridSearchTModel, TLearner, TInput, TOutput
A grid-search algorithm that has been configured with the given parameters.
Examples
// 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;
See Also