File size: 18,083 Bytes
476d115 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PyTorch Differential Privacy Experiment"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting torch\n",
" Downloading torch-2.4.0-cp311-none-macosx_11_0_arm64.whl.metadata (26 kB)\n",
"Collecting torchvision\n",
" Downloading torchvision-0.19.0-cp311-cp311-macosx_11_0_arm64.whl.metadata (6.0 kB)\n",
"Collecting opacus\n",
" Downloading opacus-1.5.2-py3-none-any.whl.metadata (7.9 kB)\n",
"Requirement already satisfied: numpy in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (2.1.0)\n",
"Requirement already satisfied: pandas in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (2.2.2)\n",
"Requirement already satisfied: filelock in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from torch) (3.15.4)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from torch) (4.12.2)\n",
"Collecting sympy (from torch)\n",
" Downloading sympy-1.13.2-py3-none-any.whl.metadata (12 kB)\n",
"Collecting networkx (from torch)\n",
" Using cached networkx-3.3-py3-none-any.whl.metadata (5.1 kB)\n",
"Requirement already satisfied: jinja2 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from torch) (3.1.4)\n",
"Requirement already satisfied: fsspec in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from torch) (2024.6.1)\n",
"Collecting pillow!=8.3.*,>=5.3.0 (from torchvision)\n",
" Downloading pillow-10.4.0-cp311-cp311-macosx_11_0_arm64.whl.metadata (9.2 kB)\n",
"Collecting numpy\n",
" Using cached numpy-1.26.4-cp311-cp311-macosx_11_0_arm64.whl.metadata (114 kB)\n",
"Collecting scipy>=1.2 (from opacus)\n",
" Downloading scipy-1.14.1-cp311-cp311-macosx_14_0_arm64.whl.metadata (60 kB)\n",
"Collecting opt-einsum>=3.3.0 (from opacus)\n",
" Downloading opt_einsum-3.3.0-py3-none-any.whl.metadata (6.5 kB)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from pandas) (2.9.0.post0)\n",
"Requirement already satisfied: pytz>=2020.1 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from pandas) (2024.1)\n",
"Requirement already satisfied: tzdata>=2022.7 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from pandas) (2024.1)\n",
"Requirement already satisfied: six>=1.5 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from jinja2->torch) (2.1.5)\n",
"Collecting mpmath<1.4,>=1.1.0 (from sympy->torch)\n",
" Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)\n",
"Downloading torch-2.4.0-cp311-none-macosx_11_0_arm64.whl (62.1 MB)\n",
"\u001b[2K \u001b[38;2;114;156;31mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m62.1/62.1 MB\u001b[0m \u001b[31m440.8 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mm eta \u001b[36m0:00:01\u001b[0m[36m0:00:04\u001b[0m\n",
"\u001b[?25hDownloading torchvision-0.19.0-cp311-cp311-macosx_11_0_arm64.whl (1.7 MB)\n",
"\u001b[2K \u001b[38;2;114;156;31mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m560.7 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m1m557.0 kB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hDownloading opacus-1.5.2-py3-none-any.whl (239 kB)\n",
"Using cached numpy-1.26.4-cp311-cp311-macosx_11_0_arm64.whl (14.0 MB)\n",
"Downloading opt_einsum-3.3.0-py3-none-any.whl (65 kB)\n",
"Downloading pillow-10.4.0-cp311-cp311-macosx_11_0_arm64.whl (3.4 MB)\n",
"\u001b[2K \u001b[38;2;114;156;31mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m457.2 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m kB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m:02\u001b[0m\n",
"\u001b[?25hDownloading scipy-1.14.1-cp311-cp311-macosx_14_0_arm64.whl (23.1 MB)\n",
"\u001b[2K \u001b[38;2;114;156;31mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m23.1/23.1 MB\u001b[0m \u001b[31m442.0 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mm eta \u001b[36m0:00:01\u001b[0m[36m0:00:02\u001b[0m\n",
"\u001b[?25hUsing cached networkx-3.3-py3-none-any.whl (1.7 MB)\n",
"Downloading sympy-1.13.2-py3-none-any.whl (6.2 MB)\n",
"\u001b[2K \u001b[38;2;114;156;31mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m6.2/6.2 MB\u001b[0m \u001b[31m462.5 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m1m487.3 kB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hUsing cached mpmath-1.3.0-py3-none-any.whl (536 kB)\n",
"Installing collected packages: mpmath, sympy, pillow, numpy, networkx, torch, scipy, opt-einsum, torchvision, opacus\n",
" Attempting uninstall: numpy\n",
" Found existing installation: numpy 2.1.0\n",
" Uninstalling numpy-2.1.0:\n",
" Successfully uninstalled numpy-2.1.0\n",
"Successfully installed mpmath-1.3.0 networkx-3.3 numpy-1.26.4 opacus-1.5.2 opt-einsum-3.3.0 pillow-10.4.0 scipy-1.14.1 sympy-1.13.2 torch-2.4.0 torchvision-0.19.0\n",
"Note: you may need to restart the kernel to use updated packages.\n",
"Requirement already satisfied: wandb in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (0.17.7)\n",
"Requirement already satisfied: datasets in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (2.21.0)\n",
"Requirement already satisfied: tqdm in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (4.66.5)\n",
"Requirement already satisfied: click!=8.0.0,>=7.1 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (8.1.7)\n",
"Requirement already satisfied: docker-pycreds>=0.4.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (0.4.0)\n",
"Requirement already satisfied: gitpython!=3.1.29,>=1.0.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (3.1.43)\n",
"Requirement already satisfied: platformdirs in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (4.2.2)\n",
"Requirement already satisfied: protobuf!=4.21.0,<6,>=3.19.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (5.27.3)\n",
"Requirement already satisfied: psutil>=5.0.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (6.0.0)\n",
"Requirement already satisfied: pyyaml in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (6.0.2)\n",
"Requirement already satisfied: requests<3,>=2.0.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (2.32.3)\n",
"Requirement already satisfied: sentry-sdk>=1.0.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (2.13.0)\n",
"Requirement already satisfied: setproctitle in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (1.3.3)\n",
"Requirement already satisfied: setuptools in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from wandb) (73.0.1)\n",
"Requirement already satisfied: filelock in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (3.15.4)\n",
"Requirement already satisfied: numpy>=1.17 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (1.26.4)\n",
"Requirement already satisfied: pyarrow>=15.0.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (17.0.0)\n",
"Requirement already satisfied: dill<0.3.9,>=0.3.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (0.3.8)\n",
"Requirement already satisfied: pandas in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (2.2.2)\n",
"Requirement already satisfied: xxhash in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (3.5.0)\n",
"Requirement already satisfied: multiprocess in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (0.70.16)\n",
"Requirement already satisfied: fsspec<=2024.6.1,>=2023.1.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from fsspec[http]<=2024.6.1,>=2023.1.0->datasets) (2024.6.1)\n",
"Requirement already satisfied: aiohttp in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (3.10.5)\n",
"Requirement already satisfied: huggingface-hub>=0.21.2 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (0.24.6)\n",
"Requirement already satisfied: packaging in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from datasets) (24.1)\n",
"Requirement already satisfied: six>=1.4.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from docker-pycreds>=0.4.0->wandb) (1.16.0)\n",
"Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from aiohttp->datasets) (2.4.0)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from aiohttp->datasets) (1.3.1)\n",
"Requirement already satisfied: attrs>=17.3.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from aiohttp->datasets) (24.2.0)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from aiohttp->datasets) (1.4.1)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from aiohttp->datasets) (6.0.5)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from aiohttp->datasets) (1.9.4)\n",
"Requirement already satisfied: gitdb<5,>=4.0.1 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from gitpython!=3.1.29,>=1.0.0->wandb) (4.0.11)\n",
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from huggingface-hub>=0.21.2->datasets) (4.12.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from requests<3,>=2.0.0->wandb) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from requests<3,>=2.0.0->wandb) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from requests<3,>=2.0.0->wandb) (2.2.2)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from requests<3,>=2.0.0->wandb) (2024.7.4)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from pandas->datasets) (2.9.0.post0)\n",
"Requirement already satisfied: pytz>=2020.1 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from pandas->datasets) (2024.1)\n",
"Requirement already satisfied: tzdata>=2022.7 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from pandas->datasets) (2024.1)\n",
"Requirement already satisfied: smmap<6,>=3.0.1 in /Users/nullzero/Library/Caches/pypoetry/virtualenvs/verida-differential-privacy-OB45ac0m-py3.11/lib/python3.11/site-packages (from gitdb<5,>=4.0.1->gitpython!=3.1.29,>=1.0.0->wandb) (5.0.1)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install -qqq torch torchvision opacus numpy pandas\n",
"%pip install -qqq wandb datasets tqdm\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import torch \n",
"from dotenv import load_dotenv\n",
"import wandb \n",
"import logging\n",
"import shutil\n",
"import sys\n",
"from datetime import datetime, timedelta\n",
"\n",
"import argparse\n",
"from collections import Counter\n",
"from pathlib import Path\n",
"from statistics import mean\n",
"\n",
"import torch\n",
"import torch.nn as nn\n",
"from opacus import PrivacyEngine\n",
"from opacus.layers import DPGRU, DPLSTM, DPRNN\n",
"from torch.nn.utils.rnn import pad_sequence\n",
"from torch.utils.data import DataLoader, Dataset\n",
"from tqdm import tqdm, tqdm_notebook"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"device = \"mps\" if torch.backends.mps.is_available() else \"cpu\"\n",
"if os.path.exists('.env'):\n",
" load_dotenv('.env')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"logging.basicConfig(\n",
" format=\"%(asctime)s:%(levelname)s:%(message)s\",\n",
" datefmt=\"%m/%d/%Y %H:%M:%S\",\n",
" stream=sys.stdout,\n",
")\n",
"logger = logging.getLogger(\"ddp\")\n",
"logger.setLevel(level=logging.INFO)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wandb.login(key=os.getenv('WANDB_API_KEY'))\n",
"wandb.init(project=\"verida-pii\", name=\"verida_data_raw\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_name = 'Ezi/medical_and_legislators_synthetic'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|