-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_xor.py
47 lines (34 loc) · 1.12 KB
/
demo_xor.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
import numpy as np
from NeuralJAXwork.layers import Dense
from NeuralJAXwork.activations import Tanh
from NeuralJAXwork.losses import MSE
from NeuralJAXwork.network import train, predict
# XOR dataset
X = np.reshape([[0, 0], [0, 1], [1, 0], [1, 1]], (4, 2, 1))
Y = np.reshape([[0], [1], [1], [0]], (4, 1, 1))
network = [
Dense(2, 3),
Tanh(),
Dense(3, 1),
Tanh()
]
# setting loss as Mean Squared Error
loss = MSE()
# train
train(network, loss, X, Y, epochs=100, learning_rate=0.1)
print("Training Succesful!")
# # if you want to plot the decision boundary in 3D
# # when you run this script, uncomment the following lines
# # and install matplotlib
# if __name__ == "__main__":
# import matplotlib.pyplot as plt
# points = []
# for x in np.linspace(0, 1, 20):
# for y in np.linspace(0, 1, 20):
# z = predict(network, [[x], [y]])
# points.append([x, y, z[0,0]])
# points = np.array(points)
# fig = plt.figure()
# ax = fig.add_subplot(111, projection="3d")
# ax.scatter(points[:, 0], points[:, 1], points[:, 2], c=points[:, 2], cmap="winter")
# plt.show()