-
Notifications
You must be signed in to change notification settings - Fork 45
/
main_mnist.py
270 lines (223 loc) · 9.77 KB
/
main_mnist.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import random
import time
import math
from argparse import ArgumentParser
from collections import defaultdict
from itertools import islice
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
from tqdm.auto import tqdm
import torch
import torch.nn as nn
import torchvision
from grokfast import *
def cycle(iterable):
while True:
for x in iterable:
yield x
def compute_accuracy(network, dataset, device, N=2000, batch_size=50):
"""Computes accuracy of `network` on `dataset`.
"""
with torch.no_grad():
N = min(len(dataset), N)
batch_size = min(batch_size, N)
dataset_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
correct = 0
total = 0
for x, labels in islice(dataset_loader, N // batch_size):
logits = network(x.to(device))
predicted_labels = torch.argmax(logits, dim=1)
correct += torch.sum(predicted_labels == labels.to(device))
total += x.size(0)
return (correct / total).item()
def compute_loss(network, dataset, loss_function, device, N=2000, batch_size=50):
"""Computes mean loss of `network` on `dataset`.
"""
with torch.no_grad():
N = min(len(dataset), N)
batch_size = min(batch_size, N)
dataset_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
loss_fn = loss_function_dict[loss_function](reduction='sum')
one_hots = torch.eye(10, 10).to(device)
total = 0
points = 0
for x, labels in islice(dataset_loader, N // batch_size):
y = network(x.to(device))
if loss_function == 'CrossEntropy':
total += loss_fn(y, labels.to(device)).item()
elif loss_function == 'MSE':
total += loss_fn(y, one_hots[labels]).item()
points += len(labels)
return total / points
optimizer_dict = {
'AdamW': torch.optim.AdamW,
'Adam': torch.optim.Adam,
'SGD': torch.optim.SGD
}
activation_dict = {
'ReLU': nn.ReLU,
'Tanh': nn.Tanh,
'Sigmoid': nn.Sigmoid,
'GELU': nn.GELU
}
loss_function_dict = {
'MSE': nn.MSELoss,
'CrossEntropy': nn.CrossEntropyLoss
}
def main(args):
log_freq = math.ceil(args.optimization_steps / 150)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.float32
torch.set_default_dtype(dtype)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
# load dataset
train = torchvision.datasets.MNIST(root=args.download_directory, train=True,
transform=torchvision.transforms.ToTensor(), download=True)
test = torchvision.datasets.MNIST(root=args.download_directory, train=False,
transform=torchvision.transforms.ToTensor(), download=True)
train = torch.utils.data.Subset(train, range(args.train_points))
train_loader = torch.utils.data.DataLoader(train, batch_size=args.batch_size, shuffle=True)
assert args.activation in activation_dict, f"Unsupported activation function: {args.activation}"
activation_fn = activation_dict[args.activation]
# create model
layers = [nn.Flatten()]
for i in range(args.depth):
if i == 0:
layers.append(nn.Linear(784, args.width))
layers.append(activation_fn())
elif i == args.depth - 1:
layers.append(nn.Linear(args.width, 10))
else:
layers.append(nn.Linear(args.width, args.width))
layers.append(activation_fn())
mlp = nn.Sequential(*layers).to(device)
with torch.no_grad():
for p in mlp.parameters():
p.data = args.initialization_scale * p.data
nparams = sum([p.numel() for p in mlp.parameters() if p.requires_grad])
print(f'Number of parameters: {nparams}')
# create optimizer
assert args.optimizer in optimizer_dict, f"Unsupported optimizer choice: {args.optimizer}"
optimizer = optimizer_dict[args.optimizer](mlp.parameters(), lr=args.lr, weight_decay=args.weight_decay)
# define loss function
assert args.loss_function in loss_function_dict
loss_fn = loss_function_dict[args.loss_function]()
train_losses, test_losses, train_accuracies, test_accuracies = [], [], [], []
norms, last_layer_norms, log_steps = [], [], []
grads = None
steps = 0
one_hots = torch.eye(10, 10).to(device)
with tqdm(total=args.optimization_steps, dynamic_ncols=True) as pbar:
for x, labels in islice(cycle(train_loader), args.optimization_steps):
do_log = (steps < 30) or (steps < 150 and steps % 10 == 0) or steps % log_freq == 0
if do_log:
train_losses.append(compute_loss(mlp, train, args.loss_function, device, N=len(train)))
train_accuracies.append(compute_accuracy(mlp, train, device, N=len(train)))
test_losses.append(compute_loss(mlp, test, args.loss_function, device, N=len(test)))
test_accuracies.append(compute_accuracy(mlp, test, device, N=len(test)))
log_steps.append(steps)
pbar.set_description(
"L: {0:1.1e}|{1:1.1e}. A: {2:2.1f}%|{3:2.1f}%".format(
train_losses[-1],
test_losses[-1],
train_accuracies[-1] * 100,
test_accuracies[-1] * 100,
)
)
y = mlp(x.to(device))
if args.loss_function == 'CrossEntropy':
loss = loss_fn(y, labels.to(device))
elif args.loss_function == 'MSE':
loss = loss_fn(y, one_hots[labels])
optimizer.zero_grad()
loss.backward()
#######
trigger = False
if args.filter == "none":
pass
elif args.filter == "ma":
grads = gradfilter_ma(mlp, grads=grads, window_size=args.window_size, lamb=args.lamb, trigger=trigger)
elif args.filter == "ema":
grads = gradfilter_ema(mlp, grads=grads, alpha=args.alpha, lamb=args.lamb)
else:
raise ValueError(f"Invalid gradient filter type `{args.filter}`")
#######
optimizer.step()
steps += 1
pbar.update(1)
if do_log:
title = (f"MNIST Image Classification")
plt.plot(log_steps, train_accuracies, label="train")
plt.plot(log_steps, test_accuracies, label="val")
plt.legend()
plt.title(title)
plt.xlabel("Optimization Steps")
plt.ylabel("Accuracy")
plt.xscale("log", base=10)
plt.grid()
plt.savefig(f"results/mnist_acc_{args.label}.png", dpi=150)
plt.close()
plt.plot(log_steps, train_losses, label="train")
plt.plot(log_steps, test_losses, label="val")
plt.legend()
plt.title(title)
plt.xlabel("Optimization Steps")
plt.ylabel(f"{args.loss_function} Loss")
plt.xscale("log", base=10)
plt.yscale("log", base=10)
plt.grid()
plt.savefig(f"results/mnist_loss_{args.label}.png", dpi=150)
plt.close()
torch.save({
'its': log_steps,
'train_acc': train_accuracies,
'train_loss': train_losses,
'val_acc': test_accuracies,
'val_loss': test_losses,
}, f"results/mnist_{args.label}.pt")
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("--label", default="")
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--train_points", type=int, default=1000)
parser.add_argument("--optimization_steps", type=int, default=100000)
parser.add_argument("--batch_size", type=int, default=200)
parser.add_argument("--loss_function", type=str, default="MSE")
parser.add_argument("--optimizer", type=str, default="AdamW")
parser.add_argument("--weight_decay", type=float, default=0.01)
parser.add_argument("--lr", type=float, default=1e-3)
parser.add_argument("--initialization_scale", type=float, default=8.0)
parser.add_argument("--download_directory", type=str, default=".")
parser.add_argument("--depth", type=int, default=3)
parser.add_argument("--width", type=int, default=200)
parser.add_argument("--activation", type=str, default="ReLU")
# Grokfast
parser.add_argument("--filter", type=str, choices=["none", "ma", "ema", "fir"], default="none")
parser.add_argument("--alpha", type=float, default=0.99)
parser.add_argument("--window_size", type=int, default=100)
parser.add_argument("--lamb", type=float, default=5.0)
args = parser.parse_args()
filter_str = ('_' if args.label != '' else '') + args.filter
window_size_str = f'_w{args.window_size}'
alpha_str = f'_a{args.alpha:.3f}'.replace('.', '')
lamb_str = f'_l{args.lamb:.2f}'.replace('.', '')
if args.filter == 'none':
filter_suffix = ''
elif args.filter == 'ma':
filter_suffix = window_size_str + lamb_str
elif args.filter == 'ema':
filter_suffix = alpha_str + lamb_str
else:
raise ValueError(f"Unrecognized filter type {args.filter}")
optim_suffix = ''
if args.weight_decay != 0:
optim_suffix = optim_suffix + f'_wd{args.weight_decay:.1e}'.replace('.', '')
if args.lr != 1e-3:
optim_suffix = optim_suffix + f'_lrx{int(args.lr / 1e-3)}'
args.label = args.label + filter_str + filter_suffix + optim_suffix
print(f'Experiment results saved under name: {args.label}')
main(args)