-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor.py
More file actions
55 lines (44 loc) · 1.29 KB
/
xor.py
File metadata and controls
55 lines (44 loc) · 1.29 KB
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
from typing import List
from numbers import Real
import random
from nn import Module
from nn.functional import sigmoid
from autograd import Variable, rand
from optim import SGD
class XorSolver(Module):
def __init__(self) -> None:
self.w_11 = rand()
self.w_12 = rand()
self.b_1 = rand()
self.w_21 = rand()
self.w_22 = rand()
self.b_2 = rand()
self.w_31 = rand()
self.w_32 = rand()
self.b_3 = rand()
def forward(self, x: List[Real]) -> Variable:
s_1 = x[0] * self.w_11 + x[1] * self.w_12 - self.b_1
z_1 = sigmoid(s_1)
s_2 = x[0] * self.w_21 + x[1] * self.w_22 - self.b_2
z_2 = sigmoid(s_2)
s_3 = z_1 * self.w_31 + z_2 * self.w_32 - self.b_3
return sigmoid(s_3)
data = [
[[0.0, 0.0], 0.0],
[[0.0, 1.0], 1.0],
[[1.0, 0.0], 1.0],
[[1.0, 1.0], 0.0]
]
model = XorSolver()
optimizer = SGD(model.parameters(), lr=0.3)
for i in range(3000):
shuffled_data = random.sample(data, len(data))
for X, y in shuffled_data:
optimizer.zero_grad()
y_p = model(X)
loss = (y_p - y) ** 2
loss.backward()
optimizer.step()
for X, y in data:
y_p = model(X)
print(f'X = {X}, y_p = {y_p.value:.3f}, y = {y}')