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.
Comments, issues, contributions, and collaborations are all welcomed!
TorchCP has implemented the following methods:
Year | Title | Venue | Code Link |
---|---|---|---|
2023 | Class-Conditional Conformal Prediction with Many Classes | NeurIPS | Link |
2023 | Conformal Prediction for Deep Classifier via Label Ranking | Arxiv | Link |
2021 | Learning Optimal Conformal Classifiers | ICLR | Link |
2020 | Uncertainty Sets for Image Classifiers using Conformal Prediction | ICLR | Link |
2020 | Classification with Valid and Adaptive Coverage | NeurIPS | Link |
2019 | Conformal Prediction Under Covariate Shift | NeurIPS | Link |
2016 | Least Ambiguous Set-Valued Classifiers with Bounded Error Levels | JASA | |
2013 | Applications of Class-Conditional Conformal Predictor in Multi-Class Classification | ICMLA |
Year | Title | Venue | Code Link |
---|---|---|---|
2021 | Adaptive Conformal Inference Under Distribution Shift | NeurIPS | Link |
2019 | Conformalized Quantile Regression | NeurIPS | Link |
2016 | Distribution-Free Predictive Inference For Regression | JASA | Link |
TorchCP is still under active development. We will add the following features/items down the road:
Year | Title | Venue | Code Link |
---|---|---|---|
2022 | Training Uncertainty-Aware Classifiers with Conformalized Deep Learning | NeurIPS | Link |
2022 | Adaptive Conformal Predictions for Time Series | ICML | Link |
2022 | Predictive Inference with Feature Conformal Prediction | ICLR | Link |
2022 | Conformal Prediction Sets with Limited False Positives | ICML | Link |
2021 | Optimized conformal classification using gradient descent approximation | Arxiv |
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
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, ClusterPredictor, 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_set = predictor.predict(test_instances)
print(predict_set)
#########################################
# 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.
The documentation webpage is on readthedocs https://torchcp.readthedocs.io/en/latest/index.html.
This project is licensed under the LGPL. The terms and conditions can be found in the LICENSE and LICENSE.GPL files.
We will release the technical report of TorchCP recently. If you find our repository useful for your research, please consider citing our paper:
@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}
}