Skip to main content

Monte Carlo Dropout-based uncertainty estimation for Transformers

Project description

Dropwise

Dropwise is a lightweight PyTorch/HuggingFace wrapper for performing Monte Carlo Dropout–based uncertainty estimation in Transformers. It enables confidence-aware decision making by revealing how certain a model is about its predictions — all with just a few lines of code.


🚀 Features

  • ✅ Enable dropout during inference for Bayesian-like uncertainty estimation
  • ✅ Compute predictive entropy, confidence, and per-class standard deviation
  • ✅ Works seamlessly with Hugging Face Transformers and PyTorch
  • ✅ Supports batch inference, CPU/GPU, and customizable num_passes
  • ✅ Cleanly packaged and extensible for research or production

🤖 Supported Models

Dropwise works with any AutoModelForSequenceClassification compatible model including:

  • bert-base-uncased, bert-large-uncased
  • roberta-base, roberta-large
  • deberta-v3-base, deberta-v2-xlarge
  • albert-base-v2, distilbert-base-uncased
  • Any custom fine-tuned Hugging Face model for classification

⚠️ Note: The model must contain dropout layers (most pretrained transformers do).


📦 Installation

pip install dropwise  # coming soon to PyPI

Or install locally for development:

git clone https://github.com/yourusername/dropwise.git
cd dropwise
pip install -e .

🧠 Example Usage

from transformers import AutoTokenizer, AutoModelForSequenceClassification
from dropwise import DropwisePredictor

model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

predictor = DropwisePredictor(model, tokenizer, num_passes=30)
result = predictor.predict("The performance was unexpectedly good!")

print("Predicted class:", result['predicted_class'].item())
print("Entropy:", result['entropy'].item())
print("Confidence:", result['probs'].max().item())
print("Per-class std dev:", result['std_dev'])

📊 Output Dictionary

  • predicted_class: index of most probable class
  • entropy: predictive entropy (higher = less confident)
  • std_dev: standard deviation across MC passes for each class
  • mean_logits: average logits before softmax
  • probs: softmax probabilities

🧠 Why Dropwise?

Unlike deterministic predictions, Dropwise estimates uncertainty via stochastic forward passes — enabling confidence-aware applications.

Use cases include:

  • Filtering low-confidence predictions
  • Active learning and semi-supervised setups
  • Detecting ambiguous, adversarial, or out-of-distribution inputs
  • Enhancing interpretability and robustness in real-world deployment

📂 Folder Structure

dropwise/
├── dropwise/
│   ├── __init__.py
│   └── predictor.py
├── tests/
│   ├── __init__.py
│   └── test_predictor.py
├── setup.py
├── README.md
├── LICENSE

🧪 Running Tests

python tests/test_predictor.py

📝 License

MIT License


Made with ❤️ for uncertainty-aware, explainable AI. [Coming soon to PyPI.]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dropwise-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dropwise-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file dropwise-0.1.0.tar.gz.

File metadata

  • Download URL: dropwise-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for dropwise-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b1f3949ce35369fe71438bc420024649f5224e1b86282785d5489004500b35d1
MD5 1aa1336ddfd8d16a88c3e02cf5991660
BLAKE2b-256 755000dbfb1ec90a3577954473c0671e79c97aebe440d6c2168584ee6d50c3b8

See more details on using hashes here.

File details

Details for the file dropwise-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dropwise-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for dropwise-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3777a349e39755511774289c8a13bf8ca146d305666a0bd31006a6981f04873c
MD5 184c3ccbd8eae93f91ac191951bb2ecc
BLAKE2b-256 4f33017894b48dc8d75439e41808d546624afc90288028544b6c51376ac77b79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page