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.2

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

Built distributions (wheels)

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

Total release size: 134.5 kB

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

Download URL humancompatible_train-0.4.2.0-py3-none-any.whl
Size 67.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d35ff5588c97dca1c6c27b817bb390be876586b4cb8652db2e43d2ad958e6eb
BLAKE2b-256 checksum
How to use checksums
cd3427ee71fa3e6630f09843b5b7e978f2251b04f3135b399cf715ad2f36a000
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.2-py3-none-any.whl

Download URL humancompatible_train-0.4.2-py3-none-any.whl
Size 67.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88dac4bdb792629805e098269f66c50ff54ba1b0ac77a2bf64b73672f3d73c3c
BLAKE2b-256 checksum
How to use checksums
c65d8137d244e3d4df84f8bffaa6a7059d17f639fe59a70a355b3d88a9b39f9c
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.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.

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