|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "1f1cbcfa", |
| 5 | + "id": "0", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | 8 | "# Adversarial Example with PyTorch CNN\n", |
|
15 | 15 | { |
16 | 16 | "cell_type": "code", |
17 | 17 | "execution_count": null, |
18 | | - "id": "80408f10", |
| 18 | + "id": "1", |
19 | 19 | "metadata": {}, |
20 | 20 | "outputs": [], |
21 | 21 | "source": [ |
|
37 | 37 | }, |
38 | 38 | { |
39 | 39 | "cell_type": "markdown", |
40 | | - "id": "39afe5a8", |
| 40 | + "id": "2", |
41 | 41 | "metadata": {}, |
42 | 42 | "source": [ |
43 | 43 | "## Define CNN model\n", |
|
48 | 48 | { |
49 | 49 | "cell_type": "code", |
50 | 50 | "execution_count": null, |
51 | | - "id": "6cb1cee3", |
| 51 | + "id": "3", |
52 | 52 | "metadata": {}, |
53 | 53 | "outputs": [], |
54 | 54 | "source": [ |
|
70 | 70 | }, |
71 | 71 | { |
72 | 72 | "cell_type": "markdown", |
73 | | - "id": "facae133", |
| 73 | + "id": "4", |
74 | 74 | "metadata": {}, |
75 | 75 | "source": [ |
76 | 76 | "## Load MNIST data\n", |
|
81 | 81 | { |
82 | 82 | "cell_type": "code", |
83 | 83 | "execution_count": null, |
84 | | - "id": "a10d8b89", |
| 84 | + "id": "5", |
85 | 85 | "metadata": {}, |
86 | 86 | "outputs": [], |
87 | 87 | "source": [ |
|
98 | 98 | }, |
99 | 99 | { |
100 | 100 | "cell_type": "markdown", |
101 | | - "id": "68d83367", |
| 101 | + "id": "6", |
102 | 102 | "metadata": {}, |
103 | 103 | "source": [ |
104 | 104 | "## Quick training (optional)\n", |
|
109 | 109 | { |
110 | 110 | "cell_type": "code", |
111 | 111 | "execution_count": null, |
112 | | - "id": "7965e4d3", |
| 112 | + "id": "7", |
113 | 113 | "metadata": {}, |
114 | 114 | "outputs": [], |
115 | 115 | "source": [ |
|
128 | 128 | " loss.backward()\n", |
129 | 129 | " optimizer.step()\n", |
130 | 130 | " running += loss.item() * xb.size(0)\n", |
131 | | - " print(f\"Epoch {epoch+1}: loss={running/len(train_loader.dataset):.4f}\")\n", |
| 131 | + " print(f\"Epoch {epoch + 1}: loss={running / len(train_loader.dataset):.4f}\")\n", |
132 | 132 | "\n", |
133 | 133 | "# Evaluate quickly\n", |
134 | 134 | "model.eval()\n", |
|
140 | 140 | " pred = logits.argmax(dim=1).cpu()\n", |
141 | 141 | " correct += (pred == yb).sum().item()\n", |
142 | 142 | " total += yb.size(0)\n", |
143 | | - "print(f\"Test accuracy: {correct/total:.3f}\")" |
| 143 | + "print(f\"Test accuracy: {correct / total:.3f}\")" |
144 | 144 | ] |
145 | 145 | }, |
146 | 146 | { |
147 | 147 | "cell_type": "markdown", |
148 | | - "id": "f59e0148", |
| 148 | + "id": "8", |
149 | 149 | "metadata": {}, |
150 | 150 | "source": [ |
151 | 151 | "## Select a correctly classified example\n", |
|
156 | 156 | { |
157 | 157 | "cell_type": "code", |
158 | 158 | "execution_count": null, |
159 | | - "id": "7faadde6", |
| 159 | + "id": "9", |
160 | 160 | "metadata": {}, |
161 | 161 | "outputs": [], |
162 | 162 | "source": [ |
|
191 | 191 | }, |
192 | 192 | { |
193 | 193 | "cell_type": "markdown", |
194 | | - "id": "76b995a1", |
| 194 | + "id": "10", |
195 | 195 | "metadata": {}, |
196 | 196 | "source": [ |
197 | 197 | "## Build MILP for adversarial example\n", |
|
204 | 204 | { |
205 | 205 | "cell_type": "code", |
206 | 206 | "execution_count": null, |
207 | | - "id": "742ca52a-4168-4202-9e9e-6ff77f2a9e30", |
| 207 | + "id": "11", |
208 | 208 | "metadata": {}, |
209 | 209 | "outputs": [], |
210 | 210 | "source": [ |
|
235 | 235 | { |
236 | 236 | "cell_type": "code", |
237 | 237 | "execution_count": null, |
238 | | - "id": "44d6f31c-5a58-4873-a1d2-a67745fd22cc", |
| 238 | + "id": "12", |
239 | 239 | "metadata": {}, |
240 | 240 | "outputs": [], |
241 | 241 | "source": [ |
|
245 | 245 | { |
246 | 246 | "cell_type": "code", |
247 | 247 | "execution_count": null, |
248 | | - "id": "d29f968a-fb32-4735-a4a6-e2053d593d07", |
| 248 | + "id": "13", |
249 | 249 | "metadata": {}, |
250 | 250 | "outputs": [], |
251 | 251 | "source": [ |
|
255 | 255 | { |
256 | 256 | "cell_type": "code", |
257 | 257 | "execution_count": null, |
258 | | - "id": "8f5da81d-4be4-4dcc-b701-d8ffb91583c5", |
| 258 | + "id": "14", |
259 | 259 | "metadata": {}, |
260 | 260 | "outputs": [], |
261 | 261 | "source": [ |
|
268 | 268 | { |
269 | 269 | "cell_type": "code", |
270 | 270 | "execution_count": null, |
271 | | - "id": "a7e3ebe5", |
| 271 | + "id": "15", |
272 | 272 | "metadata": {}, |
273 | 273 | "outputs": [], |
274 | 274 | "source": [ |
|
280 | 280 | }, |
281 | 281 | { |
282 | 282 | "cell_type": "markdown", |
283 | | - "id": "17c5fd6a", |
| 283 | + "id": "16", |
284 | 284 | "metadata": {}, |
285 | 285 | "source": [ |
286 | 286 | "## Visualize result\n", |
|
291 | 291 | { |
292 | 292 | "cell_type": "code", |
293 | 293 | "execution_count": null, |
294 | | - "id": "049c27c1", |
| 294 | + "id": "17", |
295 | 295 | "metadata": {}, |
296 | 296 | "outputs": [], |
297 | 297 | "source": [ |
|
328 | 328 | { |
329 | 329 | "cell_type": "code", |
330 | 330 | "execution_count": null, |
331 | | - "id": "3cb3162b", |
| 331 | + "id": "18", |
332 | 332 | "metadata": {}, |
333 | 333 | "outputs": [], |
334 | 334 | "source": [ |
|
353 | 353 | { |
354 | 354 | "cell_type": "code", |
355 | 355 | "execution_count": null, |
356 | | - "id": "f1c0397c-a5b5-4249-a7f8-273823ad1707", |
| 356 | + "id": "19", |
357 | 357 | "metadata": {}, |
358 | 358 | "outputs": [], |
359 | 359 | "source": [] |
|
378 | 378 | "version": "3.13.3" |
379 | 379 | }, |
380 | 380 | "license": { |
381 | | - "full_text": "# Copyright © 2023 Gurobi Optimization, LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================" |
| 381 | + "full_text": "# Copyright © 2025 Gurobi Optimization, LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# ==============================================================================" |
382 | 382 | } |
383 | 383 | }, |
384 | 384 | "nbformat": 4, |
|
0 commit comments