Home

Awesome

TorchCP is a Python toolbox for conformal prediction research on deep learning models, using PyTorch. Specifically, this toolbox has implemented some representative methods (including posthoc and training methods) for classification and regression tasks. We build the framework of TorchCP based on AdverTorch. This codebase is still under construction and maintained by Hongxin Wei's research group at SUSTech. Comments, issues, contributions, and collaborations are all welcomed!

Overview

TorchCP has implemented the following methods:

Classification

YearTitleVenueCode LinkImplementation
2023Class-Conditional Conformal Prediction with Many ClassesNeurIPSLinkclassification.predictors.cluster
2023Conformal Prediction for Deep Classifier via Label RankingArxivLinkclassification.scores.saps
2021Learning Optimal Conformal ClassifiersICLRLinkclassification.loss.conftr
2020Uncertainty Sets for Image Classifiers using Conformal PredictionICLRLinkclassification.scores.raps
2020Classification with Valid and Adaptive CoverageNeurIPSLinkclassification.scores.aps
2019Conformal Prediction Under Covariate ShiftNeurIPSLinkclassification.predictors.weight
2016Least Ambiguous Set-Valued Classifiers with Bounded Error LevelsJASAclassification.scores.thr
2015Bias reduction through conditional conformal predictionIntell. Data Anal.classification.scores.margin
2013Applications of Class-Conditional Conformal Predictor in Multi-Class ClassificationICMLAclassification.predictors.classwise

Regression

YearTitleVenueCode LinkImplementation
2023Conformal Prediction via Regression-as-ClassificationRegML @ NeurIPS 2023linkregression.predictors.r2ccp
2021Adaptive Conformal Inference Under Distribution ShiftNeurIPSLinkregression.predictors.aci
2019Conformalized Quantile RegressionNeurIPSLinkregression.predictors.cqr
2016Distribution-Free Predictive Inference For RegressionJASALinkregression.predictors.split

TODO

TorchCP is still under active development. We will add the following features/items down the road:

YearTitleVenueCode Link
2022Training Uncertainty-Aware Classifiers with Conformalized Deep LearningNeurIPSLink
2022Adaptive Conformal Predictions for Time SeriesICMLLink
2022Predictive Inference with Feature Conformal PredictionICLRLink
2022Conformal Prediction Sets with Limited False PositivesICMLLink
2021Optimized conformal classification using gradient descent approximationArxiv

Installation

TorchCP is developed with Python 3.9 and PyTorch 2.0.1. To install TorchCP, simply run

pip install torchcp

To install from TestPyPI server, run

pip install --index-url https://test.pypi.org/simple/ --no-deps torchcp

Examples

Here, we provide a simple example for a classification task, with THR score and SplitPredictor.

from torchcp.classification.scores import THR
from torchcp.classification.predictors import SplitPredictor

# Preparing a calibration data and a test data.
cal_dataloader = ...
test_dataloader = ...
# Preparing a pytorch model
model = ...

model.eval()

# Options of score function: THR, APS, SAPS, RAPS
# Define a conformal prediction algorithm. Optional: SplitPredictor, ClusteredPredictor, ClassWisePredictor
predictor = SplitPredictor(score_function=THR(), model=model)

# Calibrating the predictor with significance level as 0.1
predictor.calibrate(cal_dataloader, alpha=0.1)

#########################################
# Predicting for test instances
########################################
test_instances = ...
predict_sets = predictor.predict(test_instances)
print(predict_sets)

#########################################
# Evaluating the coverage rate and average set size on a given dataset.
########################################
result_dict = predictor.evaluate(test_dataloader)
print(result_dict["Coverage_rate"], result_dict["Average_size"])

You may find more tutorials in examples folder.

Documentation

The documentation webpage is on readthedocs https://torchcp.readthedocs.io/en/latest/index.html.

License

This project is licensed under the LGPL. The terms and conditions can be found in the LICENSE and LICENSE.GPL files.

Citation

If you find our repository useful for your research, please consider citing the following technical report:

@misc{wei2024torchcp,
      title={TorchCP: A Library for Conformal Prediction based on PyTorch}, 
      author={Hongxin Wei and Jianguo Huang},
      year={2024},
      eprint={2402.12683},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}

We welcome you to cite the following works:

@article{huang2023conformal,
  title={Conformal Prediction for Deep Classifier via Label Ranking},
  author={Huang, Jianguo and Xi, Huajun and Zhang, Linjun and Yao, Huaxiu and Qiu, Yue and Wei, Hongxin},
  journal={arXiv preprint arXiv:2310.06430},
  year={2023}
}

@article{xi2024does,
  title={Does Confidence Calibration Help Conformal Prediction?},
  author={Xi, Huajun and Huang, Jianguo and Feng, Lei and Wei, Hongxin},
  journal={arXiv preprint arXiv:2402.04344},
  year={2024}
}

Contributors