Skip to content

Commit a72188d

Browse files
committed
erasure
1 parent f1ffae3 commit a72188d

File tree

7 files changed

+503
-1
lines changed

7 files changed

+503
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ modula/__pycache__/
66
docs/build/
77
docs/jupyter_execute/
88

9+
docs/source/examples/assets/
10+
911
examples/.ipynb_checkpoints/
1012
examples/data/*
1113
!examples/data/*.py

docs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ livehtml:
2121
sphinx-autobuild "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2222

2323
livedirhtml:
24-
sphinx-autobuild -b dirhtml "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
24+
sphinx-autobuild -b dirhtml --watch ../examples --ignore source/examples/assets "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2525

2626
.PHONY: help Makefile livehtml livedirhtml
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"path": "../../../examples/weight-erasure.ipynb",
3+
"extra-media": [
4+
"../../../examples/assets/erasure.png"
5+
]
6+
}

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The docs currently contain some original research contributions not published an
6161

6262
examples/hello-world
6363
examples/hello-mnist
64+
examples/weight-erasure
6465

6566
.. toctree::
6667
:hidden:

examples/assets/erasure.png

236 KB
Loading

examples/data/cifar10.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import numpy as np
2+
import os
3+
import pickle
4+
import urllib.request
5+
import tarfile
6+
7+
def load_cifar10(normalize=True):
8+
"""
9+
Downloads (if needed) and loads the CIFAR-10 dataset.
10+
Returns: (train_images, train_labels, test_images, test_labels)
11+
"""
12+
# Create data directory
13+
data_dir = os.path.join(os.path.dirname(__file__), "cifar10_files")
14+
if not os.path.exists(data_dir):
15+
os.makedirs(data_dir)
16+
17+
# Download files if needed
18+
url = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
19+
filepath = os.path.join(data_dir, "cifar-10-python.tar.gz")
20+
extracted_dir = os.path.join(data_dir, "cifar-10-batches-py")
21+
22+
if not os.path.exists(extracted_dir):
23+
if not os.path.isfile(filepath):
24+
print(f"Downloading {url}")
25+
urllib.request.urlretrieve(url, filepath)
26+
with tarfile.open(filepath, 'r:gz') as tar:
27+
tar.extractall(data_dir)
28+
29+
def load_batch(filename):
30+
with open(os.path.join(extracted_dir, filename), 'rb') as f:
31+
batch = pickle.load(f, encoding='bytes')
32+
return batch[b'data'].reshape(-1, 3, 32, 32).transpose(0, 2, 3, 1), np.array(batch[b'labels'])
33+
34+
# Load training data
35+
train_images, train_labels = [], []
36+
for i in range(1, 6):
37+
images, labels = load_batch(f'data_batch_{i}')
38+
train_images.append(images)
39+
train_labels.append(labels)
40+
41+
train_images = np.concatenate(train_images)
42+
train_labels = np.concatenate(train_labels)
43+
44+
# Load test data
45+
test_images, test_labels = load_batch('test_batch')
46+
47+
if normalize:
48+
train_images = train_images.astype(np.float32) / 255.0
49+
test_images = test_images.astype(np.float32) / 255.0
50+
51+
return train_images, train_labels, test_images, test_labels

examples/weight-erasure.ipynb

Lines changed: 442 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)