Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Rapidsai Cuml Linear SVM

From Leeroopedia


Knowledge Sources
Domains Machine_Learning, Support_Vector_Machines
Last Updated 2026-02-08 12:00 GMT

Overview

Provides a GPU-accelerated linear Support Vector Machine (SVM) implementation for classification and regression, using a quasi-Newton (L-BFGS) solver for efficient optimization of linear kernels.

Description

The linear.hpp header declares the linear SVM API in the ML::SVM::linear namespace. Unlike the kernel SVM which uses the SMO algorithm, the linear SVM uses a quasi-Newton (L-BFGS) optimizer, which is significantly faster for linear kernels.

Parameter Struct (Params):

The Params struct provides full control over the linear SVM with:

  • Penalty: L1 or L2 regularization of the weights.
  • Loss: HINGE, SQUARED_HINGE (classification), EPSILON_INSENSITIVE, or SQUARED_EPSILON_INSENSITIVE (regression/SVR).
  • Solver settings: max_iter, linesearch_max_iter, lbfgs_memory, C (regularization constant), grad_tol, change_tol, epsilon (SVR sensitivity).
  • Intercept control: fit_intercept and penalized_intercept.

Functions:

  • fit: Trains a linear SVM model. Supports multiclass classification (one-vs-rest), binary classification, and regression. Optionally fits probability calibration scales. Returns the number of iterations.
  • computeProbabilities: Converts decision function scores to calibrated probabilities using the fitted probability scales (Platt scaling).

Both functions are templated on the data type T (float or double).

Usage

Use the linear SVM when you need a fast linear classifier or regressor with SVM-style loss functions. It is appropriate for large-scale problems where a kernel SVM would be too slow. The L-BFGS optimizer is well-suited for high-dimensional data with linear separability. Use computeProbabilities when calibrated probability outputs are needed instead of raw decision function values.

Code Reference

Source Location

Signature

namespace ML {
namespace SVM {
namespace linear {

struct Params {
  enum Penalty { L1, L2 };
  enum Loss { HINGE, SQUARED_HINGE, EPSILON_INSENSITIVE, SQUARED_EPSILON_INSENSITIVE };

  Penalty penalty = L2;
  Loss loss = HINGE;
  bool fit_intercept = true;
  bool penalized_intercept = false;
  int max_iter = 1000;
  int linesearch_max_iter = 100;
  int lbfgs_memory = 5;
  rapids_logger::level_enum verbose = rapids_logger::level_enum::off;
  double C = 1.0;
  double grad_tol = 0.0001;
  double change_tol = 0.00001;
  double epsilon = 0.0;
};

template <typename T>
int fit(const raft::handle_t& handle,
        const Params& params,
        const std::size_t nRows,
        const std::size_t nCols,
        const int nClasses,
        const T* classes,
        const T* X,
        const T* y,
        const T* sampleWeight,
        T* w,
        T* probScale);

template <typename T>
void computeProbabilities(const raft::handle_t& handle,
                          const std::size_t nRows,
                          const int nClasses,
                          const T* probScale,
                          T* scores,
                          T* out);

} // namespace linear
} // namespace SVM
} // namespace ML

Import

#include <cuml/svm/linear.hpp>

I/O Contract

Inputs

fit

Name Type Required Description
handle const raft::handle_t& Yes cuML handle for GPU resources
params const Params& Yes Model hyperparameters (penalty, loss, C, etc.)
nRows std::size_t Yes Number of training samples
nCols std::size_t Yes Number of features
nClasses int Yes Number of unique classes, or 0 for regression
classes const T* Yes Device pointer to unique class labels [nClasses], or nullptr for regression
X const T* Yes Device pointer to training data [nRows x nCols], F-contiguous (column-major)
y const T* Yes Device pointer to target values [nRows]
sampleWeight const T* No Device pointer to sample weights [nRows], or nullptr for uniform weights
probScale T* No Output probability scales [nClasses x 2], or nullptr to skip probability calibration

computeProbabilities

Name Type Required Description
handle const raft::handle_t& Yes cuML handle
nRows std::size_t Yes Number of input samples
nClasses int Yes Number of classes
probScale const T* Yes Probability scales from fit [nClasses x 2], F-contiguous
scores T* Yes Decision function scores [nRows x nClasses], C-contiguous; mutated in-place

Outputs

Name Type Description
w (fit) T* Fitted weight matrix [nCoefs x nCols] or [nCoefs+1 x nCols+1] if fit_intercept=true, F-contiguous
probScale (fit) T* Fitted probability calibration scales [nClasses x 2]
return value (fit) int Maximum number of iterations run across all classes
out (computeProbabilities) T* Computed probabilities [nRows x nClasses], C-contiguous

Usage Examples

#include <cuml/svm/linear.hpp>

raft::handle_t handle;

// Configure linear SVM for classification
ML::SVM::linear::Params params;
params.penalty = ML::SVM::linear::Params::L2;
params.loss = ML::SVM::linear::Params::SQUARED_HINGE;
params.C = 1.0;
params.fit_intercept = true;
params.max_iter = 1000;

std::size_t nRows = 10000;
std::size_t nCols = 100;
int nClasses = 3;

float* d_X;            // [nRows x nCols], column-major
float* d_y;            // [nRows]
float* d_classes;      // [nClasses] unique class labels
float* d_weights;      // output: [(nClasses) x (nCols + 1)]
float* d_probScale;    // output: [nClasses x 2]

int n_iter = ML::SVM::linear::fit<float>(
    handle, params, nRows, nCols, nClasses,
    d_classes, d_X, d_y, nullptr, d_weights, d_probScale);

// Compute calibrated probabilities from scores
float* d_scores;  // [nRows x nClasses]
float* d_probs;   // [nRows x nClasses]

ML::SVM::linear::computeProbabilities<float>(
    handle, nRows, nClasses, d_probScale, d_scores, d_probs);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment