Skip to main content

humancompatible-train: a package for constrained machine learning

License Setup docs

The toolkit implements algorithms for constrained training of neural networks based on PyTorch, and inspired by PyTorch's API.

Table of Contents

  1. Basic installation instructions
  2. Using the toolkit
  3. Extending the toolkit
  4. License and terms of use
  5. References

humancompatible-train is still under active development! If you find bugs or have feature requests, please file a Github issue.

Installation

Use

pip install humancompatible-train

The only dependencies of this package are numpy and torch.

Using the toolkit

The toolkit implements algorithms for constrained training of neural networks based on PyTorch. For the documentation, please visit our Read the Docs page!

The algorithms are intended for use in tandem with classic PyTorch optimizers, calculating the Lagrangian and keeping track of the dual variables.

In general, your code using humancompatible-train would look something like this:

optimizer = torch.optim.Adam(model.parameters(), ...)
dual_optimizer = humancompatible.train.dual_optim.ALM(...)

for inputs, labels in dataloader:
  # evaluate objective
  outputs = model(inputs)
  loss = criterion(outputs, labels)
  # evaluate tensor of constraints
  constraints = <eval_your_constraints>(inputs, labels) 
  # evaluate lagrangian and update dual variables
  lgr = dual_optimizer.forward_update(loss, constraints)
  # backward pass and step
  lgr.backward()
  optimizer.step()
  optimizer.zero_grad()

The key difference is calculating the lagrangian using lgr = forward_update(loss, constraints), and then running lgr.backward() instead of loss.backward().

Our idea is to

  1. Deviate minimally from the usual PyTorch workflow
  2. Make different stochastic-constrained stochastic optimization algorithms nearly interchangable in the code.

Code examples

You are invited to check out our new API presented in notebooks in the examples folder.

The example notebooks have additional dependencies for data and plotting, such as fairret. To install those, run

pip install humancompatible-train[examples]

Extending the toolkit

To add a new algorithm, you can subclass the PyTorch Optimizer class and proceed following the API guideline presented above.

Reproducing the Benchmark

The code for benchmarking constrained regularization algorithms is available in the benchmark directory.

Installation instructions

  1. Create a virtual environment

bash (Linux)

python3.11 -m venv fairbenchenv
source fairbenchenv/bin/activate

cmd (Windows)

python -m venv fairbenchenv
fairbenchenv\Scripts\activate.bat
  1. Install from source.
git clone https://github.com/humancompatible/train.git
cd train
pip install -r requirements.txt
pip install .

Usage instructions

The benchmark offers two families of datasets: Folktables and Dutch, several pre-defined constraints, and several constrained optimization algorithms: ALM (smoothed and non-smoothed), SPBM, and Switching Subgradient; we are currently working to add Stochastic Ghost within the new framework as well.

To run an experiment, run:

python run_benchmark.py --dataset <DATASET> [folktables, dutch] --task <TYPE OF CONSTRAINT> [loss, equalized_odds_pairwise, equalized_odds_vec, weight_norm] --n_runs <NUMBER OF RUNS OF EACH METHOD> --n_epochs <NUMBER OF EPOCHS PER RUN>

The constraint options are:

  • loss: constraint(s) on the absolute difference between the classification loss on each group and the overall classification loss;
  • equalized_odds_pairwise: constraint(s) on the absolute difference between the positive rate between each group;
  • equalized_odds_vec: constraint on the Positive Rate of each group as defined by fairret.NormLoss;
  • weight_norm: constraint on the Frobenius norm of the weights and biases of each layer of the neural network.

The benchmarking code (all of which is contained in the benchmark directory) is easy to parse and extend with other datasets and constraints.

Future work

  • Add more algorithms
  • Add more examples from different fields where constrained training of DNNs is employed

References

If you use this work, we encourage you to cite our paper,

@inproceedings{kliachkin2026benchmarking,
  title={Benchmarking Stochastic Approximation Algorithms for Fairness-Constrained Training of Deep Neural Networks},
  author={Kliachkin, Andrii and Lep{\v{s}}ov{\'a}, Jana and Bareilles, Gilles and Mare{\v{c}}ek, Jakub},
  booktitle={14th International Conference on Learning Representations},
  url={https://arxiv.org/abs/2507.04033},
  year={2026}
}

@inproceedings{kliachkin2025humancompatible,
  title={humancompatible.train: Implementing Optimization Algorithms for Stochastically-Constrained Stochastic Optimization Problems},
  author={Kliachkin, Andrii and Lep{\v{s}}ov{\'a}, Jana and Bareilles, Gilles and Mare{\v{c}}ek, Jakub},
  booktitle={NeurIPS Workshop on Constrained Optimization for Machine Learning},
  year={2025}
}

[1] Ding, Hardt & Miller et al. (2021) Retiring Adult: New Datasets for Fair Machine Learning, Curran Associates, Inc..

[2] Facchinei & Kungurtsev (2023) Stochastic Approximation for Expectation Objective and Expectation Inequality-Constrained Nonconvex Optimization, arXiv.

[3] Huang, Zhang & Alacaoglu (2025) Stochastic Smoothed Primal-Dual Algorithms for Nonconvex Optimization with Linear Inequality Constraints, arXiv.

[4] Huang & Lin (2023) Oracle Complexity of Single-Loop Switching Subgradient Methods for Non-Smooth Weakly Convex Functional Constrained Optimization, Curran Associates Inc..

Metadata

Release files for humancompatible-train 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for humancompatible-train 0.4.0
File Size Uploaded Provenance Actions
humancompatible_train-0.4.0.tar.gz 89.3 kB Without provenance Details for humancompatible_train-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for humancompatible-train 0.4.0
File Interpreter ABI Platform Actions
humancompatible_train-0.4.0-py3-none-any.whl Python 3 none any Details for humancompatible_train-0.4.0-py3-none-any.whl

Total release size: 156.0 kB

Release files / humancompatible_train-0.4.0.tar.gz

Download URL humancompatible_train-0.4.0.tar.gz
Size 89.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1a0954ff9ed9f099083d6f6e669608a7dafea79fbdec7e8370ff8096d0d39c85
BLAKE2b-256 checksum
How to use checksums
da5566117e6d8ece45d1da49b77cac4198ad8b21512353a9a94e37cf08b1e16d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Release files / humancompatible_train-0.4.0-py3-none-any.whl

Download URL humancompatible_train-0.4.0-py3-none-any.whl
Size 66.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a1f66bd878de3fd2ffba57c9a02eddcfe33d74ec11adced1e1aa24df5e0ab884
BLAKE2b-256 checksum
How to use checksums
ef9d8a69d9d87d2c4a288ca19ead8002100e942ab2ef80bf1a4b28824a8cbad5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Security for humancompatible-train 0.4.0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page