Click or drag to resize
Accord.NET (logo)

GridSearchTModel Class

Note: This API is now obsolete.

Grid search procedure for automatic parameter tuning.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningGridSearchTModel

Namespace:  Accord.MachineLearning
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
[ObsoleteAttribute("Please use GridSearch<TModel, TInput, TOutput> instead.")]
public class GridSearch<TModel> : IParallel, 
	ISupportsCancellation
where TModel : class
Request Example View Source

Type Parameters

TModel
The type of the model to be tuned.

The GridSearchTModel type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyFitting
A function that fits a model using the given parameters.
Public propertyParallelOptions
Gets or sets the parallelization options for this algorithm.
Public propertyParameterRanges
The range of parameters to consider during search.
Public propertyToken
Gets or sets a cancellation token that can be used to cancel the algorithm while it is running.
Top
Methods
  NameDescription
Public methodCompute
Searches for the best combination of parameters that results in the most accurate model.
Public methodCompute(GridSearchParameterCollection, Double)
Searches for the best combination of parameters that results in the most accurate model.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks
Grid Search tries to find the best combination of parameters across a range of possible values that produces the best fit model. If there are two parameters, each with 10 possible values, Grid Search will try an exhaustive evaluation of the model using every combination of points, resulting in 100 model fits.
Examples
How to fit a Kernel Support Vector Machine using Grid Search.
// 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
};

// Declare the parameters and ranges to be searched
GridSearchRange[] ranges = 
{
    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 } )
};


// Instantiate a new Grid Search algorithm for Kernel Support Vector Machines
var gridsearch = new GridSearch<KernelSupportVectorMachine>(ranges);

// Set the fitting function for the algorithm
gridsearch.Fitting = delegate(GridSearchParameterCollection parameters, out double error)
{
    // The parameters to be tried will be passed as a function parameter.
    int degree = (int)parameters["degree"].Value;
    double constant = parameters["constant"].Value;
    double complexity = parameters["complexity"].Value;

    // Use the parameters to build the SVM model
    Polynomial kernel = new Polynomial(degree, constant);
    KernelSupportVectorMachine ksvm = new KernelSupportVectorMachine(kernel, 2);

    // Create a new learning algorithm for SVMs
    SequentialMinimalOptimization smo = new SequentialMinimalOptimization(ksvm, inputs, xor);
    smo.Complexity = complexity;

    // Measure the model performance to return as an out parameter
    error = smo.Run();

    return ksvm; // Return the current model
};


// Declare some out variables to pass to the grid search algorithm
GridSearchParameterCollection bestParameters; double minError;

// Compute the grid search to find the best Support Vector Machine
KernelSupportVectorMachine bestModel = gridsearch.Compute(out bestParameters, out minError);
See Also