Skip to content

Commit 9ad0e80

Browse files
authored
Merge pull request #4 from andrewklayk/neurips
hydra, requirements, CI
2 parents 72e2e0d + f61380c commit 9ad0e80

38 files changed

Lines changed: 2745 additions & 1926 deletions

.github/workflows/setup.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Setup check
2+
3+
on:
4+
push:
5+
branches: [ "main", "neurips" ]
6+
pull_request:
7+
branches: [ "main", "neurips" ]
8+
9+
10+
jobs:
11+
run-on-linux:
12+
name: Setup on linux
13+
runs-on: ubuntu-latest
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
python-version: ["3.11"]
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v3
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
python -m pip install flake8 pytest
29+
pip install -r requirements.txt
30+
pip install -e .
31+
- name: Run algorithms
32+
run: |
33+
python experiments/run_folktables.py alg=sslalm n_runs=2 run_maxtime=2
34+
python experiments/run_folktables.py alg=ghost n_runs=2 run_maxtime=2
35+
python experiments/run_folktables.py alg=alm n_runs=2 run_maxtime=2
36+
python experiments/run_folktables.py alg=sgd n_runs=2 run_maxtime=2
37+
python experiments/run_folktables.py alg=fairret n_runs=2 run_maxtime=2
38+
39+
run-on-windows:
40+
name: Setup on windows
41+
runs-on: windows-latest
42+
strategy:
43+
fail-fast: false
44+
matrix:
45+
python-version: ["3.11"]
46+
47+
steps:
48+
- uses: actions/checkout@v4
49+
- name: Set up Python ${{ matrix.python-version }}
50+
uses: actions/setup-python@v3
51+
with:
52+
python-version: ${{ matrix.python-version }}
53+
- name: Install dependencies
54+
run: |
55+
python -m pip install --upgrade pip
56+
python -m pip install flake8 pytest
57+
pip install -r requirements.txt
58+
pip install -e .
59+
- name: Run algorithms
60+
run: |
61+
python experiments/run_folktables.py alg=sslalm n_runs=2 run_maxtime=2
62+
python experiments/run_folktables.py alg=ghost n_runs=2 run_maxtime=2
63+
python experiments/run_folktables.py alg=alm n_runs=2 run_maxtime=2
64+
python experiments/run_folktables.py alg=sgd n_runs=2 run_maxtime=2
65+
python experiments/run_folktables.py alg=fairret n_runs=2 run_maxtime=2
66+
67+
run-on-macos:
68+
name: Setup on macos
69+
runs-on: macos-latest
70+
strategy:
71+
fail-fast: false
72+
matrix:
73+
python-version: ["3.11"]
74+
75+
steps:
76+
- uses: actions/checkout@v4
77+
- name: Set up Python ${{ matrix.python-version }}
78+
uses: actions/setup-python@v3
79+
with:
80+
python-version: ${{ matrix.python-version }}
81+
- name: Install dependencies
82+
run: |
83+
python -m pip install --upgrade pip
84+
python -m pip install flake8 pytest
85+
pip install -r requirements.txt
86+
pip install -e .
87+
- name: Run algorithms
88+
run: |
89+
python experiments/run_folktables.py alg=sslalm n_runs=2 run_maxtime=2
90+
python experiments/run_folktables.py alg=ghost n_runs=2 run_maxtime=2
91+
python experiments/run_folktables.py alg=alm n_runs=2 run_maxtime=2
92+
python experiments/run_folktables.py alg=sgd n_runs=2 run_maxtime=2
93+
python experiments/run_folktables.py alg=fairret n_runs=2 run_maxtime=2

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
experiments/utils/raw_data/
44
experiments/utils/exp_results
55
experiments/utils/saved_models
6+
experiments/outputs
67
data/
8+
examples/data
9+
experiments/data
710
.vscode/
811
plots/
12+
outputs/
913

1014

1115
# Byte-compiled / optimized / DLL files

README.md

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Benchmarking Stochastic Approximation Algorithms for Fairness-Constrained Training of Deep Neural Networks
22

