With KRR

The following code sample shows how to use Dlib's KRR algorithm implementation for the multi-class classification:

 void KRRClassification(const Samples& samples,
const Labels& labels,
const Samples& test_samples,
const Labels& test_labels) {
using OVOtrainer = one_vs_one_trainer<any_trainer<SampleType>>;
using KernelType = radial_basis_kernel<SampleType>;

krr_trainer<KernelType> krr_trainer;
krr_trainer.set_kernel(KernelType(0.1));

OVOtrainer trainer;
trainer.set_trainer(krr_trainer);

one_vs_one_decision_function<OVOtrainer> df = trainer.train(samples,
labels);

// process results and estimate accuracy
DataType accuracy = 0;
for (size_t i = 0; i != test_samples.size(); i++) {
auto vec = test_samples[i];
auto class_idx = static_cast<size_t>(df(vec));
if (static_cast<size_t>(test_labels[i]) == class_idx)
++accuracy;
...
}

accuracy /= test_samples.size();
}

Firstly, we initialized the object of the krr_trainer class, and then we configured it with the instance of a kernel object. In this example, we used the radial_basis_kernel type for the kernel object, in order to deal with samples that can't be linearly separated. After we obtained the binary classifier object, we initialized the instance of the one_vs_one_trainer class and added this classifier to its stack with the set_trainer() method. Then, we used the train() method for training our multi-class classifier. As with most of the algorithms in the Dlib library, this one assumes that the training samples and labels have the std::vector type, whereby each element has a matrix type. The train() method returns a decision function—namely, the object that behaves as a functor, which then takes a single sample and returns a classification label for it. This decision function is an object of the one_vs_one_decision_function type. The following piece of code demonstrates how we can use it:

   auto vec = test_samples[i];
auto class_idx = static_cast<size_t>(df(vec));

There is no explicit implementation for the accuracy metric in the Dlib library; so, in this example, accuracy is calculated directly as a ration of correctly classified test samples against the total number of test samples.

The following screenshot shows the results of applying the Dlib implementation of the KRR algorithm to our datasets:

Notice that the KRR algorithm performed a 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.145.8.8