humancompatible-train 0.1.0__tar.gz → 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/PKG-INFO +115 -46
- humancompatible_train-0.1.0/humancompatible_train.egg-info/PKG-INFO → humancompatible_train-0.1.3/README.md +99 -62
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/experiments/run_folktables.py +2 -2
- {humancompatible_train-0.1.0/humancompatible/train/algorithms/torch → humancompatible_train-0.1.3/humancompatible/train/algorithms}/__init__.py +1 -0
- {humancompatible_train-0.1.0/humancompatible/train/algorithms/torch → humancompatible_train-0.1.3/humancompatible/train/algorithms}/ssl_alm.py +0 -1
- humancompatible_train-0.1.3/humancompatible/train/algorithms/ssl_alm_adam.py +330 -0
- humancompatible_train-0.1.3/humancompatible/train/algorithms/test/test_ssl_alm.py +144 -0
- humancompatible_train-0.1.3/humancompatible/train/algorithms/test/test_ssl_alm_adam.py +146 -0
- humancompatible_train-0.1.3/humancompatible/train/algorithms/test/test_ssw.py +64 -0
- humancompatible_train-0.1.3/humancompatible/train/fairness/__init__.py +0 -0
- humancompatible_train-0.1.3/humancompatible/train/fairness/utils/tests/__init__.py +0 -0
- humancompatible_train-0.1.3/humancompatible/train/fairness/utils/tests/test_balanced_batch_sampler.py +78 -0
- humancompatible_train-0.1.0/README.md → humancompatible_train-0.1.3/humancompatible_train.egg-info/PKG-INFO +131 -45
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible_train.egg-info/SOURCES.txt +8 -8
- humancompatible_train-0.1.3/humancompatible_train.egg-info/requires.txt +23 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/pyproject.toml +3 -1
- humancompatible_train-0.1.0/humancompatible/train/algorithms/Algorithm.py +0 -25
- humancompatible_train-0.1.0/humancompatible/train/algorithms/__init__.py +0 -8
- humancompatible_train-0.1.0/humancompatible/train/algorithms/ghost.py +0 -250
- humancompatible_train-0.1.0/humancompatible/train/algorithms/sgd.py +0 -107
- humancompatible_train-0.1.0/humancompatible/train/algorithms/ssl_alm.py +0 -311
- humancompatible_train-0.1.0/humancompatible/train/algorithms/switching_subgradient.py +0 -192
- humancompatible_train-0.1.0/humancompatible/train/algorithms/utils.py +0 -61
- humancompatible_train-0.1.0/humancompatible_train.egg-info/requires.txt +0 -6
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/LICENCE.txt +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/experiments/__init__.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/experiments/calculate_iteration_values.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/experiments/run_folktables_torchalgs.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/__init__.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/__init__.py +0 -0
- {humancompatible_train-0.1.0/humancompatible/train/algorithms/torch → humancompatible_train-0.1.3/humancompatible/train/algorithms}/ssw.py +0 -0
- {humancompatible_train-0.1.0/humancompatible/train/fairness → humancompatible_train-0.1.3/humancompatible/train/algorithms/test}/__init__.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/fairness/constraints/__init__.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/fairness/constraints/constraint.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/fairness/constraints/constraint_fns.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/fairness/constraints/torch/__init__.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/fairness/constraints/torch/constraints.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/fairness/utils/__init__.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible/train/fairness/utils/balanced_batch_sampler.py +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible_train.egg-info/dependency_links.txt +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/humancompatible_train.egg-info/top_level.txt +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/setup.cfg +0 -0
- {humancompatible_train-0.1.0 → humancompatible_train-0.1.3}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: humancompatible-train
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: PyTorch-based package for constrained training of neural networks
|
|
5
5
|
Author: Gilles Bareilles, Jana Lepsova, Jakub Marecek
|
|
6
6
|
Author-email: Andrii Kliachkin <kliachkin.andrii@gmail.com>
|
|
@@ -10,45 +10,131 @@ Description-Content-Type: text/markdown
|
|
|
10
10
|
License-File: LICENCE.txt
|
|
11
11
|
Requires-Dist: torch
|
|
12
12
|
Requires-Dist: numpy
|
|
13
|
+
Provides-Extra: examples
|
|
14
|
+
Requires-Dist: ipykernel; extra == "examples"
|
|
15
|
+
Requires-Dist: ipympl; extra == "examples"
|
|
16
|
+
Requires-Dist: fairret; extra == "examples"
|
|
17
|
+
Requires-Dist: folktables; extra == "examples"
|
|
18
|
+
Requires-Dist: scikit-learn; extra == "examples"
|
|
19
|
+
Requires-Dist: matplotlib; extra == "examples"
|
|
20
|
+
Provides-Extra: benchmark
|
|
21
|
+
Requires-Dist: fairret; extra == "benchmark"
|
|
22
|
+
Requires-Dist: matplotlib; extra == "benchmark"
|
|
23
|
+
Requires-Dist: pandas; extra == "benchmark"
|
|
24
|
+
Requires-Dist: folktables; extra == "benchmark"
|
|
25
|
+
Requires-Dist: pot; extra == "benchmark"
|
|
26
|
+
Requires-Dist: hydra; extra == "benchmark"
|
|
27
|
+
Requires-Dist: omegaconf; extra == "benchmark"
|
|
13
28
|
Provides-Extra: ghost
|
|
14
29
|
Requires-Dist: qpsolvers; extra == "ghost"
|
|
15
30
|
Requires-Dist: scipy; extra == "ghost"
|
|
16
31
|
Dynamic: license-file
|
|
17
32
|
|
|
18
|
-
#
|
|
33
|
+
# humancompatible-train: a package for constrained machine learning
|
|
19
34
|
|
|
20
35
|
[](https://opensource.org/licenses/Apache-2.0) [](https://github.com/humancompatible/train/actions/workflows/setup.yml)
|
|
21
36
|
|
|
22
|
-
The toolkit implements algorithms for constrained training of neural networks based on PyTorch, and inspired by PyTorch's API
|
|
37
|
+
The toolkit implements algorithms for constrained training of neural networks based on PyTorch, and inspired by PyTorch's API.
|
|
38
|
+
<!-- , as well as a tool to compare stochastic-constrained stochastic optimization algorithms on a _fair learning_ task in the `experiments` folder. -->
|
|
23
39
|
|
|
24
40
|
## Table of Contents
|
|
41
|
+
|
|
25
42
|
1. [Basic installation instructions](#basic-installation-instructions)
|
|
26
43
|
2. [Using the toolkit](#using-the-toolkit)
|
|
27
|
-
3. [
|
|
28
|
-
4. [
|
|
44
|
+
3. [Extending the toolkit](#extending-the-toolkit)
|
|
45
|
+
4. [Reproducing the Benchmark](#reproducing-the-benchmark)
|
|
29
46
|
5. [License and terms of use](#license-and-terms-of-use)
|
|
30
47
|
6. [References](#references)
|
|
31
48
|
|
|
32
|
-
|
|
49
|
+
humancompatible-train is still under active development! If you find bugs or have feature
|
|
33
50
|
requests, please file a
|
|
34
|
-
[Github issue](https://github.com/humancompatible/train/issues).
|
|
51
|
+
[Github issue](https://github.com/humancompatible/train/issues).
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Use
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install humancompatible-train
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The only dependencies of this package are `numpy` and `torch`.
|
|
62
|
+
|
|
63
|
+
## Using the toolkit
|
|
64
|
+
|
|
65
|
+
The toolkit implements algorithms for constrained training of neural networks based on PyTorch.
|
|
66
|
+
|
|
67
|
+
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.
|
|
68
|
+
|
|
69
|
+
In general, your code using `humancompatible-train` would look something like this:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
for inputs, labels in dataloader:
|
|
73
|
+
# inference
|
|
74
|
+
outputs = model(inputs)
|
|
75
|
+
# calculate constraints and grads
|
|
76
|
+
for constraint in constraints:
|
|
77
|
+
c_eval = constraint(outputs, labels)
|
|
78
|
+
c_eval.backwards(retain_grad=True)
|
|
79
|
+
# depending on optimizer, update dual parameters / save constraint gradient / both
|
|
80
|
+
optimizer.dual_step(c_eval)
|
|
81
|
+
optimizer.zero_grad()
|
|
82
|
+
# calculate objective
|
|
83
|
+
loss = criterion(outputs,labels)
|
|
84
|
+
loss.backwards()
|
|
85
|
+
optimizer.step()
|
|
86
|
+
optimizer.zero_grad()
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Our idea is to
|
|
90
|
+
|
|
91
|
+
1. Deviate minimally from the usual PyTorch workflow
|
|
92
|
+
2. Make different stochastic-constrained stochastic optimization algorithms nearly interchangable in the code.
|
|
93
|
+
|
|
94
|
+
### Code examples
|
|
95
|
+
|
|
96
|
+
You are invited to check out our new API presented in notebooks in the `examples` folder.
|
|
97
|
+
|
|
98
|
+
The example notebooks have additional dependencies, such as `fairret`. To install those, run
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
pip install humancompatible-train[examples]
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
*The legacy API used for the benchmark is presented in `examples/_old_/algorithm_demo.ipynb` and `examples/_old_/constraint_demo.ipynb`.*
|
|
105
|
+
|
|
106
|
+
## Extending the toolkit
|
|
107
|
+
|
|
108
|
+
### Adding new code
|
|
109
|
+
|
|
110
|
+
**To add a new algorithm**, you can subclass the PyTorch ```Optimizer``` class and proceed following the API guideline presented above.
|
|
111
|
+
|
|
112
|
+
## Reproducing the Benchmark
|
|
113
|
+
|
|
114
|
+
The code used in [our benchmark paper](https://arxiv.org/abs/2507.04033) is not migrated to the new API yet (WIP).
|
|
115
|
+
|
|
116
|
+
### Basic installation instructions
|
|
35
117
|
|
|
36
|
-
## Basic installation instructions
|
|
37
118
|
The code requires Python version ```3.11```.
|
|
38
119
|
|
|
39
120
|
1. Create a virtual environment
|
|
40
121
|
|
|
41
122
|
**bash** (Linux)
|
|
123
|
+
|
|
42
124
|
```
|
|
43
125
|
python3.11 -m venv fairbenchenv
|
|
44
126
|
source fairbenchenv/bin/activate
|
|
45
127
|
```
|
|
128
|
+
|
|
46
129
|
**cmd** (Windows)
|
|
130
|
+
|
|
47
131
|
```
|
|
48
132
|
python -m venv fairbenchenv
|
|
49
133
|
fairbenchenv\Scripts\activate.bat
|
|
50
134
|
```
|
|
135
|
+
|
|
51
136
|
2. Install from source.
|
|
137
|
+
|
|
52
138
|
```
|
|
53
139
|
git clone https://github.com/humancompatible/train.git
|
|
54
140
|
cd train
|
|
@@ -57,11 +143,12 @@ pip install .
|
|
|
57
143
|
```
|
|
58
144
|
|
|
59
145
|
If you wish to edit the code of the algorithms, install as an editable package:
|
|
146
|
+
|
|
60
147
|
```
|
|
61
148
|
pip install -e .
|
|
62
149
|
```
|
|
63
150
|
|
|
64
|
-
|
|
151
|
+
**Warning**: it is recommended to use Stochastic Ghost with the mkl-accelerated version of the scipy package with Stochastic Ghost; to install it, run
|
|
65
152
|
|
|
66
153
|
```pip install --force-reinstall -i https://software.repos.intel.com/python/pypi scipy```
|
|
67
154
|
|
|
@@ -72,31 +159,17 @@ after installing requirements.txt; otherwise, the algorithm will run slower. How
|
|
|
72
159
|
<!-- pip install folktables -->
|
|
73
160
|
<!-- ``` -->
|
|
74
161
|
|
|
75
|
-
## Using the toolkit
|
|
76
|
-
|
|
77
|
-
The toolkit implements algorithms for constrained training of neural networks based on PyTorch, and inspired by PyTorch's API.
|
|
78
|
-
|
|
79
|
-
### Code examples
|
|
80
|
-
|
|
81
|
-
You are invited to check out the new API presented in notebooks in the `examples` folder.
|
|
82
|
-
|
|
83
|
-
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.
|
|
84
|
-
|
|
85
|
-
The idea is to make different algorithms nearly interchangable in the code.
|
|
86
|
-
|
|
87
|
-
The legacy API used for the benchmark is presented in `examples/_old_/algorithm_demo.ipynb` and `examples/_old_/constraint_demo.ipynb`.
|
|
88
|
-
|
|
89
|
-
## Reproducing the Benchmark
|
|
90
|
-
|
|
91
162
|
### Running the algorithms
|
|
92
163
|
|
|
93
164
|
The benchmark comprises the following algorithms:
|
|
165
|
+
|
|
94
166
|
- Stochastic Ghost [[2]](#2),
|
|
95
167
|
- SSL-ALM [[3]](#3),
|
|
96
168
|
- Stochastic Switching Subgradient [[4]](#4).
|
|
97
169
|
|
|
98
170
|
To reproduce the experiments of the paper, run the following:
|
|
99
|
-
|
|
171
|
+
|
|
172
|
+
```
|
|
100
173
|
cd experiments
|
|
101
174
|
python run_folktables.py data=folktables alg=sslalm
|
|
102
175
|
python run_folktables.py data=folktables alg=alm
|
|
@@ -105,38 +178,35 @@ python run_folktables.py data=folktables alg=ssg
|
|
|
105
178
|
python run_folktables.py data=folktables alg=sgd # baseline, no fairness
|
|
106
179
|
python run_folktables.py data=folktables alg=fairret # baseline, fairness with regularizer
|
|
107
180
|
```
|
|
181
|
+
|
|
108
182
|
Each command will start 10 runs of the `alg`, 30 seconds each.
|
|
109
183
|
The results will be saved to `experiments/utils/saved_models` and `experiments/utils/exp_results`.
|
|
110
184
|
<!-- 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`. -->
|
|
111
185
|
<!-- Repeat for the other algorithms by changing the `alg` parameter. -->
|
|
112
186
|
|
|
113
|
-
This repository uses [Hydra](https://hydra.cc/) to manage parameters; see `experiments/conf` for configuration files.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
187
|
+
This repository uses [Hydra](https://hydra.cc/) to manage parameters; see `experiments/conf` for configuration files.
|
|
188
|
+
|
|
189
|
+
- 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`.
|
|
190
|
+
- 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`
|
|
191
|
+
- To change algorithm hyperparameters, use `alg/{algorithm_name}.yaml`.
|
|
192
|
+
- To change constraint hyperparameters, use `constraint/{constraint_name}.yaml`
|
|
118
193
|
|
|
119
194
|
<!-- ; it is installed as one of the dependencies. -->
|
|
120
195
|
<!-- To learn more about using Hydra, please check out the [official tutorial](https://hydra.cc/docs/tutorials/basic/your_first_app). -->
|
|
121
196
|
|
|
122
197
|
### Producing plots
|
|
123
|
-
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.
|
|
124
198
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
### Adding new code
|
|
128
|
-
|
|
129
|
-
**To add a new algorithm**, you can subclass the PyTorch ```Optimizer``` class and proceed following the API guideline presented above.
|
|
199
|
+
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.
|
|
130
200
|
|
|
131
201
|
## License and terms of use
|
|
132
202
|
|
|
133
|
-
humancompatible
|
|
203
|
+
humancompatible-train is provided under the Apache 2.0 Licence.
|
|
134
204
|
|
|
135
205
|
The benchmark part of the package relies on the Folktables package, provided under MIT Licence.
|
|
136
206
|
It provides code to download data from the American Community Survey
|
|
137
207
|
(ACS) Public Use Microdata Sample (PUMS) files managed by the US Census Bureau.
|
|
138
208
|
The data itself is governed by the terms of use provided by the Census Bureau.
|
|
139
|
-
For more information, see https://www.census.gov/data/developers/about/terms-of-service.html
|
|
209
|
+
For more information, see <https://www.census.gov/data/developers/about/terms-of-service.html>
|
|
140
210
|
|
|
141
211
|
<!-- ## Cite this work -->
|
|
142
212
|
|
|
@@ -154,7 +224,7 @@ For more information, see https://www.census.gov/data/developers/about/terms-of-
|
|
|
154
224
|
|
|
155
225
|
## Future work
|
|
156
226
|
|
|
157
|
-
- Add more algorithms
|
|
227
|
+
- Add more algorithms
|
|
158
228
|
- Add more examples from different fields where constrained training of DNNs is employed
|
|
159
229
|
- Migrate the benchmark to the new API
|
|
160
230
|
|
|
@@ -162,7 +232,7 @@ For more information, see https://www.census.gov/data/developers/about/terms-of-
|
|
|
162
232
|
|
|
163
233
|
If you use this work, we encourage you to cite [our paper](https://arxiv.org/abs/2507.04033),
|
|
164
234
|
|
|
165
|
-
```
|
|
235
|
+
```bibtex
|
|
166
236
|
@misc{kliachkin2025benchmarkingstochasticapproximationalgorithms,
|
|
167
237
|
title={Benchmarking Stochastic Approximation Algorithms for Fairness-Constrained Training of Deep Neural Networks},
|
|
168
238
|
author={Andrii Kliachkin and Jana Lepšová and Gilles Bareilles and Jakub Mareček},
|
|
@@ -174,15 +244,14 @@ If you use this work, we encourage you to cite [our paper](https://arxiv.org/abs
|
|
|
174
244
|
}
|
|
175
245
|
```
|
|
176
246
|
|
|
177
|
-
<a id="1">[1]</a>
|
|
247
|
+
<a id="1">[1]</a>
|
|
178
248
|
Ding, Hardt & Miller et al. (2021) Retiring Adult: New Datasets for Fair Machine Learning, Curran Associates, Inc..
|
|
179
249
|
|
|
180
|
-
<a id="2">[2]</a>
|
|
250
|
+
<a id="2">[2]</a>
|
|
181
251
|
Facchinei & Kungurtsev (2023) Stochastic Approximation for Expectation Objective and Expectation Inequality-Constrained Nonconvex Optimization, arXiv.
|
|
182
252
|
|
|
183
|
-
<a id="3">[3]</a>
|
|
253
|
+
<a id="3">[3]</a>
|
|
184
254
|
Huang, Zhang & Alacaoglu (2025) Stochastic Smoothed Primal-Dual Algorithms for Nonconvex Optimization with Linear Inequality Constraints, arXiv.
|
|
185
255
|
|
|
186
|
-
<a id="4">[4]</a>
|
|
256
|
+
<a id="4">[4]</a>
|
|
187
257
|
Huang & Lin (2023) Oracle Complexity of Single-Loop Switching Subgradient Methods for Non-Smooth Weakly Convex Functional Constrained Optimization, Curran Associates Inc..
|
|
188
|
-
|
|
@@ -1,54 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
Name: humancompatible-train
|
|
3
|
-
Version: 0.1.0
|
|
4
|
-
Summary: PyTorch-based package for constrained training of neural networks
|
|
5
|
-
Author: Gilles Bareilles, Jana Lepsova, Jakub Marecek
|
|
6
|
-
Author-email: Andrii Kliachkin <kliachkin.andrii@gmail.com>
|
|
7
|
-
Maintainer-email: Andrii Kliachkin <kliachkin.andrii@gmail.com>
|
|
8
|
-
Requires-Python: >=3.11
|
|
9
|
-
Description-Content-Type: text/markdown
|
|
10
|
-
License-File: LICENCE.txt
|
|
11
|
-
Requires-Dist: torch
|
|
12
|
-
Requires-Dist: numpy
|
|
13
|
-
Provides-Extra: ghost
|
|
14
|
-
Requires-Dist: qpsolvers; extra == "ghost"
|
|
15
|
-
Requires-Dist: scipy; extra == "ghost"
|
|
16
|
-
Dynamic: license-file
|
|
17
|
-
|
|
18
|
-
# Benchmarking Stochastic Approximation Algorithms for Fairness-Constrained Training of Deep Neural Networks
|
|
1
|
+
# humancompatible-train: a package for constrained machine learning
|
|
19
2
|
|
|
20
3
|
[](https://opensource.org/licenses/Apache-2.0) [](https://github.com/humancompatible/train/actions/workflows/setup.yml)
|
|
21
4
|
|
|
22
|
-
The toolkit implements algorithms for constrained training of neural networks based on PyTorch, and inspired by PyTorch's API
|
|
5
|
+
The toolkit implements algorithms for constrained training of neural networks based on PyTorch, and inspired by PyTorch's API.
|
|
6
|
+
<!-- , as well as a tool to compare stochastic-constrained stochastic optimization algorithms on a _fair learning_ task in the `experiments` folder. -->
|
|
23
7
|
|
|
24
8
|
## Table of Contents
|
|
9
|
+
|
|
25
10
|
1. [Basic installation instructions](#basic-installation-instructions)
|
|
26
11
|
2. [Using the toolkit](#using-the-toolkit)
|
|
27
|
-
3. [
|
|
28
|
-
4. [
|
|
12
|
+
3. [Extending the toolkit](#extending-the-toolkit)
|
|
13
|
+
4. [Reproducing the Benchmark](#reproducing-the-benchmark)
|
|
29
14
|
5. [License and terms of use](#license-and-terms-of-use)
|
|
30
15
|
6. [References](#references)
|
|
31
16
|
|
|
32
|
-
|
|
17
|
+
humancompatible-train is still under active development! If you find bugs or have feature
|
|
33
18
|
requests, please file a
|
|
34
|
-
[Github issue](https://github.com/humancompatible/train/issues).
|
|
19
|
+
[Github issue](https://github.com/humancompatible/train/issues).
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Use
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install humancompatible-train
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The only dependencies of this package are `numpy` and `torch`.
|
|
30
|
+
|
|
31
|
+
## Using the toolkit
|
|
32
|
+
|
|
33
|
+
The toolkit implements algorithms for constrained training of neural networks based on PyTorch.
|
|
34
|
+
|
|
35
|
+
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.
|
|
36
|
+
|
|
37
|
+
In general, your code using `humancompatible-train` would look something like this:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
for inputs, labels in dataloader:
|
|
41
|
+
# inference
|
|
42
|
+
outputs = model(inputs)
|
|
43
|
+
# calculate constraints and grads
|
|
44
|
+
for constraint in constraints:
|
|
45
|
+
c_eval = constraint(outputs, labels)
|
|
46
|
+
c_eval.backwards(retain_grad=True)
|
|
47
|
+
# depending on optimizer, update dual parameters / save constraint gradient / both
|
|
48
|
+
optimizer.dual_step(c_eval)
|
|
49
|
+
optimizer.zero_grad()
|
|
50
|
+
# calculate objective
|
|
51
|
+
loss = criterion(outputs,labels)
|
|
52
|
+
loss.backwards()
|
|
53
|
+
optimizer.step()
|
|
54
|
+
optimizer.zero_grad()
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Our idea is to
|
|
58
|
+
|
|
59
|
+
1. Deviate minimally from the usual PyTorch workflow
|
|
60
|
+
2. Make different stochastic-constrained stochastic optimization algorithms nearly interchangable in the code.
|
|
61
|
+
|
|
62
|
+
### Code examples
|
|
63
|
+
|
|
64
|
+
You are invited to check out our new API presented in notebooks in the `examples` folder.
|
|
65
|
+
|
|
66
|
+
The example notebooks have additional dependencies, such as `fairret`. To install those, run
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
pip install humancompatible-train[examples]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
*The legacy API used for the benchmark is presented in `examples/_old_/algorithm_demo.ipynb` and `examples/_old_/constraint_demo.ipynb`.*
|
|
73
|
+
|
|
74
|
+
## Extending the toolkit
|
|
75
|
+
|
|
76
|
+
### Adding new code
|
|
77
|
+
|
|
78
|
+
**To add a new algorithm**, you can subclass the PyTorch ```Optimizer``` class and proceed following the API guideline presented above.
|
|
79
|
+
|
|
80
|
+
## Reproducing the Benchmark
|
|
81
|
+
|
|
82
|
+
The code used in [our benchmark paper](https://arxiv.org/abs/2507.04033) is not migrated to the new API yet (WIP).
|
|
83
|
+
|
|
84
|
+
### Basic installation instructions
|
|
35
85
|
|
|
36
|
-
## Basic installation instructions
|
|
37
86
|
The code requires Python version ```3.11```.
|
|
38
87
|
|
|
39
88
|
1. Create a virtual environment
|
|
40
89
|
|
|
41
90
|
**bash** (Linux)
|
|
91
|
+
|
|
42
92
|
```
|
|
43
93
|
python3.11 -m venv fairbenchenv
|
|
44
94
|
source fairbenchenv/bin/activate
|
|
45
95
|
```
|
|
96
|
+
|
|
46
97
|
**cmd** (Windows)
|
|
98
|
+
|
|
47
99
|
```
|
|
48
100
|
python -m venv fairbenchenv
|
|
49
101
|
fairbenchenv\Scripts\activate.bat
|
|
50
102
|
```
|
|
103
|
+
|
|
51
104
|
2. Install from source.
|
|
105
|
+
|
|
52
106
|
```
|
|
53
107
|
git clone https://github.com/humancompatible/train.git
|
|
54
108
|
cd train
|
|
@@ -57,11 +111,12 @@ pip install .
|
|
|
57
111
|
```
|
|
58
112
|
|
|
59
113
|
If you wish to edit the code of the algorithms, install as an editable package:
|
|
114
|
+
|
|
60
115
|
```
|
|
61
116
|
pip install -e .
|
|
62
117
|
```
|
|
63
118
|
|
|
64
|
-
|
|
119
|
+
**Warning**: it is recommended to use Stochastic Ghost with the mkl-accelerated version of the scipy package with Stochastic Ghost; to install it, run
|
|
65
120
|
|
|
66
121
|
```pip install --force-reinstall -i https://software.repos.intel.com/python/pypi scipy```
|
|
67
122
|
|
|
@@ -72,31 +127,17 @@ after installing requirements.txt; otherwise, the algorithm will run slower. How
|
|
|
72
127
|
<!-- pip install folktables -->
|
|
73
128
|
<!-- ``` -->
|
|
74
129
|
|
|
75
|
-
## Using the toolkit
|
|
76
|
-
|
|
77
|
-
The toolkit implements algorithms for constrained training of neural networks based on PyTorch, and inspired by PyTorch's API.
|
|
78
|
-
|
|
79
|
-
### Code examples
|
|
80
|
-
|
|
81
|
-
You are invited to check out the new API presented in notebooks in the `examples` folder.
|
|
82
|
-
|
|
83
|
-
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.
|
|
84
|
-
|
|
85
|
-
The idea is to make different algorithms nearly interchangable in the code.
|
|
86
|
-
|
|
87
|
-
The legacy API used for the benchmark is presented in `examples/_old_/algorithm_demo.ipynb` and `examples/_old_/constraint_demo.ipynb`.
|
|
88
|
-
|
|
89
|
-
## Reproducing the Benchmark
|
|
90
|
-
|
|
91
130
|
### Running the algorithms
|
|
92
131
|
|
|
93
132
|
The benchmark comprises the following algorithms:
|
|
133
|
+
|
|
94
134
|
- Stochastic Ghost [[2]](#2),
|
|
95
135
|
- SSL-ALM [[3]](#3),
|
|
96
136
|
- Stochastic Switching Subgradient [[4]](#4).
|
|
97
137
|
|
|
98
138
|
To reproduce the experiments of the paper, run the following:
|
|
99
|
-
|
|
139
|
+
|
|
140
|
+
```
|
|
100
141
|
cd experiments
|
|
101
142
|
python run_folktables.py data=folktables alg=sslalm
|
|
102
143
|
python run_folktables.py data=folktables alg=alm
|
|
@@ -105,38 +146,35 @@ python run_folktables.py data=folktables alg=ssg
|
|
|
105
146
|
python run_folktables.py data=folktables alg=sgd # baseline, no fairness
|
|
106
147
|
python run_folktables.py data=folktables alg=fairret # baseline, fairness with regularizer
|
|
107
148
|
```
|
|
149
|
+
|
|
108
150
|
Each command will start 10 runs of the `alg`, 30 seconds each.
|
|
109
151
|
The results will be saved to `experiments/utils/saved_models` and `experiments/utils/exp_results`.
|
|
110
152
|
<!-- 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`. -->
|
|
111
153
|
<!-- Repeat for the other algorithms by changing the `alg` parameter. -->
|
|
112
154
|
|
|
113
|
-
This repository uses [Hydra](https://hydra.cc/) to manage parameters; see `experiments/conf` for configuration files.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
155
|
+
This repository uses [Hydra](https://hydra.cc/) to manage parameters; see `experiments/conf` for configuration files.
|
|
156
|
+
|
|
157
|
+
- 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`.
|
|
158
|
+
- 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`
|
|
159
|
+
- To change algorithm hyperparameters, use `alg/{algorithm_name}.yaml`.
|
|
160
|
+
- To change constraint hyperparameters, use `constraint/{constraint_name}.yaml`
|
|
118
161
|
|
|
119
162
|
<!-- ; it is installed as one of the dependencies. -->
|
|
120
163
|
<!-- To learn more about using Hydra, please check out the [official tutorial](https://hydra.cc/docs/tutorials/basic/your_first_app). -->
|
|
121
164
|
|
|
122
165
|
### Producing plots
|
|
123
|
-
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.
|
|
124
166
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
### Adding new code
|
|
128
|
-
|
|
129
|
-
**To add a new algorithm**, you can subclass the PyTorch ```Optimizer``` class and proceed following the API guideline presented above.
|
|
167
|
+
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.
|
|
130
168
|
|
|
131
169
|
## License and terms of use
|
|
132
170
|
|
|
133
|
-
humancompatible
|
|
171
|
+
humancompatible-train is provided under the Apache 2.0 Licence.
|
|
134
172
|
|
|
135
173
|
The benchmark part of the package relies on the Folktables package, provided under MIT Licence.
|
|
136
174
|
It provides code to download data from the American Community Survey
|
|
137
175
|
(ACS) Public Use Microdata Sample (PUMS) files managed by the US Census Bureau.
|
|
138
176
|
The data itself is governed by the terms of use provided by the Census Bureau.
|
|
139
|
-
For more information, see https://www.census.gov/data/developers/about/terms-of-service.html
|
|
177
|
+
For more information, see <https://www.census.gov/data/developers/about/terms-of-service.html>
|
|
140
178
|
|
|
141
179
|
<!-- ## Cite this work -->
|
|
142
180
|
|
|
@@ -154,7 +192,7 @@ For more information, see https://www.census.gov/data/developers/about/terms-of-
|
|
|
154
192
|
|
|
155
193
|
## Future work
|
|
156
194
|
|
|
157
|
-
- Add more algorithms
|
|
195
|
+
- Add more algorithms
|
|
158
196
|
- Add more examples from different fields where constrained training of DNNs is employed
|
|
159
197
|
- Migrate the benchmark to the new API
|
|
160
198
|
|
|
@@ -162,7 +200,7 @@ For more information, see https://www.census.gov/data/developers/about/terms-of-
|
|
|
162
200
|
|
|
163
201
|
If you use this work, we encourage you to cite [our paper](https://arxiv.org/abs/2507.04033),
|
|
164
202
|
|
|
165
|
-
```
|
|
203
|
+
```bibtex
|
|
166
204
|
@misc{kliachkin2025benchmarkingstochasticapproximationalgorithms,
|
|
167
205
|
title={Benchmarking Stochastic Approximation Algorithms for Fairness-Constrained Training of Deep Neural Networks},
|
|
168
206
|
author={Andrii Kliachkin and Jana Lepšová and Gilles Bareilles and Jakub Mareček},
|
|
@@ -174,15 +212,14 @@ If you use this work, we encourage you to cite [our paper](https://arxiv.org/abs
|
|
|
174
212
|
}
|
|
175
213
|
```
|
|
176
214
|
|
|
177
|
-
<a id="1">[1]</a>
|
|
215
|
+
<a id="1">[1]</a>
|
|
178
216
|
Ding, Hardt & Miller et al. (2021) Retiring Adult: New Datasets for Fair Machine Learning, Curran Associates, Inc..
|
|
179
217
|
|
|
180
|
-
<a id="2">[2]</a>
|
|
218
|
+
<a id="2">[2]</a>
|
|
181
219
|
Facchinei & Kungurtsev (2023) Stochastic Approximation for Expectation Objective and Expectation Inequality-Constrained Nonconvex Optimization, arXiv.
|
|
182
220
|
|
|
183
|
-
<a id="3">[3]</a>
|
|
221
|
+
<a id="3">[3]</a>
|
|
184
222
|
Huang, Zhang & Alacaoglu (2025) Stochastic Smoothed Primal-Dual Algorithms for Nonconvex Optimization with Linear Inequality Constraints, arXiv.
|
|
185
223
|
|
|
186
|
-
<a id="4">[4]</a>
|
|
224
|
+
<a id="4">[4]</a>
|
|
187
225
|
Huang & Lin (2023) Oracle Complexity of Single-Loop Switching Subgradient Methods for Non-Smooth Weakly Convex Functional Constrained Optimization, Curran Associates Inc..
|
|
188
|
-
|
|
@@ -12,7 +12,7 @@ from omegaconf import DictConfig, OmegaConf
|
|
|
12
12
|
from torch import nn, tensor
|
|
13
13
|
from utils.load_folktables import prepare_folktables_multattr
|
|
14
14
|
from utils.network import SimpleNet
|
|
15
|
-
from humancompatible.train.algorithms.utils import net_grads_to_tensor
|
|
15
|
+
from humancompatible.train.benchmark.algorithms.utils import net_grads_to_tensor
|
|
16
16
|
from itertools import combinations
|
|
17
17
|
from humancompatible.train.fairness.constraints import FairnessConstraint
|
|
18
18
|
|
|
@@ -177,7 +177,7 @@ def run(cfg: DictConfig) -> None:
|
|
|
177
177
|
net = SimpleNet(in_shape=X_test.shape[1], out_shape=1, dtype=DTYPE).to(device)
|
|
178
178
|
|
|
179
179
|
optimizer_name = cfg.alg.import_name
|
|
180
|
-
module = importlib.import_module("humancompatible.train.algorithms")
|
|
180
|
+
module = importlib.import_module("humancompatible.train.benchmark.algorithms")
|
|
181
181
|
Optimizer = getattr(module, optimizer_name)
|
|
182
182
|
|
|
183
183
|
optimizer = Optimizer(net, train_ds, loss_fn, c)
|