Click or drag to resize
Accord.NET (logo)

IParallelParallelOptions Property

Gets or sets the parallelization options for this algorithm. It can be used to control the maximum number of cores that should be used during the algorithm's execution.

Namespace:  Accord.MachineLearning
Assembly:  Accord (in Accord.dll) Version: 3.8.0
Syntax
ParallelOptions ParallelOptions { get; set; }
Request Example View Source

Property Value

Type: ParallelOptions
Remarks
The IParallel interface is implemented by most machine learning algorithms in the framework, and it is most common use is to allow the user to tune how many cores should be used by a multi-threaded learning algorithm.
Examples

In the following example, we will be using the ParallelOptions property to limit the maximum degree of parallelism of a support vector machine learning algorithm to be 1, meaning the algorithm will be running in a single thread:

// Let's say we have the following data to be classified
// into three possible classes. Those are the samples:
// 
double[][] inputs =
{
    //               input         output
    new double[] { 0, 1, 1, 0 }, //  0 
    new double[] { 0, 1, 0, 0 }, //  0
    new double[] { 0, 0, 1, 0 }, //  0
    new double[] { 0, 1, 1, 0 }, //  0
    new double[] { 0, 1, 0, 0 }, //  0
    new double[] { 1, 0, 0, 0 }, //  1
    new double[] { 1, 0, 0, 0 }, //  1
    new double[] { 1, 0, 0, 1 }, //  1
    new double[] { 0, 0, 0, 1 }, //  1
    new double[] { 0, 0, 0, 1 }, //  1
    new double[] { 1, 1, 1, 1 }, //  2
    new double[] { 1, 0, 1, 1 }, //  2
    new double[] { 1, 1, 0, 1 }, //  2
    new double[] { 0, 1, 1, 1 }, //  2
    new double[] { 1, 1, 1, 1 }, //  2
};

int[] outputs = // those are the class labels
{
    0, 0, 0, 0, 0,
    1, 1, 1, 1, 1,
    2, 2, 2, 2, 2,
};

// Create a one-vs-one multi-class SVM learning algorithm 
var teacher = new MulticlassSupportVectorLearning<Linear>()
{
    // using LIBLINEAR's L2-loss SVC dual for each SVM
    Learner = (p) => new LinearDualCoordinateDescent()
    {
        Loss = Loss.L2
    }
};

// The following line is only needed to ensure reproducible results. Please remove it to enable full parallelization
teacher.ParallelOptions.MaxDegreeOfParallelism = 1; // (Remove, comment, or change this line to enable full parallelism)

// Learn a machine
var machine = teacher.Learn(inputs, outputs);

// Obtain class predictions for each sample
int[] predicted = machine.Decide(inputs);

// Compute classification error
double error = new ZeroOneLoss(outputs).Loss(predicted);
See Also