Docs · Machine learning
svm: linear support vector machine.
A linear support vector machine trained with hinge loss by full-batch gradient descent, in the style of Spark MLlib's LinearSVC but without regularisation. Labels are 0/1 in the data and mapped to -1/+1 inside the kernel.
- Module
- ml_svm
- Header
- clio_ml/glm.h
- Rows
- features, then 0/1 label
What it does
The GLM engine with GlmKind::kSvm: one paged pass per iteration sums the hinge-loss subgradient, then one fixed-step update. No regularisation and no early stop. The recorded loss is the mean hinge loss before each update.
Rows
n_features feature columns followed by a 0/1 label. clio_ml.COLUMNS["svm"] is 1.
Python
python
import numpy as np, clio_ml as cm
rng = np.random.default_rng(1)
X = rng.normal(size=(4096, 7)).astype(np.float32)
y = (X[:, 0] + X[:, 1] > 0).astype(np.float32)
data = np.column_stack([X, y]).astype(np.float32)
r = cm.fit_svm(data, iters=20, learning_rate=0.1)
margin = cm.predict("svm", r, data[0, :7]) # signed linear value
print(r.weights, margin)fit_svm(X, **options)calls the GLM engine withGlmKind.svm.- Options (
GlmOptions):iters(100),learning_rate(0.1),threads(256). - Result
GlmResult:weights(coefficients, then intercept),loss_history,stats. predict("svm", r, x)returnsGlmPredict(GlmKind.svm, ...), the linear valuew . x + b; its sign is the class.
C++
cpp
GlmResult FitLinearSvm(Dataset &ds, const GlmOptions &opt); // FitGlm(kSvm, ...)
GlmResult FitGlm(GlmKind kind, Dataset &ds, const GlmOptions &opt);
GlmResult FitGlmPlain(GlmKind kind, const float *data, u64 n_rows, u32 n_features,
u64 elems_per_page, u32 nblocks, const GlmOptions &opt);
float GlmPredict(GlmKind kind, const std::vector<float> &w, const float *x);GlmOptions and GlmResult are described on the linear regression page. The ChiMod is module ml_svm.
Related: logistic regression, multinomial logistic regression.