-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
36 lines (28 loc) · 1.05 KB
/
main.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
import numpy as np
import mllib
class Model(mllib.bases.Module):
def __init__(self):
self.layer1 = mllib.models.Dense(10, 20)
self.layer2 = mllib.models.Dense(20, 20)
self.layer3 = mllib.models.Dense(20, 10)
self.nonlin = mllib.models.Sigmoid()
def forward(self, inputs):
return self.nonlin(self.layer3(self.nonlin(self.layer2(self.nonlin(self.layer1(inputs)))))) # forward pass
def backward(self, error, lr):
error = self.nonlin.backward(error, lr)
error = self.layer3.backward(error, lr)
error = self.nonlin.backward(error, lr)
error = self.layer2.backward(error, lr)
error = self.nonlin.backward(error, lr)
error = self.layer1.backward(error, lr)
return error
model = Model()
x = np.random.randn(10)
y = np.random.randn(10)
def meanSquaredError(targets, values):
return np.sum(np.abs(targets - values))
print(meanSquaredError(y, model(x)))
for _ in range(10):
error = y - model(x)
model.backward(error, .1)
print(meanSquaredError(y, model(x)))