Skip to content

Repository files navigation

ReHLine-Python: Efficient Solver for ERM with PLQ Loss and Linear Constraints

PyPI versionLicense: MITDocumentationPaperDownloadsCI Tests

Fast, scalable, and scikit-learn compatible optimization for machine learning

ReHLine-Python is the official Python implementation of ReHLine, a powerful solver for large-scale empirical risk minimization (ERM) problems with convex piecewise linear-quadratic (PLQ) loss functions and linear constraints. Built with high-performance C++ core and seamless Python integration, ReHLine delivers exceptional speed while maintaining ease of use.

See more details in the ReHLine documentation.

✨ Key Features

  • 🚀 Blazing Fast: Linear computational complexity per iteration, scales to millions of samples
  • 🎯 Versatile: Supports any convex PLQ loss (hinge, check, Huber, and more)
  • 🔒 Constrained Optimization: Handle linear equality and inequality constraints
  • 📊 Scikit-Learn Compatible: Drop-in replacement with GridSearchCV, Pipeline support
  • 🐍 Pythonic API: Both low-level and high-level interfaces for flexibility

📦 Installation

Quick Install

pip install rehline

Development Install

For contributors and developers:

git clone https://github.com/softmin/ReHLine-python.git
cd ReHLine-python
pip install -e ".[dev]"

To run tests:

pytest tests/

🚀 Quick Start

Scikit-Learn Style API (Recommended)

Open In Colab

ReHLine provides plq_Ridge_Classifier and plq_Ridge_Regressor that work seamlessly with scikit-learn:

fromrehlineimportplq_Ridge_Classifierfromsklearn.datasetsimportmake_classificationfromsklearn.model_selectionimporttrain_test_split, GridSearchCVfromsklearn.pipelineimportPipelinefromsklearn.preprocessingimportStandardScaler# Generate datasetX, y=make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test=train_test_split(X, y, test_size=0.2)
# Simple usageclf=plq_Ridge_Classifier(loss={'name': 'svm'}, C=1.0)
clf.fit(X_train, y_train)
print(f"Accuracy: {clf.score(X_test, y_test):.3f}")
# Use in Pipelinepipeline=Pipeline([
('scaler', StandardScaler()),
('classifier', plq_Ridge_Classifier(loss={'name': 'svm'}))
])
pipeline.fit(X_train, y_train)
# Hyperparameter tuning with GridSearchCVparam_grid= {
'C': [0.1, 1.0, 10.0],
'loss': [{'name': 'svm'}, {'name': 'sSVM'}]
}
grid_search=GridSearchCV(plq_Ridge_Classifier(loss={"name": "svm"}), param_grid, cv=5)
grid_search.fit(X_train, y_train)
print(f"Best params: {grid_search.best_params_}")

See more details in ReHLine with Scikit-Learn.

Low-Level API for Custom Problems

fromrehlineimportReHLineimportnumpyasnp# Generate sample datanp.random.seed(42)
X=np.random.randn(100, 5)
y=np.random.choice([-1, 1], size=100)
n, d=X.shapeC=1.0# Define custom PLQ loss parametersclf=ReHLine()
# Set custom U, V matrices for ReLU loss# and S, T, tau for ReHU loss## Uclf._U=-(C*y).reshape(1,-1)
## Vclf._V= (C*np.ones(n)).reshape(1,-1)
# Set custom linear constraints A*beta + b >= 0X_sen=X[:,0]
tol_sen=0.1clf._A=np.repeat([X_sen @ X], repeats=[2], axis=0) /nclf._A[1] =-clf._A[1]
clf.fit(X)

See more detailed in Manual ReHLine Formulation.

🎯 Use Cases

ReHLine excels at solving a wide range of machine learning problems:

ProblemDescriptionKey Benefits
Support Vector MachinesBinary and multi-class classification100-400× faster than CVXPY solvers
Fair Machine LearningClassification with fairness constraintsHandles demographic parity efficiently
Quantile RegressionRobust conditional quantile estimation2800× faster than general solvers
Huber RegressionOutlier-resistant regressionSuperior to specialized solvers
Sparse LearningFeature selection with L1 regularizationScales to high dimensions
Custom OptimizationAny PLQ loss with linear constraintsFlexible framework for research

⚡ Performance Benchmarks

ReHLine delivers exceptional speed compared to state-of-the-art solvers. Here are speed-up factors on real-world datasets:

Speed Comparison vs. Popular Solvers

Taskvs. ECOSvs. MOSEKvs. SCSvs. Specialized Solvers
SVM415× faster (failed)340× faster4.5× vs. LIBLINEAR
Fair SVM273× faster100× faster252× faster vs. DCCP (failed)
Quantile Regression2843× faster (failed) (failed)
Huber Regression (failed)452× faster (failed)2.4× vs. hqreg
Smoothed SVM1.6-2.3× vs. SAGA/SAG/SDCA/SVRG

Note: "∞" indicates the competing solver failed to produce a valid solution or exceeded time limits. Results from NeurIPS 2023 paper.

Reproducible Benchmarks (powered by benchopt)

All benchmarks are reproducible via benchopt at our ReHLine-benchmark repository.

ProblemBenchmark CodeInteractive Results
SVMCode📊 View
Smoothed SVMCode📊 View
Fair SVMCode📊 View
Quantile RegressionCode📊 View
Huber RegressionCode📊 View

🤝 Contributing

We welcome contributions! Whether it's bug reports, feature requests, or code contributions:

📚 Citation

If you use ReHLine in your research, please cite our NeurIPS 2023 paper:

@inproceedings{dai2023rehline,
title={ReHLine: Regularized Composite ReLU-ReHU Loss Minimization with Linear Computation and Linear Convergence},
author={Dai, Ben and Qiu, Yixuan},
booktitle={Thirty-seventh Conference on Neural Information Processing Systems},
year={2023}
}

🔗 ReHLine Ecosystem

🏠 Core Projects

📊 Resources

About

[NeurIPS 2023] Regularized Composite ReLU-ReHU Loss Minimization with Linear Computation and Linear Convergence

Topics

Resources

Contributing

Security policy

Stars

18 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages