Calvera
Calvera is a Python library offering a collection of neural multi-armed bandit algorithms, designed to integrate seamlessly with PyTorch and PyTorch Lightning. Whether you're exploring contextual bandits or developing new strategies, Calvera provides a flexible, easy-to-use interface. You can bring your own neural networks and datasets while Calvera focuses on the implementation of the bandit algorithms.
Features
-
Multi-Armed Bandit Algorithms:
- (Approximate + Standard) Linear Thompson Sampling
- (Approximate + Standard) Linear UCB
- Neural Linear
- Neural Thompson Sampling
- Neural UCB
-
Customizable Selectors:
- ArgMaxSelector: Chooses the arm with the highest score.
- EpsilonGreedySelector: Chooses the best arm with probability
1-epsilonor a random arm with probabilityepsilon. - TopKSelector: Selects the top
karms with the highest scores. - EpsilonGreedyTopKSelector: Selects the top
karms with probability1-epsilonorkrandom arms with probabilityepsilon.
-
Integration:
- Built on top of PyTorch Lightning for training and inference.
- Minimal adjustments needed to plug into your existing workflow.
Installation
Calvera is available on PyPI. Install it via pip:
pip install calvera
This installs the necessary dependencies for the base library. If you want to use parts of the benchmark subpackage we recommend installing the optional dependencies as well:
pip install calvera[benchmark]
For development you can install the development dependencies via:
pip install calvera[dev]
Quick Start
Below is a simple example using a Linear Thompson Sampling bandit:
import torch
import lightning as pl
from calvera.bandits import LinearTSBandit
# 1. Create a bandit for a linear model with 128 features.
N_FEATURES = 128
bandit = LinearTSBandit(n_features=N_FEATURES)
# 2. Generate sample data (batch_size, n_actions, n_features) and perform inference.
data = torch.randn(100, 1, N_FEATURES)
chosen_arms_one_hot, probabilities = bandit(data)
chosen_arms = chosen_arms_one_hot.argmax(dim=1)
# 3. Retrieve rewards for the chosen arms.
rewards = torch.randn(100, 1)
# 4. Add the data to the bandit.
chosen_contextualized_actions = data[:, :, chosen_arms]
bandit.record_feedback(chosen_contextualized_actions, rewards)
# 5. Train the bandit.
trainer = pl.Trainer(
max_epochs=1,
enable_progress_bar=False,
enable_model_summary=False,
accelerator=accelerator,
)
trainer.fit(bandit)
# (6. Repeat the process as needed)
For more detailed examples, see the examples page in the documentation.
Documentation
-
Bandits: Each bandit is implemented as a PyTorch Lightning Module with
forward()for inference andtraining_step()for training. -
Buffers: Data is managed via buffers that subclass AbstractBanditDataBuffer.
-
Selectors: Easily customize your arm selection strategy by using or extending the provided selectors.
Benchmarks & Experimental Results
Detailed benchmarks, datasets, and experimental results are available in the extended documentation. The configuration and even more specific results can be found in ./experiments under the specific sub-directories.
Contributing
Contributions are welcome! For guidelines on how to contribute, please refer to our CONTRIBUTING.md.
License
Calvera is licensed under the MIT License. See the LICENSE file for details.
Contact
For questions or feedback, please reach out to one of the authors:
-
Philipp Kolbe
-
Robert Weeke
-
Parisa Shahabinejad
Metadata
Release files for calvera 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| calvera-1.0.2.tar.gz | 66.3 kB | Without provenance | Details for calvera-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| calvera-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for calvera-1.0.2-py3-none-any.whl |
Total release size: 153.0 kB
Release files / calvera-1.0.2.tar.gz
| Download URL | calvera-1.0.2.tar.gz |
|---|---|
| Size | 66.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ccd36fb8e851d6c43471447af903eb009c65b430731599fb6396a72ae3e01c68
|
|
BLAKE2b-256 checksum How to use checksums |
37b33f1b2a4e1c2b6473d01b42e41a405d56502077ceb442b93d5204b306a3a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.5
|
Release files / calvera-1.0.2-py3-none-any.whl
| Download URL | calvera-1.0.2-py3-none-any.whl |
|---|---|
| Size | 86.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f8467004651ff86866c5a5ccdcc06df72a717946a7d86484af40626045e8cfa2
|
|
BLAKE2b-256 checksum How to use checksums |
eaaeb097b53c923e5f9ac6b216ac8b04f5dcd712a2cec8b2895b029839928220
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.5
|
Security for calvera 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.