MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / test_xor

Function test_xor

ann_class/xor_donut.py:67–101  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

65
66
67def test_xor():
68 X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
69 Y = np.array([0, 1, 1, 0])
70 W1 = np.random.randn(2, 5)
71 b1 = np.zeros(5)
72 W2 = np.random.randn(5)
73 b2 = 0
74 LL = [] # keep track of log-likelihoods
75 learning_rate = 1e-2
76 regularization = 0.
77 last_error_rate = None
78 for i in range(30000):
79 pY, Z = forward(X, W1, b1, W2, b2)
80 ll = get_log_likelihood(Y, pY)
81 prediction = predict(X, W1, b1, W2, b2)
82 er = np.mean(prediction != Y)
83
84 LL.append(ll)
85
86 # get gradients
87 gW2 = derivative_w2(Z, Y, pY)
88 gb2 = derivative_b2(Y, pY)
89 gW1 = derivative_w1(X, Z, Y, pY, W2)
90 gb1 = derivative_b1(Z, Y, pY, W2)
91
92 W2 += learning_rate * (gW2 - regularization * W2)
93 b2 += learning_rate * (gb2 - regularization * b2)
94 W1 += learning_rate * (gW1 - regularization * W1)
95 b1 += learning_rate * (gb1 - regularization * b1)
96 if i % 1000 == 0:
97 print(ll)
98
99 print("final classification rate:", np.mean(prediction == Y))
100 plt.plot(LL)
101 plt.show()
102
103
104def test_donut():

Callers 1

xor_donut.pyFile · 0.85

Calls 7

get_log_likelihoodFunction · 0.85
forwardFunction · 0.70
predictFunction · 0.70
derivative_w2Function · 0.70
derivative_b2Function · 0.70
derivative_w1Function · 0.70
derivative_b1Function · 0.70

Tested by

no test coverage detected