Machine Learning in C++: Power and Performance
Despite Python's dominance in Data Science, C++ remains an indispensable language for production systems where processing speed, low memory consumption, and operation under resource-constrained conditions (embedded systems, robotics, high-load servers) are critical. Libraries for machine learning in C++ provide ready-made algorithms optimized for modern processor architectures and allow integrating ML models directly into code without an interpreter layer.
In this article, we will review key C++ libraries for machine learning, their strengths, and provide practical code examples.
1. Shark — A Flexible Library for Neural Networks and Optimization
Shark is one of the oldest and most well-documented C++ libraries for machine learning. It includes implementations of:
- Multilayer perceptrons and convolutional neural networks (CNNs);
- Optimization methods (gradient descent, L-BFGS, CMA-ES);
- Clustering algorithms (k-means, DBSCAN);
- Linear models and decision trees.
Shark uses Eigen for linear algebra, ensuring high performance. Example of training linear regression:
#include <shark/Data/Dataset.h>#include <shark/Algorithms/Trainers/LinearRegression.h>
int main() { // Create data: y = 2*x + 1 + noise shark::RegressionDataset data = generateData(100); shark::LinearRegression trainer; shark::LinearModel<shark::RealVector> model; trainer.train(model, data);
std::cout << "Coefficients: " << model.parameterVector() << std::endl; return 0;}Pros: excellent documentation, GPU support (via OpenCL), active community.
2. Dlib — A Versatile Tool for Computer Vision and ML
Dlib is known primarily for face recognition and object detection algorithms, but its machine learning module is equally powerful. It includes:
- Support vector machines (SVM) with various kernels;
- Random forests and gradient boosting;
- Deep neural networks (with cuDNN support);
- Dimensionality reduction algorithms (t-SNE, PCA).
Example of training an SVM classifier:
#include <dlib/svm.h>
int main() { typedef dlib::matrix<double, 2, 1> sample_type; std::vector<sample_type> samples; std::vector<double> labels;
// Fill data (two classes) samples.push_back({1.0, 2.0}); labels.push_back(1); samples.push_back({3.0, 4.0}); labels.push_back(-1);
dlib::svm_c_linear_trainer<dlib::linear_kernel<sample_type>> trainer; trainer.set_c(10.0); auto decision_func = trainer.train(samples, labels);
std::cout << "Prediction for (0,0): " << decision_func({0.0, 0.0}) << std::endl; return 0;}Pros: unified API for all algorithms, excellent integration with OpenCV, cross-platform.
3. Shogun — A Scientific Library with a Focus on Kernel Methods
Shogun is a powerful library designed primarily for researchers. Its features include:
- Over 60 implementations of kernel functions (RBF, polynomial, string);
- Support for structured learning (HMM, CRF);
- Interfaces for Python, R, Java, Octave;
- Built-in visualization tools.
Example of using Gaussian process regression:
#include <shogun/regression/GaussianProcessRegression.h>#include <shogun/kernel/GaussianKernel.h>
int main() { auto features = some_real_features(); // your data auto labels = some_regression_labels(); auto kernel = std::make Also in library
gRPC-Go for Go: Complete Guide with Examples
Rate Limiting in C++: Libraries and Implementation for API Protection
GraphQL Java — server library for implementing GraphQL API in Java