/
mnist.py
311 lines (261 loc) · 8.3 KB
/
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""This script trains a NeuralNetClassifier on MNIST data, once with
skorch, once with pure PyTorch.
Apart from that change, both approaches are as close to eachother as
possible (e.g. performing the same validation steps).
At the end, we assert that the test accuracies are very close (1%
difference) and that skorch is not too much slower (at worst 30%
slower).
Call like this:
```
python examples/benchmarks/mnist.py
python examples/benchmarks/mnist.py --device cpu --num_samples 5000
```
When called the first time, this will download MNIST data to
examples/datasets/mldata (53M).
This script uses similar parameters as this one:
https://github.com/keras-team/keras/blob/0de2adf04b37aa972c955e69caf6917372b70a5b/examples/mnist_cnn.py
"""
import argparse
import time
import numpy as np
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import accuracy_score
from sklearn.utils import shuffle
import torch
from torch import nn
from skorch.utils import to_device
from skorch import NeuralNetClassifier
from skorch.callbacks import EpochScoring
BATCH_SIZE = 128
LEARNING_RATE = 0.1
MAX_EPOCHS = 12
def get_data(num_samples):
mnist = fetch_openml('mnist_784', as_frame=False)
torch.manual_seed(0)
X = mnist.data.astype('float32').reshape(-1, 1, 28, 28)
y = mnist.target.astype('int64')
X, y = shuffle(X, y, random_state=0)
X, y = X[:num_samples], y[:num_samples]
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
X_train /= 255
X_test /= 255
return X_train, X_test, y_train, y_test
class ClassifierModule(nn.Module):
def __init__(self):
super().__init__()
self.cnn = nn.Sequential(
nn.Conv2d(1, 32, (3, 3)),
nn.ReLU(),
nn.Conv2d(32, 64, (3, 3)),
nn.ReLU(),
nn.MaxPool2d((2, 2)),
nn.Dropout(0.25),
)
self.out = nn.Sequential(
nn.Linear(64 * 12 * 12, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, 10),
nn.Softmax(dim=-1),
)
def forward(self, X, **kwargs):
X = self.cnn(X)
X = X.reshape(-1, 64 * 12 * 12)
X = self.out(X)
return X
def performance_skorch(
X_train,
X_test,
y_train,
y_test,
batch_size,
device,
lr,
max_epochs,
):
torch.manual_seed(0)
net = NeuralNetClassifier(
ClassifierModule,
batch_size=batch_size,
optimizer=torch.optim.Adadelta,
lr=lr,
device=device,
max_epochs=max_epochs,
callbacks=[
('tr_acc', EpochScoring(
'accuracy',
lower_is_better=False,
on_train=True,
name='train_acc',
)),
],
)
net.fit(X_train, y_train)
y_pred = net.predict(X_test)
score = accuracy_score(y_test, y_pred)
return score
def report(losses, batch_sizes, y, y_proba, epoch, time, training=True):
template = "{} | epoch {:>2} | "
loss = np.average(losses, weights=batch_sizes)
y_pred = np.argmax(y_proba, axis=1)
acc = accuracy_score(y, y_pred)
template += "acc: {:.4f} | loss: {:.4f} | time: {:.2f}"
print(template.format(
'train' if training else 'valid', epoch + 1, acc, loss, time))
def train_torch(
model,
X,
y,
batch_size,
device,
lr,
max_epochs,
):
model = to_device(model, device)
idx_train, idx_valid = next(iter(StratifiedKFold(5).split(np.arange(len(X)), y)))
X_train, X_valid, y_train, y_valid = (
X[idx_train], X[idx_valid], y[idx_train], y[idx_valid])
dataset_train = torch.utils.data.TensorDataset(
torch.tensor(X_train),
torch.tensor(y_train),
)
iterator_train = torch.utils.data.DataLoader(dataset_train, batch_size=batch_size)
dataset_valid = torch.utils.data.TensorDataset(
torch.tensor(X_valid),
torch.tensor(y_valid),
)
iterator_valid = torch.utils.data.DataLoader(dataset_valid, batch_size=batch_size)
optimizer = torch.optim.Adadelta(model.parameters(), lr=lr)
criterion = nn.NLLLoss()
for epoch in range(max_epochs):
train_out = train_step(
model,
iterator_train,
batch_size=batch_size,
device=device,
criterion=criterion,
optimizer=optimizer,
)
report(y=y_train, epoch=epoch, training=True, **train_out)
valid_out = valid_step(
model,
iterator_valid,
batch_size=batch_size,
device=device,
criterion=criterion,
)
report(y=y_valid, epoch=epoch, training=False, **valid_out)
print('-' * 50)
return model
def train_step(model, iterator, device, criterion, batch_size, optimizer):
model.train()
y_preds = []
losses = []
batch_sizes = []
tic = time.time()
for Xi, yi in iterator:
Xi, yi = to_device(Xi, device), to_device(yi, device)
optimizer.zero_grad()
y_pred = model(Xi)
y_pred = torch.log(y_pred)
loss = criterion(y_pred, yi)
loss.backward()
optimizer.step()
y_preds.append(y_pred)
losses.append(loss.item())
batch_sizes.append(len(Xi))
toc = time.time()
return {
'losses': losses,
'batch_sizes': batch_sizes,
'y_proba': torch.cat(y_preds).cpu().detach().numpy(),
'time': toc - tic,
}
def valid_step(model, iterator, device, criterion, batch_size):
model.eval()
y_preds = []
losses = []
batch_sizes = []
tic = time.time()
with torch.no_grad():
for Xi, yi in iterator:
Xi, yi = to_device(Xi, device), to_device(yi, device)
y_pred = model(Xi)
y_pred = torch.log(y_pred)
loss = criterion(y_pred, yi)
y_preds.append(y_pred)
loss = loss.item()
losses.append(loss)
batch_sizes.append(len(Xi))
toc = time.time()
return {
'losses': losses,
'batch_sizes': batch_sizes,
'y_proba': torch.cat(y_preds).cpu().detach().numpy(),
'time': toc - tic,
}
def performance_torch(
X_train,
X_test,
y_train,
y_test,
batch_size,
device,
lr,
max_epochs,
):
torch.manual_seed(0)
model = ClassifierModule()
model = train_torch(
model,
X_train,
y_train,
batch_size=batch_size,
device=device,
max_epochs=max_epochs,
lr=lr,
)
X_test = torch.tensor(X_test).to(device)
with torch.no_grad():
y_pred = model(X_test).cpu().numpy().argmax(1)
return accuracy_score(y_test, y_pred)
def main(device, num_samples):
data = get_data(num_samples)
# trigger potential cuda call overhead
torch.zeros(1).to(device)
print("\nTesting skorch performance")
tic = time.time()
score_skorch = performance_skorch(
*data,
batch_size=BATCH_SIZE,
max_epochs=MAX_EPOCHS,
lr=LEARNING_RATE,
device=device,
)
time_skorch = time.time() - tic
print("\nTesting pure torch performance")
tic = time.time()
score_torch = performance_torch(
*data,
batch_size=BATCH_SIZE,
max_epochs=MAX_EPOCHS,
lr=LEARNING_RATE,
device=device,
)
time_torch = time.time() - tic
print("time skorch: {:.4f}, time torch: {:.4f}".format(
time_skorch, time_torch))
print("score skorch: {:.4f}, score torch: {:.4f}".format(
score_skorch, score_torch))
assert np.isclose(score_skorch, score_torch, rtol=0.01), "Scores are not close enough."
assert np.isclose(time_skorch, time_torch, rtol=0.3), "Times are not close enough."
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="skorch MNIST benchmark")
parser.add_argument('--device', type=str, default='cuda',
help='device (e.g. "cuda", "cpu")')
parser.add_argument('--num_samples', type=int, default=20000,
help='total number of samples to use')
args = parser.parse_args()
main(device=args.device, num_samples=args.num_samples)