-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.py
138 lines (108 loc) · 4.12 KB
/
helper_functions.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
import numpy as np
import matplotlib.pyplot as plt
import os
import pickle
import torch
def predict(X, network):
output = X.T
for layer in network:
output = layer.forward(output)
return output.argmax(axis=0)
def decision_surface(network, predict, ax, X, y, n_classes):
"""
A function to draw the decision surface for a given model in a 2-D plot.
Inputs:
- classifier: a classifier object
- ax: An matplotlib.pyplot axes object
- X: the whole dataset used in model (including both train and test sets).
- n_classes: the number of classes
Outputs:
- An plot containing the decision surface for the given classifier.
"""
# define bounds of the domain
min1, max1 = X[:, 0].min()-1, X[:, 0].max()+1
min2, max2 = X[:, 1].min()-1, X[:, 1].max()+1
# define the x and y scale
x1grid = np.arange(min1, max1, 0.1)
x2grid = np.arange(min2, max2, 0.1)
# create all of the lines and rows of the grid
xx, yy = np.meshgrid(x1grid, x2grid)
# flatten each grid to a vector
r1, r2 = xx.flatten(), yy.flatten()
r1, r2 = r1.reshape((len(r1), 1)), r2.reshape((len(r2), 1))
# horizontal stack vectors to create x1,x2 input for the model
grid = np.hstack((r1,r2))
# make predictions for the grid
yhat = predict(grid, network)
# reshape the predictions back into a grid
zz = yhat.reshape(xx.shape)
# plot the grid of x, y and z values as a surface
ax.contourf(xx, yy, zz, cmap='Set3', alpha=0.7)
# plot the datapoints
for i in range(n_classes):
class_i = X[y == i]
ax.scatter(class_i[:, 0], class_i[:, 1], label=f'class {i}')
ax.legend()
def decision_surface_torch(model, ax, X, y, n_classes):
"""
A function to draw the decision surface for a given model in a 2-D plot.
Inputs:
- classifier: a classifier object
- ax: An matplotlib.pyplot axes object
- X: the whole dataset used in model (including both train and test sets).
- n_classes: the number of classes
Outputs:
- An plot containing the decision surface for the given classifier.
"""
# define bounds of the domain
min1, max1 = X[:, 0].min()-1, X[:, 0].max()+1
min2, max2 = X[:, 1].min()-1, X[:, 1].max()+1
# define the x and y scale
x1grid = np.arange(min1, max1, 0.1)
x2grid = np.arange(min2, max2, 0.1)
# create all of the lines and rows of the grid
xx, yy = np.meshgrid(x1grid, x2grid)
# flatten each grid to a vector
r1, r2 = xx.flatten(), yy.flatten()
r1, r2 = r1.reshape((len(r1), 1)), r2.reshape((len(r2), 1))
# horizontal stack vectors to create x1,x2 input for the model
grid = np.hstack((r1,r2))
grid = torch.tensor(grid).type(torch.float)
model.eval()
with torch.inference_mode():
train_scores = model(grid)
yhat = train_scores.numpy().argmax(axis=1) # go from scores -> prediction labels
# make predictions for the grid
# yhat = predict(grid, network)
# reshape the predictions back into a grid
zz = yhat.reshape(xx.shape)
# plot the grid of x, y and z values as a surface
ax.contourf(xx, yy, zz, cmap='Set3', alpha=0.7)
# plot the datapoints
for i in range(n_classes):
class_i = X[y == i]
ax.scatter(class_i[:, 0], class_i[:, 1], label=f'class {i}')
ax.legend()
def load_cifar10_batch(filename):
with open(filename, 'rb') as f:
datadict = pickle.load(f, encoding='bytes')
X = datadict[b'data']
Y = datadict[b'labels']
X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype(np.float32)
return X, Y
def load_cifar10(path):
"""Load all batches from CIFAR-10 dataset."""
# load train data batch
xs = []
ys = []
for i in range(1, 6):
filename = os.path.join(path, f'data_batch_{i}')
X, y = load_cifar10_batch(filename)
xs.append(X)
ys.append(y)
Xtr = np.concatenate(xs)
ytr = np.concatenate(ys)
del X, y
# load test data batch
Xte, yte = load_cifar10_batch(os.path.join(path, 'test_batch'))
return Xtr, ytr, Xte, yte