This code repository is for our paper Privacy-Preserving Neural Processes for Probabilistic User Modeling (in python). In this work, we explore the integration of Neural Processes with Differential Privacy using DP-SGD and PLD for user-level privacy in probabilistic user modeling. This is a python implementation of this Julia repo.
-
Neural Process-based user modeling: Learn probabilistic representations of user behavior.
-
Differential Privacy Integration: Achieve privacy-preserving predictions with DP-SGD and PLD.
-
Customizable Pipeline: Adapt the model for various user data types and privacy budgets.
This repo was developed by Jonathan Taylor, Research IT, University of Manchester.
Questions about the scientific content should be directed to the paper's corresponding author, Haripriya Harikumar.
-
Clone and enter the repo, then switch to the main branch for the latest release:
git clone https://github.com/AI-Fundamentals/dp-priv-python.git cd dp-priv-python git checkout mainNB if using CSF, please ignore the rest of the instructions and follow the workflow in the jobscripts folder.
-
Create and activate a python 3.11 environment
-
Install packages from pip:
pip install -r requirements.txt
-
Install PyTorch:
- On a system with Nvidia GPU:
pip install torch==2.3.0
- On Mac with Silicon GPU:
pip install --pre torch==2.3.0 --extra-index-url https://download.pytorch.org/whl/nightly/cpu
- On high-performance computing systems, refer to the relevant documentation.
-
Test the environment installation:
pytest tests/test_nps.py
-
Run unit tests:
python -m pytest
This repo requires pre-computed data which are generated using this Julia code.
To run the training for experiment 2:
python -m experiments.train -settings settings/ex2/settings_ex2_train.jsonSettings must be loaded from a valid json file. If no valid file is found (or the -settings argument isn't used), the default settings will be loaded instead. For full details of the settings files, and some example files, see the settings folder.
In the training settings json file, there is an item 'init_weights'. Set this to the path of a file containing weights from a previous model training run. These weights will then be used as the initial training weights.
To run the validation script (i.e. model performance vs number of training epochs) for experiment 2:
python -m experiments.val -settings settings/ex2/settings_ex2_val.jsonThis will then save a figure in your figs_dir folder from the settings file, and also the evaluation metrics will also be saved in the models_dir folder.
To run the n_traj evaluation (i.e. model performance vs number of context trajectories), run the experiment2_eval_ntraj script:
python -m experiments.eval_ntraj -settings settings/ex2/settings_ex2_val.json- Generate training data using the Julia data generation code.
- Run training script
experiments/train.pywith an appropriate settings file. Load training data from data folder. Save model weights and metadata parameters in models folder. Save training metrics plot to figures folder. - Run
experiments/val.py, with an appropriate settings file, to validate model performance for different numbers of training epochs. - Run
experiments/eval_ntraj.py, with an appropriate settings file, to evaluate model performence for different numbers of inference trajectories. This requires the user to specify a specific set of model weights to use- this will likely be the best training epoch (as evaluated during step 3). - Load test performance data from CSV files and plot. Save to figures folder.
For a detailed description of the workflow, see the jobscripts folder.
Each model training or evaluation run requires a json settings file (otherwise a set of default settings will be used). Example files, and a detailed explanation of the various settings, are given in the settings folder.
The scripts folder contains some useful scripts. Most of these are for plotting the final output data by loading the CSV files created during steps 3 and 4 in the workflow overview section.