C++ for Machine Learning: Real Capabilities and Objective Limitations
The world of machine learning (ML) today is inextricably linked with Python. However, behind the scenes of most frameworks, from TensorFlow to PyTorch, another language works — C++. Why is this the case and is it worth using C++ directly for ML tasks? In this article, we will analyze the strengths of C++ in machine learning, its limitations, and provide practical code examples.
Why C++ is Important for ML: Performance and Control
C++ is a compiled language that provides maximum performance and fine-grained control over memory. In the context of ML, this is critical for several reasons:
- Computational Speed: Loops, mathematical operations, and working with large volumes of data execute significantly faster than in interpreted languages.
- Memory Optimization: Direct memory management allows avoiding the overhead of garbage collection, which is important for models operating in real-time (robotics, autonomous systems).
- Hardware Integration: C++ allows direct work with GPUs (via CUDA) and other specialized devices (FPGA, ASIC).
This is precisely why the cores of all popular frameworks are written in C++: they provide a Python API for convenience, but critically important computations are performed in C++.
Key Libraries and Tools for ML in C++
Although the C++ ecosystem for ML is not as extensive as Python's, it includes powerful libraries:
1. TensorFlow C++ API
Allows loading trained models (from Python) and performing inference in C++. Ideal for production environments where speed is important.
#include "tensorflow/cc/saved_model/loader.h"#include <iostream>
int main() { // Loading a saved model tensorflow::SavedModelBundle model; tensorflow::SessionOptions session_options; tensorflow::RunOptions run_options;
tensorflow::Status status = tensorflow::LoadSavedModel( session_options, run_options, "path/to/model", {"serve"}, &model);
if (!status.ok()) { std::cerr << "Model loading error: " << status.ToString() << std::endl; return 1; }
// Preparing input data (example) tensorflow::Tensor input(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 784})); // ... filling input with data ...
std::vector<tensorflow::Tensor> outputs; status = model.session->Run({{"input_tensor", input}}, {"output_tensor"}, {}, &outputs);
if (status.ok()) { std::cout << "Inference successful!" << std::endl; }
return 0;}2. Dlib
A modern C++ library containing machine learning algorithms (SVM, neural networks, clustering). It stands out for its ease of use and good documentation.
#include <dlib/svm.h>#include <iostream>
int main() { using namespace dlib;
// Example: training an SVM classifier typedef matrix<double, 2, 1> sample_type; typedef radial_basis_kernel<sample_type> kernel_type;
std::vector<sample_type> samples; std::vector<double> labels;
// Adding data (conditional points) sample_type m1; m1 = 1, 1; samples.push_back(m1); labels.push_back(1); sample_type m2; m2 = -1, -1; samples.push_back(m2); labels.push_back(-1);
// Training svm_nu_trainer<kernel_type> trainer; trainer.set_kernel(kernel_type(0.1)); decision_function<kernel_type> df = trainer.train(samples, labels);
// Prediction sample_type test; test = 0.5, 0.5; std::cout << "Prediction: " << df(test) << std::endl;
return 0;}3. Shark-ML
A fast library with support for linear algebra