-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforget.py
186 lines (154 loc) · 7.49 KB
/
forget.py
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
import torch
import torch.functional as F
import torch.nn as nn
import random
from model import CNN
from utils import get_loaders, entropy, accuracy, confidence, EWC
import wandb
run = wandb.init(
project="machine-unlearning",
config={
"learning_rate": 1.5e-4,
"epochs": 200,
})
torch.manual_seed(1)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = CNN()
train_dl, valid_dl, forget_dl = get_loaders()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
model.load_state_dict(torch.load('weights/model.pt'))
model.to(device)
def forget(model, num_epochs, forget_dl, sample_size=200, importance=2500):
accuracy_train = 0
accuracy_valid = 0
with torch.no_grad():
for x_batch, y_batch in valid_dl:
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
pred = model(x_batch)
is_correct = (
torch.argmax(pred, dim=1) == y_batch
).float()
accuracy_valid += is_correct.sum()
accuracy_valid /= len(valid_dl.dataset)
for x_batch, y_batch in train_dl:
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
pred = model(x_batch)
is_correct = (
torch.argmax(pred, dim=1) == y_batch
).float()
accuracy_train += is_correct.sum()
accuracy_train /= len(train_dl.dataset)
print(f'Initial Train Acc: {accuracy_train:.4f} Valid Acc: {accuracy_valid:.4f}')
loss_hist_forget = [0] * num_epochs
accuracy_hist_forget = [0] * num_epochs
entropy_hist_forget = [0] * num_epochs
confidence_hist_forget = [0] * num_epochs
loss_hist_train = [0] * num_epochs
accuracy_hist_train = [0] * num_epochs
entropy_hist_train = [0] * num_epochs
confidence_hist_train = [0] * num_epochs
loss_hist_valid = [0] * num_epochs
accuracy_hist_valid = [0] * num_epochs
entropy_hist_valid = [0] * num_epochs
confidence_hist_valid = [0] * num_epochs
for epoch in range(num_epochs):
model.train()
for x_batch, y_batch in forget_dl:
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
pred = model(x_batch).to(device)
# Uniform Prediction
uniform_pred = 0.1 * torch.ones(*y_batch.shape, 10).to(device)
# Random Prediction
random_pred = torch.zeros(*y_batch.shape, 10)
random_indices = torch.tensor([random.randint(0, 9) for _ in range(y_batch.shape[0])])
random_pred.scatter_(1, random_indices.view(-1, 1), 1)
random_pred = random_pred.to(device)
size = 0
imgs = []
for x_b, y_b in train_dl:
x_b, y_b = x_b.to(device), y_b.to(device)
imgs.append(x_b)
size += 1
if size >= 200:
break
ewc = EWC(model, imgs)
loss = loss_fn(pred, random_pred) + importance * ewc.penalty(model)
loss.backward()
optimizer.step()
optimizer.zero_grad()
is_correct = (
torch.argmax(pred, dim=1) == y_batch
).float()
loss_hist_forget[epoch] += loss.item() * y_batch.size(0)
accuracy_hist_forget[epoch] += is_correct.sum()
entropy_hist_forget[epoch] += entropy(pred)
confidence_hist_forget[epoch] += confidence(pred)
accuracy_hist_forget[epoch] /= len(forget_dl.dataset)
loss_hist_forget[epoch] /= len(forget_dl.dataset)
entropy_hist_forget[epoch] /= len(forget_dl.dataset)
confidence_hist_forget[epoch] /= len(forget_dl.dataset)
model.eval()
with torch.no_grad():
for x_batch, y_batch in valid_dl:
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
pred = model(x_batch)
loss = loss_fn(pred, y_batch)
loss_hist_valid[epoch] += \
loss.item() * y_batch.size(0)
is_correct = (
torch.argmax(pred, dim=1) == y_batch
).float()
accuracy_hist_valid[epoch] += is_correct.sum()
entropy_hist_valid[epoch] += entropy(pred)
confidence_hist_valid[epoch] += confidence(pred)
for x_batch, y_batch in train_dl:
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
pred = model(x_batch)
loss = loss_fn(pred, y_batch)
loss_hist_train[epoch] += \
loss.item() * y_batch.size(0)
is_correct = (
torch.argmax(pred, dim=1) == y_batch
).float()
accuracy_hist_train[epoch] += is_correct.sum()
entropy_hist_train[epoch] += entropy(pred)
confidence_hist_train[epoch] += confidence(pred)
loss_hist_valid[epoch] /= len(valid_dl.dataset)
accuracy_hist_valid[epoch] /= len(valid_dl.dataset)
entropy_hist_valid[epoch] /= len(valid_dl.dataset)
confidence_hist_valid[epoch] /= len(valid_dl.dataset)
loss_hist_train[epoch] /= len(train_dl.dataset)
accuracy_hist_train[epoch] /= len(train_dl.dataset)
entropy_hist_train[epoch] /= len(train_dl.dataset)
confidence_hist_train[epoch] /= len(train_dl.dataset)
print(f'----- Epoch {epoch+1} -----\n'
f' Forget Acc: {accuracy_hist_forget[epoch]:.4f}\n'
f' Forget Loss: {loss_hist_forget[epoch]:.4f}\n'
f' Forget Entropy: {entropy_hist_forget[epoch]:.4f}\n'
f' Forget Confidence: {confidence_hist_forget[epoch]:.4f}\n\n'
f' Train Acc: {accuracy_hist_train[epoch]:.4f}\n'
f' Train Loss: {loss_hist_train[epoch]:.4f}\n'
f' Train Entropy: {entropy_hist_train[epoch]:.4f}\n'
f' Train Confidence: {confidence_hist_train[epoch]:.4f}\n\n'
f' Valid Acc: {accuracy_hist_valid[epoch]:.4f}\n'
f' Valid Loss: {loss_hist_valid[epoch]:.4f}\n'
f' Valid Entropy: {entropy_hist_valid[epoch]:.4f}\n'
f' Valid Confidence: {confidence_hist_valid[epoch]:.4f}\n'
f'----------------------\n')
wandb.log({"Forget Acc": accuracy_hist_forget[epoch],
"Forget Loss": loss_hist_forget[epoch],
"Forget Entropy": entropy_hist_forget[epoch],
"Forget Confidence": confidence_hist_forget[epoch],
"Train Acc": accuracy_hist_train[epoch],
"Train Loss": loss_hist_train[epoch],
"Train Entropy": entropy_hist_train[epoch],
"Train Confidence": confidence_hist_train[epoch],
"Valid Loss": loss_hist_valid[epoch],
"Valid Acc": accuracy_hist_valid[epoch],
"Valid Entropy": entropy_hist_valid[epoch],
"Valid Confidence": confidence_hist_valid[epoch],
})
forget(model, 200, forget_dl)
# TODO:
# Basiline for Random Input & Output