This code allows reproduction of results in the paper
"Are Normalizing Flows the Key to Unlocking the Exponential Mechanism? " https://arxiv.org/abs/2311.09200
The code here provides implementation of our experiments where we test if we can training models with ExpM+NF alongside and in comparison with DPSGD and non-private training.
For use of our code or reference to our method, please cite
@misc{bridges2024normalizing,
title={Are Normalizing Flows the Key to Unlocking the Exponential Mechanism? A Path through the Accuracy-Privacy Ceiling Constraining Differentially Private ML},
author={Robert A. Bridges and Vandy J. Tombs and Christopher B. Stanley},
year={2024},
eprint={2311.09200},
archivePrefix={arXiv},
primaryClass={stat.ML}
}
Bridges, Robert A., Vandy J. Tombs, and Christopher B. Stanley. "Are Normalizing Flows the Key to Unlocking the Exponential Mechanism? A Path through the Accuracy-Privacy Ceiling Constraining Differentially Private ML" arXiv preprint arXiv:2311.09200 (2023).
(See data set up instructions for citations needed when using MIMIC-III data and MIMIC_Extract's pipeline. )
We used pyenv for managing different versions of python and venv for our python virtual envrionment.
Quick background and useful commands appear at the bottom of this readme.
The setup follows reference: https://www.freecodecamp.org/news/manage-multiple-python-versions-and-virtual-environments-venv-pyenv-pyvenv-a29fb00c296f/
Steps:
-
Install python 3.9.14 via pyenv using
pyenv install 3.9.14) -
Set pyenv envrionment to python 3.9.14 using
pyenv local 3.9.14 -
Initialize virtual envrionment in the .venv folder
python3 -m venv .venv. Now, in the new folder.venv/binshould be a copy of python3.9Note that the
.venvfolder is gitignored and should not ship with the repo. -
Start virtual envrionment:
source .venv/bin/activateAt this point running
which pipandwhich pythonshould produce a path to thepipandpythoninstances in.venv/bin/.Running
python --versionshould produce 3.9.14.VS Code and Jupyter users may need to point their environment to the right Python interpreter. (
.vinv/bin/python) -
We shipped our
pip freezeoutput as therequirements_*.txtfiles file, so one can (try to) install all packages withpip install -r requirements.txt. If that is problematic, one can trycat requirements.txt | xargs -n 1 pip installfollowing https://stackoverflow.com/questions/22250483/stop-pip-from-failing-on-single-package-when-installing-with-requirements-txt (For some reason to install scikit requires:python3 -m pip install scikit-learn)
-
/.venv/ folder (.gitignored) holds pyvenv copies of python etc. Created by you using the setup above.
-
/data/* (gitignored) holds the mimic3 data (not included, instructions below). Below is what it should have after downloading mimic3 data and preprocessing it.
- /mimic/ # note! this is a submodule
- all_hourly_data.h5 # gitignored, must be downloaded. See notebooks/mimic_preprocessing/README.md for info/directions
- lvl2_l_inf_normalized.h5 l_infinity normalized mimic3 data, note! not pivoted on hours yet
- lvl2_z_normalized.h5 z-score normalized mimic3 data. note! not pivoted on hours yet
- Ys_mimic_extract.csv - First three columns are multi-indices for the patient, remaining columns are the targets. Use
Ysr = pd.read_csv('data/mimic/Ys_mimic_extract.csv', index_col=[0,1,2])when you read it in!
- /mimic/ # note! this is a submodule
-
/code/ folder holds python scripts that in general process /data into /results
-
/notebooks/ folder for jupyter notebooks
-
/results/ folder with results from code/
We use the MIMIC-III data version 1.4 (https://physionet.org/content/mimiciii/1.4/) and MIMIC-III data processing pipeline called MIMIC-Extract of Wang et al. (https://github.com/MLforHealth/MIMIC_Extract). Please cite their papers when using their data and code, see subsection below with their citation requests.
The concise todos are itemized here (with in depth details to follow):
- First go to https://physionet.org/ create a login and required trainings for accessing the MIMIC-III
- Create
/data/mimic/folder in this repo (/data already gitignored) - Download the data. While the direct URL for the dataset is page is https://physionet.org/content/mimiciii/1.4/, once Physionet verified+trainings are complete, you should have access to this link. Download the
all_hourly_data.h5and put it in/data/mimicSome notes:- in the (MIMIC-Extract repo)[https://github.com/MLforHealth/MIMIC_Extract#pre-processed-output] it says
If you simply wish to use the output of this pipeline in your own research, a preprocessed version with default parameters is available via gcp, here.
- this is the "level_2" data in the mimic-extract and chasing long tails paper it seems.
- the "raw" data is w/o the second layer aggregation. We believe and it does not appear in this google cloud account. One could conceivably rerun the pipeline from mimic-extract to make it. In that case, the "full" mimic-3 data would be supplied by physionet. At the bottom of the MIMIC-III v1.4 page are the download options. Register your google email as an email in your physionet account and verify email.
- in the (MIMIC-Extract repo)[https://github.com/MLforHealth/MIMIC_Extract#pre-processed-output] it says
- Run either
/code/mimic_experiment/mimic_extract_preprocess.pyor the equivalent notebook/notebooks/mimic_extract_preprocess.ipynb(in this repo). It will read in the/data/mimic/all_hourly_data.h5data. It will create twolvl_*.h5files in the same location.- Discrepancies with Suriyakumar et al. are that we
- have 104 features, but they claim to have 78
- have 89.1% missingness before imputation but they claim to have 78%
- Notably the number of rows matches exactly. Discussion of these directions from MIMIC_Extract repo is here
- Discrepancies with Suriyakumar et al. are that we
When using the mimic3 data resource, please cite:
-
Johnson, A., Pollard, T., & Mark, R. (2016). MIMIC-III Clinical Database (version 1.4). PhysioNet. https://doi.org/10.13026/C2XW26.
-
Johnson, A. E. W., Pollard, T. J., Shen, L., Lehman, L. H., Feng, M., Ghassemi, M., Moody, B., Szolovits, P., Celi, L. A., & Mark, R. G. (2016). MIMIC-III, a freely accessible critical care database. Scientific Data, 3, 160035.
-
Goldberger, A., Amaral, L., Glass, L., Hausdorff, J., Ivanov, P. C., Mark, R., ... & Stanley, H. E. (2000). PhysioBank, PhysioToolkit, and PhysioNet: Components of a new research resource for complex physiologic signals. Circulation [Online]. 101 (23), pp. e215–e220.
When using MIMIC_Extract pipeline please cite:
- Shirly Wang, Matthew B. A. McDermott, Geeticka Chauhan, Michael C. Hughes, Tristan Naumann, and Marzyeh Ghassemi. MIMIC-Extract: A Data Extraction, Preprocessing, and Representation Pipeline for MIMIC-III. arXiv:1907.08322.
Goal was to reproduce benchmarks in the following paper:
"Chasing your long tails" Suriyakumar 2021 and to create approapriate dataset to build on their work. The Supplementary Information for the paper describes their data processing details.
In brief, the benchmark paper uses the mimic-extract pipeline defaults:
- patient first ICU visit
- all patients older than 15 years of age
- Except: ICU stay > 36 hours (mimic-extract: > 12 hours) They arrive with a cohort of 21,877 unique ICU stays. (we do too) Discrepancies with Suriyakumar et al. are that we - have 104 features, but they claim to have 78 - have 89.1% missingness before imputation but they claim to have 78%
- Filter for ICU stay > 36 hours (as noted above)
- Filter for patients missing
diagnosis_at_admission
NOTE: There was 1 patient with missing record for
diagnosis_at_admission. After dropping them, the total cohort selection = 21,877
A preprocessed version with default parameters is available via gcp, here.
To create accuracy and privacy results for each training (nonprivate/dpsgd/expm) and each (logistic_regression/grud) experiments, one will use code/mimic_experiment/<model>/run_<type>.py, e.g., for dpsgd training on logistic_regression use code/mimic_experiment/logistic_regression/run_dpsgd.py.
- We ran this three times, two hyperparameter searching runs and a third final run.
- first run uses
h_pass = "first", use_full_train = False(broad hyperparmeter search w/ validation on dev set.) - second run uses
h_pass = "refined", use_full_train = False(refined hyperparms based results of first run w/ validation on dev set.) - final results run uses
h_pass = "refined", use_full_train = True(best hyperparms previously observed are use, now with validation on the test set.)
- first run uses
- To set up the script:
- lines ~15 are the GPU/CPU setup and will have to be configured for your enviornment (
gpu_names, device_ids, use_gpu = setup_devices(devices)) - lines ~17 are parameters to set as follows:
use_full_train = Falsewill use train/dev splits (for all hyperparm search runs).use_full_train = Truewill use train+dev/ test (for final results only)h_pass: tells which hyperparmeters to use in the code//hyperparemeter_sets.py.
- lines ~15 are the GPU/CPU setup and will have to be configured for your enviornment (
One must have results from h_pass = refined to run h_pass = final as it uses the best found hyperparms in the refined run.
Output will appear in results folder according to the path in line ~215: RESULTS_FOLDER = Path(FOLDER, f"{h_pass}{run}") which is printed upon writing.
Hyperparmeter search spaces we used are in the hyperparm_sets.py scripts.
To begin the membership inference attack, each (logistic regression/grud) need shadow models trained. To train shadow models, one will use code/mimic_experiment/<model>/run_mia_shadow_models.py. Two methods hinge/stable are implemented for computing logit confidences per example (both in/out examples) and a flag at the top determines which will be used (our reported experiments use stable). The number of shadow models can also be changed (we default to 1000). The shadow models will use the best non-private hyperparameters found from the hyperparameter searches from the Accuracy and privacy results Section (i.e., that hyperparameter search must be done prior to running this code).
Once the shadow models have been trained, to attack and audit for target models for each training (nonprivate/dpsgd/expm) and each (logistic regression/grud) experiments, one will use code/mimic_experiment/<model>/mia_<type>.py. These target models will use the best hyperparameters found during their respective hyperparameter search runs done previously. The number of target models and what confidence method to use can be set at the top. If fewer than 1000 shadow models were trained, N_shadow_models must be set to the correct number.
Output will appear in the results folder according to the path set in line ~210: RESULTS_FOLDER = Path(FOLDER, f"{conf_type}_{N_shadow_models}") which is printed upon writing.
To create the timing benchmarking results for each training (nonprivate/dpsgd/expm) an each (logistic_regression/grud) experiments, one will run code/mimic_experiment/<model>/timing_benchmark_<type>.py, e.g., for dpsgd training on logistic_regression we would open code/mimic_experiment/logistic_regression/timing_benchmark_dpsgd.py.
These scripts are set up similarly to the run_<type>.py scripts.
We used the full dataset for timing benchmarks, i.e. use_full_train = True.
Set h_pass = benchmarks.
See Pytorch Timer source code if desired:
- https://github.com/pytorch/pytorch/blob/main/torch/utils/benchmark/utils/timer.py
- https://github.com/pytorch/pytorch/blob/main/torch/utils/benchmark/utils/common.py#L74
For each (logistic regression /grud) there is a plotting code file that can be run to create the result plots, namely /code/mimic_experiment/<model>/plot_results.py
See the two notebooks
- /notebooks/1d_expm_plots.ipynb ## expm plots in 1D for varying epsilon
- /3d-plots-expm-nf.ipynb ## Expm and NF plots for easy data.
- run
python code/mimic_experiment/logistic_regression/uq_example_lr.pyHappy reproducing!