3-
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
3+
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Setup](https://github.com/humancompatible/train/actions/workflows/setup.yml/badge.svg)](https://github.com/humancompatible/train/actions/workflows/setup.yml)
44

55
This repository provides a tool to compare stochastic-constrained stochastic optimization algorithms on a _fair learning_ task.
66

@@ -16,19 +16,33 @@ requests, please file a
1616
[Github issue](https://github.com/humancompatible/train/issues).
1717

1818
## Basic installation instructions
19-
The code requires Python version ```3.10```.
19+
The code requires Python version ```3.11```.
2020

2121
1. Create a virtual environment
22+
23+
**bash** (Linux)
2224
```
23-
python3.10 -m venv fairbenchenv
25+
python3.11 -m venv fairbenchenv
2426
source fairbenchenv/bin/activate
2527
```
26-
2. Install from source.
28+
**cmd** (Windows)
29+
```
30+
python -m venv fairbenchenv
31+
fairbenchenv\Scripts\activate.bat
32+
```
33+
2. Install from source (*as an editable package*).
2734
```
2835
git clone https://github.com/humancompatible/train.git
2936
cd train
3037
pip install -r requirements.txt
38+
pip install -e .
3139
```
40+
__Warning__: it is recommended to use Stochastic Ghost with the mkl-accelerated version of the scipy package with Stochastic Ghost; to install it, run
41+
42+
```pip install --force-reinstall -i https://software.repos.intel.com/python/pypi scipy```
43+
44+
after installing requirements.txt; otherwise, the algorithm will run slower. However, this is not supported on MacOS and may fail on some Windows devices.
45+
3246
<!-- Install via pip -->
3347
<!-- ``` -->
3448
<!-- pip install folktables -->
@@ -37,24 +51,62 @@ pip install -r requirements.txt
3751
## Reproducing the Benchmark
3852

3953
### Running the algorithms
40-
To reproduce the experiments in the paper, run ```experiments/run_folktables.py``` with the dataset name, algorithm name and hyperparameters as command line arguments, like below:
41-
```run_folktables.py --algorithm sslalm --state OK --task income --constraint loss --loss_bound 0.005 --num_exp 10 --time 30 --batch_size 8 -mu 2. -rho 1. -tau 0.01 -eta 5e-2 -beta 0.5```
42-
This will start 10 runs of the SSL-ALM algorithm, 30 seconds each, and save the model and the results in the ```experiments/utils/saved_models``` and ```experiments/utils/exp_results``` folders.
4354

4455
The benchmark comprises the following algorithms:
4556
- Stochastic Ghost [[2]](#2),
4657
- SSL-ALM [[3]](#3),
4758
- Stochastic Switching Subgradient [[4]](#4).
4859

60+
To reproduce the experiments of the paper, run the following:
61+
``` python
62+
cd experiments
63+
python run_folktables.py data=folktables alg=sslalm
64+
python run_folktables.py data=folktables alg=alm
65+
python run_folktables.py data=folktables alg=ghost
66+
python run_folktables.py data=folktables alg=ssg
67+
python run_folktables.py data=folktables alg=sgd # baseline, no fairness
68+
python run_folktables.py data=folktables alg=fairret # baseline, fairness with regularizer
69+
```
70+
Each command will start 10 runs of the `alg`, 30 seconds each.
71+
The results will be saved to `experiments/utils/saved_models` and `experiments/utils/exp_results`.
72+
<!-- 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`. -->
73+
<!-- Repeat for the other algorithms by changing the `alg` parameter. -->
74+
75+
This repository uses [Hydra](https://hydra.cc/) to manage parameters; see `experiments/conf` for configuration files.
76+
* 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`.
77+
* To change the dataset settings - such as file location - or do dataset-specific adjustments, use `data/{dataset_name}.yaml`
78+
* To change algorithm hyperparameters, use `alg/{algorithm_name}.yaml`.
79+
* To change constraint hyperparameters, use `constraint/{constraint_name}.yaml`
80+
81+
<!-- ; it is installed as one of the dependencies. -->
82+
<!-- To learn more about using Hydra, please check out the [official tutorial](https://hydra.cc/docs/tutorials/basic/your_first_app). -->
83+
4984
### Producing plots
5085
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.
5186

52-
**Warning**: As of 16/05, Folktables seems to be unable to connect to the American census servers. This means that downloading the dataset through the code is not possible. Manual download requires two files: the .csv dataset, at https://www2.census.gov/programs-surveys/acs/data/pums/`{year}`/`{horizon}`, and the corresponding .csv description, at https://www2.census.gov/programs-surveys/acs/tech_docs/pums/data_dict/; use the flag ```--no-download```. By default, the files will be placed in `experiments/utils/raw_data/{task}/{year}/{horizon}` (e.g. `experiments/utils/raw_data/income/2018/1-Year/{filename}.csv`). A custom path can be specified with the --data_path argument, but it has to have the form `*/{year}/{horizon}/`.
87+
## Extending the benchmark
5388

54-
## Extending the benchmark
89+
**To add a new algorithm**, you can subclass the ```Algorithm``` class. Before you can run it, you will need to follow these steps:
90+
1. In the `experiments/conf/alg` folder, add a `.yaml` file with `import_name: {ClassName}` (so the code knows which algorithm to import) and the desired keyword parameter values under `params`:
5591

56-
To add a different constraint formulation, you can use the ```FairnessConstraint``` class by passing your callable function to the constructor as ```fn```.
57-
To add a new algorithm, you can subclass the ```Algorithm``` class.
92+
```
93+
import_name: ClassName
94+
95+
params:
96+
param_name_1: value
97+
param_name_2: value
98+
```
99+
100+
2. In `src/algorithms/__init__.py`, add `from .{filename} import {ClassName}` (so the code is able to import it).
101+
102+
Now you can run the algorithm by executing `python run_folktables.py data=folktables alg={yaml_file_name}`, or by changing the experiment config files.
103+
104+
**To add a different constraint formulation**, you can use the `FairnessConstraint` class by passing your callable function to the constructor as `fn`. If you use `run_folktables.py`, you can add a new constraint function by following the steps:
105+
106+
1. Add a `.yaml` file with `import_name: {FunctionName}`, along with the desired batch size and bound (*to be reworked for more generality*), to the `experiments/conf/constraint` folder
107+
2. Import it in `src/constraints/__init__.py` as in step 2 above.
108+
109+
Now, to run the code with your constraint, use the `constraint` field in the main config.
58110

59111
## License and terms of use
60112

@@ -80,6 +132,11 @@ For more information, see https://www.census.gov/data/developers/about/terms-of-
80132
<!-- } -->
81133
<!-- ``` -->
82134

135+
## Future work
136+
137+
- Add support for fairness constraints with >=2 subgroups (limitation of the code, not of the algorithms)
138+
- Add support to datasets besides Folktables
139+
- Move towards a more PyTorch-like API for optimizers
83140

84141
## References
85142

constraint_demo.ipynb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "1efc20cc",
6+
"metadata": {},
7+
"source": [
8+
"This notebook will demonstrate the `FairnessConstraint` class and how you can use it to **add a constraint formulation**."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "58feeb34",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": []
18+
}
19+
],
20+
"metadata": {
21+
"kernelspec": {
22+
"display_name": "humancompatible",
23+
"language": "python",
24+
"name": "python3"
25+
},
26+
"language_info": {
27+
"codemirror_mode": {
28+
"name": "ipython",
29+
"version": 3
30+
},
31+
"file_extension": ".py",
32+
"mimetype": "text/x-python",
33+
"name": "python",
34+
"nbconvert_exporter": "python",
35+
"pygments_lexer": "ipython3",
36+
"version": "3.11.12"
37+
}
38+
},
39+
"nbformat": 4,
40+
"nbformat_minor": 5
41+
}

experiments/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)