Normalizing with Dlib

The Dlib library provides functionality for feature standardization with the Dlib::vector_normalizer class. There is one limitation for using this class—we cannot use it with one big matrix containing all training samples. Alternatively, we should represent each sample with a separate vector object and put them into the C++ std::vector container, as follows:

std::vector<matrix<double>> samples;
...
vector_normalizer<matrix<double>> normalizer;
samples normalizer.train(samples);
samples = normalizer(samples);

We see that the object of this class can be reused, but it should be trained at first. The train method implementation can look like this:

matrix<double> m(mean(mat(samples)));
matrix<double> sd(reciprocal(stddev(mat(samples))));
for (size_t i = 0; i < samples.size(); ++i)
samples[i] = pointwise_multiply(samples[i] - m, sd);

Notice that the Dlib::mat() function has different overloads for matrix creation from different sources.  Also, we use the reciprocal() function that makes the   matrix if the m is the input matrix.

Printing matrices for debugging purpose in the Dlib library can be done with the simple streaming operator, as illustrated in the following code snippet:

std::cout << mat(samples) << std::endl;
..................Content has been hidden....................

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