Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
aaaf070
update default config
andrewklayk Sep 2, 2025
09ee23d
updates
andrewklayk Sep 9, 2025
ba7c480
add pde and adam
andrewklayk Sep 15, 2025
9ba8aa0
update pde example
andrewklayk Sep 15, 2025
c6755cc
Merge pull request #5 from humancompatible/main
andrewklayk Sep 15, 2025
7d405d5
include regularization in pde exampel
andrewklayk Sep 15, 2025
06343b1
add sslalm-adam w/ tests, update version
andrewklayk Sep 17, 2025
3a0d5b5
update pyproject dependencies & version, minor example update
andrewklayk Sep 17, 2025
dc697d1
Merge branch 'main' of https://github.com/andrewklayk/humancompatible…
andrewklayk Sep 17, 2025
1c97f1b
update readme
andrewklayk Sep 17, 2025
4bbaa73
bugfix in benchmark, update setup.yml
andrewklayk Sep 17, 2025
3b539c0
macos certificate
andrewklayk Sep 17, 2025
70abd2e
update structure to remove unneeded dependencies from test runs
andrewklayk Sep 18, 2025
6cb3d7a
update setup workflow
andrewklayk Sep 18, 2025
1248501
fix import
andrewklayk Sep 18, 2025
9ee0aa9
remove benchmark from macos workflow
andrewklayk Sep 18, 2025
4379144
cooper graph example, minor refactor
andrewklayk Sep 19, 2025
7f54d3e
update benchmark
andrewklayk Sep 30, 2025
fe4b30d
update pyproject.toml
andrewklayk Sep 30, 2025
71ec4e0
update examples
andrewklayk Oct 1, 2025
4f0d5cf
add saving objective gradient to sslalm methods
andrewklayk Oct 1, 2025
94351b9
update tests
andrewklayk Oct 1, 2025
2165501
update sslalm-adam internal logic
andrewklayk Oct 8, 2025
b313576
Revert "add saving objective gradient to sslalm methods"
andrewklayk Oct 8, 2025
59ef7e6
Merge branch 'no-accum'
andrewklayk Oct 8, 2025
8c205d3
update sslalm_adam and tests
andrewklayk Oct 8, 2025
da69c4c
update pyproject version
andrewklayk Oct 8, 2025
d3e1cb0
cleanup, device type
andrewklayk Oct 8, 2025
b46c84a
update sslalm to new dual_step logic, update examples
andrewklayk Oct 9, 2025
e7b9c0a
rename algorithms to optim, small alg updates
andrewklayk Oct 16, 2025
5434a53
update pyproject.toml
andrewklayk Oct 16, 2025
dc25e2f
restructuring
andrewklayk Oct 16, 2025
dfe9a63
cleanup
andrewklayk Nov 4, 2025
4aab65e
fix configs
andrewklayk Nov 4, 2025
1ebe603
fix bug in ghost
andrewklayk Nov 4, 2025
94c35dd
benchmarking script
bosakad Nov 6, 2025
079cacf
ssg slmalm done
bosakad Nov 6, 2025
54d0f87
cooper trial - not working yet
bosakad Nov 6, 2025
5ef10c5
add add_constraint
andrewklayk Nov 6, 2025
160c8cc
Merge branch 'pull'
andrewklayk Nov 6, 2025
20927b2
benchmarking deterministic cooper done
bosakad Nov 7, 2025
a47d7d6
benchmark update
bosakad Nov 7, 2025
974e5a4
Merge branch 'main' of github.com:andrewklayk/humancompatible-train
bosakad Nov 7, 2025
e97ad50
plots done with cooper. TODO next: ssl-alm with adam optimizer
bosakad Nov 7, 2025
7160911
sslalm adam now works
bosakad Nov 10, 2025
9fb142d
benchmarking logs done
bosakad Nov 10, 2025
211744e
benchmarking figure all in a single plot
bosakad Nov 10, 2025
19e7b23
parameter tuning done. TODO: rerun the notebook + implement chop / st…
bosakad Nov 10, 2025
3c0ccf5
benchmarked deterministic constraints done
bosakad Nov 11, 2025
e6c45d7
chop library works now
bosakad Nov 11, 2025
3d681a0
benchmarking of deterministic constraints done
bosakad Nov 12, 2025
28f9014
deterministic constraints done; tweaked hyperparams of frankwolfe
bosakad Nov 12, 2025
9463353
SSLALM sgd + adam done stochastic
bosakad Nov 20, 2025
28a0a8b
SSG stochastic done
bosakad Nov 20, 2025
888dcea
Unconstrained adam stochastic done
bosakad Nov 20, 2025
b3031d6
cooper stochastic benchmark done
bosakad Nov 20, 2025
c159771
stochastic benchmarking done
bosakad Nov 21, 2025
1d859b6
better deterministic plots
bosakad Nov 21, 2025
90d30e5
plots redone
bosakad Nov 21, 2025
e3f57c2
plots redone
bosakad Nov 21, 2025
c08fc14
deterministic constraints benchmarks all plots added
bosakad Nov 21, 2025
45f2219
plots are done for both stochastic and deterministic
bosakad Nov 21, 2025
aa2ae46
benchmark + markdown for deterministic and stochastic done. todo: imp…
bosakad Nov 21, 2025
2bc7002
importace sampling done
bosakad Nov 24, 2025
be7b457
plotting of importance sampling
bosakad Nov 24, 2025
8855cde
plotting of importance sampling + new
bosakad Nov 24, 2025
d618fb9
more atributes benchmarking for importance sampling
bosakad Nov 25, 2025
e866daf
importance sampling is working now - todo: finish
bosakad Nov 26, 2025
547f971
updates
andrewklayk Nov 27, 2025
6daa5f1
importance sampling done
bosakad Nov 27, 2025
15df6d7
importance sampling done
bosakad Nov 27, 2025
106f492
markdown update
bosakad Nov 27, 2025
3ecaf0e
Merge branch 'backup-main'
andrewklayk Nov 28, 2025
c899d01
gitignore update
andrewklayk Nov 28, 2025
ec23010
BalancedBatchSampler shuffle each batch internally
andrewklayk Nov 28, 2025
7e0fc58
refactor, extend balancedbatchsampler
andrewklayk Dec 1, 2025
9706924
Merge branch 'main' into main
jmarecek Dec 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions .github/workflows/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
- name: Install package dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
pip install -r requirements.txt
pip install -e .
- name: Run tests
run: |
python -m unittest
- name: Install benchmark dependencies
run: |
pip install -r requirements_benchmark.txt
pip install -e .
- name: Run algorithms
run: |
python experiments/run_folktables.py alg=sslalm n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=ghost n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=alm n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=sgd n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=fairret n_runs=2 run_maxtime=2

