-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
28 lines (25 loc) · 807 Bytes
/
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
class Model(object):
def __init__(self, layers, optimizer, loss):
self.layers = layers
self.optimizer = optimizer
self.loss = loss
def initialize(self):
for layer in self.layers:
layer.initialize()
self.optimizer.initialize()
def predict(self, A):
for layer in self.layers:
A = layer.forward(A, train=False)
return A
def train(self, A, Y):
loss = 0
for layer in self.layers:
A = layer.forward(A, train=True)
loss += layer.get_l2_loss()
Y_hat = A
cost, dA = self.loss.compute_loss_and_grad(Y, Y_hat)
loss += cost
for layer in reversed(self.layers):
dA = layer.backward(dA)
self.optimizer.update()
return loss