Model serialization with Dlib

The Dlib library uses the serialization API for decision_function and neural network type objects. Let's learn how to use it by implementing a real example.

First, we define the types for the neural network, regression kernel, and training sample:

 using namespace dlib;  using NetworkType = loss_mean_squared<fc<1, input<matrix<double>>>>; using SampleType = matrix<double, 1, 1>; using KernelType = linear_kernel<SampleType>;

Then, we generate the training data with the following code:

 size_t n = 1000; std::vector<matrix<double>> x(n); std::vector<float> y(n);  std::random_device rd; std::mt19937 re(rd()); std::uniform_real_distribution<float> dist(-1.5, 1.5);  // generate data for (size_t i = 0; i < n; ++i) { ...

Get Hands-On Machine Learning with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.