run-on-windows:
name: Setup on windows
Expand All @@ -50,19 +55,24 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
- name: Install package dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
pip install -r requirements.txt
pip install -e .
- name: Run tests
run: |
python -m unittest
- name: Install benchmark dependencies
run: |
pip install -r requirements_benchmark.txt
pip install -e .
- name: Run algorithms
run: |
python experiments/run_folktables.py alg=sslalm n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=ghost n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=alm n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=sgd n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=fairret n_runs=2 run_maxtime=2

run-on-macos:
name: Setup on macos
Expand All @@ -78,16 +88,22 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
- name: Install package dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
pip install -r requirements.txt
pip install -e .
- name: Run algorithms
- name: Run tests
run: |
python experiments/run_folktables.py alg=sslalm n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=ghost n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=alm n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=sgd n_runs=2 run_maxtime=2
python experiments/run_folktables.py alg=fairret n_runs=2 run_maxtime=2
python -m unittest
# - name: Install benchmark dependencies
# run: |
# pip install -U --force-reinstall certifi
# pip install -r requirements_benchmark.txt
# pip install -e .
# - name: Run algorithms
# run: |
# python experiments/run_folktables.py alg=sslalm n_runs=2 run_maxtime=2
# python experiments/run_folktables.py alg=ghost n_runs=2 run_maxtime=2
# python experiments/run_folktables.py alg=alm n_runs=2 run_maxtime=2
# python experiments/run_folktables.py alg=sgd n_runs=2 run_maxtime=2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ experiments/utils/raw_data/
experiments/utils/exp_results
experiments/utils/saved_models
experiments/outputs
experiments/conf
data/
examples/data
experiments/data
Expand Down
68 changes: 38 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ The toolkit implements algorithms for constrained training of neural networks ba
<!-- , as well as a tool to compare stochastic-constrained stochastic optimization algorithms on a _fair learning_ task in the `experiments` folder. -->

