Page 43 - MSDN Magazine, March 2019
P. 43

The code for the demo program is too long to present in its entirety in this article, but the complete source code is available in the accompanying file download. All normal error checking has been removed to keep the main ideas as clear as possible.
Overall Program Structure
The structure of the demo program is shown in Figure 2. All the control logic is contained in a single Main method. Program- defined class SupportVectorMachine declares all member fields as public so you can more easily inspect them programmatically.
I used Visual Studio 2017 to create the demo program, but there are no significant .NET Framework dependencies so any ver- sion of Visual Studio will work fine. I created a new C# console application and named it SVM_CSharp. After the template code loaded into the editor window, I removed all unneeded using statements and then added a reference to the Collections.Generic assembly. In the Solution Explorer window, I right-clicked on file
Figure 1 SVM Demo Program in Action msdnmagazine.com
Program.cs and renamed it to SVM_Program.cs and allowed Visual Studio to automatically rename class Program.
Using the SVM
The demo program sets up eight hardcoded training items:
double[][] train_X = new double[8][] { new double[] { 4,5,7 },
...
new double[] { 8,9,10 } };
int[] train_y = new int[8]{ -1, -1, -1, -1, 1, 1, 1, 1 };
In a non-demo scenario, you should normalize the predictor values, and you’d likely store your data in a text file. The SVM clas- sifier is created like so:
var svm = new SupportVectorMachine("poly", 0); svm.gamma = 1.0;
svm.coef = 0.0;
svm.degree = 2;
The “poly” argument is really a dummy value because the SVM is hardcoded to use a polynomial kernel function. The 0 argument is a seed value for the random component of the training algorithm. The gamma, coef (also called constant), and degree arguments are parameters for the polynomial kernel function. Next, parameters for the training algorithm are specified:
svm.complexity = 1.0; svm.epsilon = 0.001; svm.tolerance = 0.001; int maxIter = 1000;
All of these values are hyperparameters that must be determined by trial and error. The main challenge when using any implemen- tation of an SVM is understanding which kernel to use, the kernel parameters and the training parameters. Training is performed by a single statement:
int iter = svm.Train(train_X, train_y, maxIter);
Figure 2 Demo Program Structure
using System;
using System.Collections.Generic; namespace SVM_CSharp
{
class SVM_Program {
static void Main(string[] args) {
// Set up training data
// Create SVM object, set parameters // Train the SVM
// Display SVM properties
// Use trained SVM to make a prediction
} }
public class SupportVectorMachine {
// All member fields are declared public
public SupportVectorMachine(string kernelType, int seed) . .
public double PolyKernel(double[] v1, double[] v2) . . public double ComputeDecision(double[] input) . . public int Train(double[][] X_matrix,
int[] y_vector, int maxIter) . .
public double Accuracy(double[][] X_matrix,
int[] y_vector) . .
private bool TakeStep(int i1, int i2,
double[][] X_matrix, int[] y_vector) . .
private int ExamineExample(int i2, double[][] X_matrix,
int[] y_vector) . .
private double ComputeAll(double[] vector,
double[][] X_matrix, int[] y_vector) . . }
}
March 2019 37


































































































   41   42   43   44   45