-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
251 lines (235 loc) · 7.72 KB
/
model.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
from layers.convolution2d import Conv2D
from layers.maxpooling2d import MaxPool2D
from layers.fullyconnected import FC
from activations import Activation, get_activation, Sigmoid, ReLU, LinearActivation, Tanh
import pickle
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
from random import shuffle
class Model:
def __init__(self, arch, criterion, optimizer, name=None):
"""
Initialize the model.
args:
arch: dictionary containing the architecture of the model
criterion: loss
optimizer: optimizer
name: name of the model
"""
if name is None:
self.model = arch
self.criterion = criterion
self.optimizer = optimizer
self.layers_names = list(arch.keys())
else:
self.model, self.criterion, self.optimizer, self.layers_names = self.load_model(name)
def is_layer(self, layer):
"""
Check if the layer is a layer.
args:
layer: layer to be checked
returns:
True if the layer is a layer, False otherwise
"""
if type(layer) == MaxPool2D or type(layer) == FC or type(layer) == Conv2D:
return True
else:
return False
def is_activation(self, layer):
"""
Check if the layer is an activation function.
args:
layer: layer to be checked
returns:
True if the layer is an activation function, False otherwise
"""
if type(layer) == Sigmoid or type(layer) == LinearActivation or type(layer) == ReLU or type(layer) == Tanh:
return True
else:
return False
def forward(self, x):
"""
Forward pass through the model.
args:
x: input to the model
returns:
output of the model
"""
tmp = []
A = x
for l in range(0, len(self.layers_names), 2):
Z = self.model[self.layers_names[l]].forward(A)
tmp.append(np.copy(Z)) # hint add a copy of Z to tmp
A = self.model[self.layers_names[l + 1]].forward(Z)
tmp.append(np.copy(A)) # hint add a copy of A to tmp
return tmp
def backward(self, dAL, tmp, x):
"""
Backward pass through the model.
args:
dAL: derivative of the cost with respect to the output of the model
tmp: list containing the intermediate values of Z and A
x: input to the model
returns:
gradients of the model
"""
dA = dAL
grads = {}
for l in range(len(self.layers_names) - 1, -1, -2):
if l > 2:
Z, A = tmp[l - 1], tmp[l - 2]
else:
Z, A = tmp[l - 1], x
dZ = self.model[self.layers_names[l]].backward(dA, Z)
dA, grad = self.model[self.layers_names[l - 1]].backward(dZ, A)
grads[self.layers_names[l - 1]] = grad
return grads
def update(self, grads):
"""
Update the model.
args:
grads: gradients of the model
"""
for name in self.layers_names:
if self.is_layer(self.model[name]) and (type(self.model[
name]) != MaxPool2D):
self.model[name].update_parameters(self.optimizer, grads[name])
def one_epoch(self, x, y):
"""
One epoch of training.
args:
x: input to the model
y: labels
batch_size: batch size
returns:
loss
"""
tmp = self.forward(x)
AL = tmp[-1]
loss = self.criterion.compute(AL, y)
dAL = self.criterion.backward(AL, y)
grads = self.backward(dAL, tmp, x)
self.update(grads)
return loss
def save(self, name):
"""
Save the model.
args:
name: name of the model
"""
with open(name, 'wb') as f:
pickle.dump((self.model, self.criterion, self.optimizer, self.layers_names), f)
def load_model(self, name):
"""
Load the model.
args:
name: name of the model
returns:
model, criterion, optimizer, layers_names
"""
with open(name, 'rb') as f:
return pickle.load(f)
def shuffle(self, m, shuffling):
order = list(range(m))
if shuffling:
np.random.shuffle(order)
return order
def batch(self, X, y, batch_size, index, order):
"""
Get a batch of data.
args:
X: input to the model
y: labels
batch_size: batch size
index: index of the batch
e.g: if batch_size = 3 and index = 1 then the batch will be from index [3, 4, 5]
order: order of the data
returns:
bx, by: batch of data
"""
start_index = index * batch_size
last_index = (index + 1) * batch_size
batch = order[start_index: last_index]
batch_np = np.asarray(batch)
if len(X.shape) == 2:
bx = X[:, batch_np]
by = y[:, batch_np]
return bx, by
else:
bx = []
by = []
for i in batch_np:
bx.append(X[i, :, :, :])
by.append(y[0][i])
temp_x = np.array(bx)
temp_y = np.array(by).reshape(1, -1)
return temp_x, temp_y
def compute_loss(self, X, y, batch_size):
"""
Compute the loss.
args:
X: input to the model
y: labels
Batch_Size: batch size
returns:
loss
"""
if len(X.shape) == 4:
m = X.shape[0]
else:
m = X.shape[1]
order = self.shuffle(m, False)
cost = 0
for b in range(m // batch_size):
bx, by = self.batch(X, y, batch_size, b, order)
tmp = self.forward(bx)
AL = tmp[-1]
cost += self.criterion.compute_cost(AL, y)(m // batch_size)
return cost
def train(self, X, y, epochs, val=None, batch_size=3, shuffling=False, verbose=1, save_after=None):
"""
Train the model.
args:
X: input to the model
y: labels
epochs: number of epochs
val: validation data
batch_size: batch size
shuffling: if True shuffle the data
verbose: if 1 print the loss after each epoch
save_after: save the model after training
"""
train_cost = []
val_cost = []
if len(X.shape) == 4:
m = X.shape[0]
else:
m = X.shape[1]
for e in tqdm(range(1, epochs + 1)):
order = self.shuffle(m, shuffling)
cost = 0
for b in range(m // batch_size):
bx, by = self.batch(X, y, batch_size, b, order)
cost += self.one_epoch(bx, by) / (m // batch_size)
train_cost.append(cost)
if val is not None:
val_cost.append(self.compute_loss(X, y, batch_size))
if verbose != False:
if e % verbose == 0:
print("Epoch {}: train cost = {}".format(e, cost))
if val is not None:
print("Epoch {}: val cost = {}".format(e, val_cost[-1]))
if save_after is not None:
self.save(save_after)
return train_cost, val_cost
def predict(self, X):
"""
Predict the output of the model.
args:
X: input to the model
returns:
predictions
"""
tmp = self.forward(X)
return tmp[-1]