## Table of Contents

1. [Basic installation instructions](#basic-installation-instructions)
2. [Using the toolkit](#using-the-toolkit)
3. [Extending the toolkit](#extending-the-toolkit)
3. [Extending the toolkit](#extending-the-toolkit)
4. [Reproducing the Benchmark](#reproducing-the-benchmark)
5. [License and terms of use](#license-and-terms-of-use)
6. [References](#references)

humancompatible-train is still under active development! If you find bugs or have feature
requests, please file a
[Github issue](https://github.com/humancompatible/train/issues).
[Github issue](https://github.com/humancompatible/train/issues).

## Installation

Use

```
```bash
pip install humancompatible-train
```

Expand All @@ -31,11 +32,11 @@ The only dependencies of this package are `numpy` and `torch`.

The toolkit implements algorithms for constrained training of neural networks based on PyTorch.

The algorithms follow the `dual_step()` - `step()` framework: taking inspiration from PyTorch, the `double_step` does updates related to the dual parameters and prepares for the primal update (by, e.g., saving constraint gradients), and `step()` updates the primal parameters.
The algorithms follow the `dual_step()` - `step()` framework: taking inspiration from PyTorch, the `dual_step()` does updates related to the dual parameters and prepares for the primal update (by, e.g., saving constraint gradients), and `step()` updates the primal parameters.

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

```
```python
for inputs, labels in dataloader:
# inference
outputs = model(inputs)
Expand All @@ -54,13 +55,20 @@ for inputs, labels in dataloader:
```

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, such as `fairret`. To install those, run

```
pip install humancompatible-train[examples]
```

*The legacy API used for the benchmark is presented in `examples/_old_/algorithm_demo.ipynb` and `examples/_old_/constraint_demo.ipynb`.*

## Extending the toolkit
Expand All @@ -74,21 +82,27 @@ You are invited to check out our new API presented in notebooks in the `examples
The code used in [our benchmark paper](https://arxiv.org/abs/2507.04033) is not migrated to the new API yet (WIP).

### Basic installation instructions

The code requires Python version ```3.11```.

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

2. Install from source.

```
git clone https://github.com/humancompatible/train.git
cd train
Expand All @@ -97,11 +111,12 @@ pip install .
```

If you wish to edit the code of the algorithms, install as an editable package:

```
pip install -e .
```

__Warning__: it is recommended to use Stochastic Ghost with the mkl-accelerated version of the scipy package with Stochastic Ghost; to install it, run
**Warning**: it is recommended to use Stochastic Ghost with the mkl-accelerated version of the scipy package with Stochastic Ghost; to install it, run

```pip install --force-reinstall -i https://software.repos.intel.com/python/pypi scipy```

Expand All @@ -115,12 +130,14 @@ after installing requirements.txt; otherwise, the algorithm will run slower. How
### Running the algorithms

The benchmark comprises the following algorithms:

- Stochastic Ghost [[2]](#2),
- SSL-ALM [[3]](#3),
- Stochastic Switching Subgradient [[4]](#4).

To reproduce the experiments of the paper, run the following:
``` python

```
cd experiments
python run_folktables.py data=folktables alg=sslalm
python run_folktables.py data=folktables alg=alm
Expand All @@ -129,23 +146,25 @@ python run_folktables.py data=folktables alg=ssg
python run_folktables.py data=folktables alg=sgd # baseline, no fairness
python run_folktables.py data=folktables alg=fairret # baseline, fairness with regularizer
```

Each command will start 10 runs of the `alg`, 30 seconds each.
The results will be saved to `experiments/utils/saved_models` and `experiments/utils/exp_results`.
<!-- In the repository, we include the configuration needed to reproduce the experiments in the paper. To do so, go to `experiments` and run `python run_folktables.py data=folktables alg=sslalm`. -->
<!-- Repeat for the other algorithms by changing the `alg` parameter. -->

This repository uses [Hydra](https://hydra.cc/) to manage parameters; see `experiments/conf` for configuration files.
* To change the parameters of the experiment, such as the number of runs for each algorithm, run time, the dataset used (*note: for now supports only Folktables*) - use `experiment.yaml`.
* To change the dataset settings - such as file location - or do dataset-specific adjustments - such as the configuration of the protected attributes - use `data/{dataset_name}.yaml`
* To change algorithm hyperparameters, use `alg/{algorithm_name}.yaml`.
* To change constraint hyperparameters, use `constraint/{constraint_name}.yaml`
This repository uses [Hydra](https://hydra.cc/) to manage parameters; see `experiments/conf` for configuration files.

- To change the parameters of the experiment, such as the number of runs for each algorithm, run time, the dataset used (*note: for now supports only Folktables*) - use `experiment.yaml`.
- To change the dataset settings - such as file location - or do dataset-specific adjustments - such as the configuration of the protected attributes - use `data/{dataset_name}.yaml`
- To change algorithm hyperparameters, use `alg/{algorithm_name}.yaml`.
- To change constraint hyperparameters, use `constraint/{constraint_name}.yaml`

<!-- ; it is installed as one of the dependencies. -->
<!-- To learn more about using Hydra, please check out the [official tutorial](https://hydra.cc/docs/tutorials/basic/your_first_app). -->

### Producing plots
The plots and tables like the ones in the paper can be produced using the two notebooks. `experiments/algo_plots.ipynb` houses the convergence plots, and `experiments/model_plots.ipynb` - all the others.

The plots and tables like the ones in the paper can be produced using the two notebooks. `experiments/algo_plots.ipynb` houses the convergence plots, and `experiments/model_plots.ipynb` - all the others.

## License and terms of use

Expand All @@ -155,7 +174,7 @@ The benchmark part of the package relies on the Folktables package, provided und
It provides code to download data from the American Community Survey
(ACS) Public Use Microdata Sample (PUMS) files managed by the US Census Bureau.
The data itself is governed by the terms of use provided by the Census Bureau.
For more information, see https://www.census.gov/data/developers/about/terms-of-service.html
For more information, see <https://www.census.gov/data/developers/about/terms-of-service.html>

<!-- ## Cite this work -->

Expand All @@ -181,18 +200,7 @@ For more information, see https://www.census.gov/data/developers/about/terms-of-

If you use this work, we encourage you to cite [our paper](https://arxiv.org/abs/2509.21254),

```
@misc{kliachkin2025train,
title={humancompatible.train: Implementing Optimization Algorithms for Stochastically-Constrained Stochastic Optimization Problems},
author={Andrii Kliachkin and Jana Lepšová and Gilles Bareilles and Jakub Mareček},
year={2025},
eprint={2509.21254},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2509.21254},
note={Workshop on Constrained Optimization for Machine Learning at NeurIPS 2025}
}

```bibtex
@misc{kliachkin2025benchmarkingstochasticapproximationalgorithms,
title={Benchmarking Stochastic Approximation Algorithms for Fairness-Constrained Training of Deep Neural Networks},
author={Andrii Kliachkin and Jana Lepšová and Gilles Bareilles and Jakub Mareček},
Expand All @@ -204,14 +212,14 @@ If you use this work, we encourage you to cite [our paper](https://arxiv.org/abs
}
```

<a id="1">[1]</a>
<a id="1">[1]</a>
Ding, Hardt & Miller et al. (2021) Retiring Adult: New Datasets for Fair Machine Learning, Curran Associates, Inc..

<a id="2">[2]</a>
<a id="2">[2]</a>
Facchinei & Kungurtsev (2023) Stochastic Approximation for Expectation Objective and Expectation Inequality-Constrained Nonconvex Optimization, arXiv.

<a id="3">[3]</a>
<a id="3">[3]</a>
Huang, Zhang & Alacaoglu (2025) Stochastic Smoothed Primal-Dual Algorithms for Nonconvex Optimization with Linear Inequality Constraints, arXiv.

<a id="4">[4]</a>
<a id="4">[4]</a>
Huang & Lin (2023) Oracle Complexity of Single-Loop Switching Subgradient Methods for Non-Smooth Weakly Convex Functional Constrained Optimization, Curran Associates Inc..
Loading