With SVM

To implement the SVM multi-class classification with the Shark-ML library, we should use the same approach as we used before for the logistic regression approach. The main difference is the type of binary classifiers. In this case, it is the KernelClassifier class, and the trainer for it is the CSvmTrainer class.

In the following sample, we will use two predefined parameters for the model, one being the C parameter for the SVM algorithm, and the second being the gamma for the kernel. The kernel object in this sample has the GaussianRbfKernel type, to deal with non-linearly separable data.

The following code snippet shows a function declaration with dataset objects as arguments:

void SVMClassification(const ClassificationDataset& train,
const ClassificationDataset& test,
unsigned int num_classes) {
...
}

Then, we define a kernel object, as follows:

double gamma = 0.5;
GaussianRbfKernel<> kernel(gamma);

The following code snippet shows how to initialize and configure a one-versus-one classifier object:

OneVersusOneClassifier<RealVector> ovo;
unsigned int pairs = num_classes * (num_classes - 1) / 2;
std::vector<KernelClassifier<RealVector> > svm(pairs);
for (std::size_t n = 0, cls1 = 1; cls1 < num_classes; cls1++) {
using BinaryClassifierType =
OneVersusOneClassifier<RealVector>::binary_classifier_type;
std::vector<BinaryClassifierType*> ovo_classifiers;
for (std::size_t cls2 = 0; cls2 < cls1; cls2++, n++) {
// get the binary subproblem
ClassificationDataset binary_cls_data =
binarySubProblem(train, cls2, cls1);

// train the binary machine
double c = 10.0;
CSvmTrainer<RealVector> trainer(&kernel, c, false);
trainer.train(svm[n], binary_cls_data);
ovo_classifiers.push_back(&svm[n]);
}
ovo.addClass(ovo_classifiers);
}

And after the training completion, we use the ovo object for evaluation, as follows:

// estimate accuracy
ZeroOneLoss<unsigned int> loss;
Data<unsigned int> output = ovo(test.inputs());
double accuracy = 1. - loss.eval(test.labels(), output);

// process results
for (std::size_t i = 0; i != test.numberOfElements(); i++) {
auto cluser_idx = output.element(i);
auto element = test.inputs().element(i);
...
}

The following screenshot shows the results of applying the Shark-ML implementation of the SVM algorithm to our datasets:

Notice that the Shark-ML SVM algorithm implementation did the correct classification on all datasets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.184