Machine Learning in C++: Overview of Key Libraries
Although Python has become the de facto standard for research and rapid prototyping in the field of machine learning (ML), C++ remains an indispensable language for production systems where performance, low memory consumption, and real-time operation are critical. C++ allows you to get the maximum out of hardware, which is especially important when deploying models on mobile devices, embedded systems, or high-load servers.
In this article, we will review five leading machine learning libraries for C++, their strengths, typical use cases, and provide practical code examples. You will learn which library to choose for classification, regression, clustering, working with neural networks, or signal processing.
1. TensorFlow C++ API — The Power of Deep Learning
TensorFlow is one of the most popular frameworks for deep learning. Its C++ API allows you to load pre-trained models (e.g., from Python) and perform inference at maximum speed. This is an ideal choice for embedded systems (Raspberry Pi, Jetson Nano, Android) and production servers.
Key features:
- Working with saved computation graphs (SavedModel, Frozen Graph).
- GPU support via CUDA and cuDNN.
- Model optimization for a specific device (XLA, Quantization).
Example of loading and running a model (inference):
#include <tensorflow/core/public/session.h>#include <tensorflow/core/platform/env.h>
using namespace tensorflow;
int main() { // Create a session Session* session; NewSession(SessionOptions(), &session);
// Load the graph from a file GraphDef graph_def; Status status = ReadBinaryProto(Env::Default(), "model.pb", &graph_def); if (!status.ok()) { /* error handling */ }
// Add the graph to the session session->Create(graph_def);
// Prepare input data (tensor) Tensor input_tensor(DT_FLOAT, TensorShape({1, 224, 224, 3})); auto input_map = input_tensor.tensor<float, 4>(); // ... fill the tensor with data ...
// Run inference std::vector<Tensor> outputs; session->Run({{"input_tensor", input_tensor}}, {"output_tensor"}, {}, &outputs);
// Use the result auto result = outputs[0].tensor<float, 2>(); std::cout << "Prediction: " << result(0, 0) << std::endl;
session->Close(); return 0;}2. Dlib — A Versatile Tool for ML and Computer Vision
Dlib is a general-purpose library that includes both classical machine learning algorithms (SVM, decision trees, K-means) and modern deep neural networks. It is particularly strong in computer vision tasks: face detection, object tracking, face recognition (FaceNet).
Key features:
- Easy integration: header-only for most functions.
- Built-in support for multithreading (thread_pool).
- Ready-to-use utilities for image processing and matrix operations.
Example of training an SVM classifier:
#include <dlib/svm.h>#include <dlib/rand.h>
using namespace dlib;
int main() { // Kernel type: Radial Basis Function (RBF) typedef radial_basis_kernel<sample_type> kernel_type;
// Create a classifier and trainer svm_c_trainer<kernel_type> trainer; trainer.set_kernel(kernel_type(0.1));
// Training data std::vector<sample_type> samples; std::vector<double> labels; // ... fill samples and labels ...
// Training decision_function<kernel_type> df = trainer.train(samples, labels);
// Test a new sample