private void btnCreateVectorMachines_Click(object sender, EventArgs e)
{
double[][] inputs;
int[] outputs;
getData(out inputs, out outputs);
int classes = outputs.Distinct().Count();
var kernel = getKernel();
// Create the Multi-class Support Vector Machine using the selected Kernel
ksvm = new MulticlassSupportVectorMachine(inputs[0].Length, kernel, classes);
// Create the learning algorithm using the machine and the training data
MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, inputs, outputs);
// Extract training parameters from the interface
double complexity = (double)numComplexity.Value;
double tolerance = (double)numTolerance.Value;
int cacheSize = (int)numCache.Value;
SelectionStrategy strategy = (SelectionStrategy)cbStrategy.SelectedItem;
// Configure the learning algorithm
ml.Algorithm = (svm, classInputs, classOutputs, i, j) =>
{
return new SequentialMinimalOptimization(svm, classInputs, classOutputs)
{
Complexity = complexity,
Tolerance = tolerance,
CacheSize = cacheSize,
Strategy = strategy,
};
};
lbStatus.Text = "Training the classifiers. This may take a (very) significant amount of time...";
Application.DoEvents();
Stopwatch sw = Stopwatch.StartNew();
// Train the machines. It should take a while.
double error = ml.Run();
sw.Stop();
lbStatus.Text = String.Format(
"Training complete ({0}ms, {1}er). Click Classify to test the classifiers.",
sw.ElapsedMilliseconds, error);
btnClassifyElimination.Enabled = true;
// Populate the information tab with the machines
dgvMachines.Rows.Clear();
int k = 1;
for (int i = 0; i < classes; i++)
{
for (int j = 0; j < i; j++, k++)
{
var machine = ksvm[i, j];
int sv = machine.SupportVectors == null ? 0 : machine.SupportVectors.Length;
int c = dgvMachines.Rows.Add(k, i + "-vs-" + j, sv, machine.Threshold);
dgvMachines.Rows[c].Tag = machine;
}
}
// approximate size in bytes =
// number of support vectors *
// number of doubles in a support vector *
// size of double
int bytes = ksvm.SupportVectorUniqueCount * 1024 * sizeof(double);
float megabytes = bytes / (1024 * 1024);
lbSize.Text = String.Format("{0} ({1} MB)", ksvm.SupportVectorUniqueCount, megabytes);
}