-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtiny_xor_net.py
80 lines (64 loc) · 1.59 KB
/
tiny_xor_net.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
import tinytorch as tt
class XorNet(tt.Module):
def __init__(self):
super().__init__()
self.l1 = tt.Linear(2, 2)
self.l2 = tt.Linear(2, 1)
def forward(self, x):
x = self.l1(x)
x = tt.tanh(x)
x = self.l2(x)
x = tt.tanh(x)
return x
lr = 1e-1
model = XorNet()
optimizer = tt.SGD(model.parameters(), lr=lr)
x = tt.tensor(
[
[0, 0],
[1, 0],
[0, 1],
[1, 1],
]
)
y = tt.tensor(
[
[0],
[1],
[1],
[0],
]
)
ITER = 2000
pred = model(x)
print(pred)
for idx in range(ITER):
pred = model(x)
loss = tt.mse_loss(pred, y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
print(loss.item())
print("=" * 100)
pred = model(x)
print(pred)
class Max(Function):
def forward(x: Tensor, shape):
axis = tuple(idx for idx, (a, b) in enumerate(zip(x.shape, shape)) if a != b)
return x.max(axis, keepdims=True)
def backward(ctx: Function, grad_output: Tensor) -> Tensor:
x, shape = ctx.args
ret = Max.forward(x, shape)
max_is_1s: np.ndarray = np.full_like(x.data, 1.0) - (
x.data < np.broadcast_to(ret.data, x.shape)
)
axis = tuple(
idx
for idx, (a, b) in enumerate(zip(max_is_1s.shape, grad_output.shape))
if a != b
)
div = max_is_1s.sum(axis, keepdims=True)
div = np.broadcast_to(div, x.shape)
maximum = max_is_1s / div
maximum = maximum * np.broadcast_to(grad_output.data, x.shape) #
return maximum