forked from TheIndependentCode/Neural-Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor.py
35 lines (28 loc) · 844 Bytes
/
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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from dense import Dense
from activations import Tanh
from losses import mse, mse_prime
from network import train, predict
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()
]
# train
train(network, mse, mse_prime, X, Y, epochs=10000, learning_rate=0.1)
# decision boundary plot
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()