Skip to main content

PyTorch-based package for constrained training of neural networks

Project description

humancompatible-train: a package for constrained machine learning

License Setup

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.

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

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

humancompatible_train-0.3.2.tar.gz (52.1 kB view details)

Uploaded Source

Built Distribution

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

humancompatible_train-0.3.2-py3-none-any.whl (65.5 kB view details)

Uploaded Python 3

File details

Details for the file humancompatible_train-0.3.2.tar.gz.

File metadata

  • Download URL: humancompatible_train-0.3.2.tar.gz
  • Upload date:
  • Size: 52.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for humancompatible_train-0.3.2.tar.gz
Algorithm Hash digest
SHA256 2978d1ff9649467426d534022bf726fee1d55041faf8d817055513facc8a1c89
MD5 8e515574be207b0f2e4cc07685ec6d93
BLAKE2b-256 6bac21e29f83d4a5a391a2134c1a89c66eb07a00438243f767ac85295b4f4b8c

See more details on using hashes here.

File details

Details for the file humancompatible_train-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for humancompatible_train-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f5b332e47b0d4943419ff5e8f0f7943ffbaef8c7da1d7e5d06c6ac8c14338f18
MD5 4883030bf601e3e3e16be9508f832409
BLAKE2b-256 8e51f7a5d26084a7f7f5a3ec5cf552701b8a318f4991ec861c2a68d2936a8f2e

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