This release is a pre-release and may not be stable for production use.
🚀 Getting Started - 📚 Documentation - 💻 User guide
PyC is a library built upon
PyTorch and
PyTorch Lightning to easily implement interpretable and causally transparent deep learning models.
The library provides primitives for annotated tensors, interpretable layers, interventions, interpretable probabilistic graphical models, and APIs for running experiments at scale.
The name of the library stands for both
- PyTorch Concepts: as concepts are essential building blocks for interpretable deep learning.
- $P(y|C)$: as the main purpose of the library is to support sound probabilistic modeling of the conditional distribution of targets $y$ given concepts $C$.
Quick Start
Install PyC from PyPI:
pip install --pre "pytorch-concepts[data]"
Use pip install --pre pytorch-concepts for core-only (no data dependencies), or see full installation options for conda setup.
Design Principles
A few basic elements let you name concepts, compute them, train them and act on them. Here is what each one lets you do:
|
Annotations name the concepts, with their type and size. |
Annotated tensors bind values to concept names. |
import torch_concepts as pyc
ann = pyc.Annotations(
labels=["red", "shape", "size"],
types=[
"binary",
"categorical",
"continuous",
],
cardinalities=[1, 3, 1],
)
|
x = torch.randn(8, 5)
c = pyc.AnnotatedTensor(x, ann)
c["red"] # slice by concept
c.binary() # slice by type
|
|
Concept layers map embeddings and/or concepts to concepts. |
Concept losses score each concept with the loss of its type. |
from torch_concepts.nn import (
LinearEmbeddingToConcept,
)
layer = LinearEmbeddingToConcept(
in_embeddings=16,
out_concepts=ann,
)
emb = torch.randn(8, 16)
out = layer.annotate(layer(emb))
|
from torch import nn
from torch_concepts.nn import ConceptLoss
loss_fn = ConceptLoss(
binary=nn.BCEWithLogitsLoss(),
categorical=nn.CrossEntropyLoss(),
continuous=nn.MSELoss(),
)
loss = loss_fn(out, target)
|
|
Interventions edit concepts inside a |
|
from torch_concepts.nn import DoIntervention, UniformPolicy, intervention
with intervention(layer, DoIntervention(1.0), UniformPolicy(), ["red"]):
out = layer(emb) # "red" is set to 1.0
|
|
The quickstart trains a first model end to end, and the user guide covers the rest of PyC.
PyC Software Stack
The library is organized to be modular and accessible at different levels of abstraction:
Conceptarium (No-code API): applications and benchmarking. These APIs allow to easily run large-scale experiments by interfacing only with configuration files. Built on top of
Hydra and
WandB.
- High-level APIs: use out-of-the-box models. These APIs allow to instantiate models with 1 line of code. Models are available both as plain
PyTorch modules (implementing
forward, allowing custom training loops) and asPyTorch Lightning modules.
- Mid-level APIs: interpretable probabilistic graphical models. These APIs allow to define variables (concepts and embeddings), connect them via conditional distributions parametrized by interpretable layers, and perform probabilistic inference on the resulting graphical model.
- Low-level APIs: interpretable layers. These APIs allow to build architectures from basic interpretable layers in a plain
PyTorch-like interface. These APIs also include annotated tensors, interventions, metrics, losses, and datasets.
Contributing
Contributions are welcome! Please check our contributing guidelines to get started.
Thanks to all contributors! 🧡
External Contributors
- Sonia Laguna, ETH Zurich (CH).
- Moritz Vandenhirtz, ETH Zurich (CH).
Cite this Library
If you found this library useful for your research article, blog post, or product, we would be grateful if you would cite it using the following bibtex entry:
@software{pycteam2025concept,
author = {De Felice, Giovanni and Barbiero, Pietro and De Santis, Francesco and Gabrielli, Edoardo and Casanova Flores, Arianna and Ciravegna, Gabriele and Debot, David and Espinosa Zarlenga, Mateo and Jamnik, Mateja and Marra, Giuseppe},
license = {Apache 2.0},
month = {3},
title = {{PyTorch Concepts}},
url = {https://github.com/pyc-team/pytorch_concepts},
year = {2025}
}
Reference authors: Giovanni De Felice, Pietro Barbiero, and Mateo Espinosa Zarlenga.
Funding
This project is supported by the following organizations:
Metadata
Release files for pytorch-concepts 1.0.0a7
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 | |
|---|---|---|---|---|
| pytorch_concepts-1.0.0a7.tar.gz | 415.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytorch_concepts-1.0.0a7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 914.7 kB
Release files / pytorch_concepts-1.0.0a7.tar.gz
| Download URL | pytorch_concepts-1.0.0a7.tar.gz |
|---|---|
| Size | 415.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7b0e35721188d7c09fe6c54b97fbe02cf1d4d4c1fbee98911b0b692d031b7ba8
|
|
BLAKE2b-256 checksum How to use checksums |
d0c80a168bc2750342a2ff3316a056360256d27e23898591d7335fc807c4ddd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Release files / pytorch_concepts-1.0.0a7-py3-none-any.whl
| Download URL | pytorch_concepts-1.0.0a7-py3-none-any.whl |
|---|---|
| Size | 499.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bd11fc090403bd7a73b263a0543fce00066b5c9bebf19948c6a7ca5881e11812
|
|
BLAKE2b-256 checksum How to use checksums |
54c49c66ce74df6f03501f4e8ab35fad03474bc787b1b36314596eb6b0de4ced
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Security for pytorch-concepts 1.0.0a7
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.