forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor_donut.py
145 lines (115 loc) · 4.21 KB
/
xor_donut.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# revisiting the XOR and donut problems to show how features
# can be learned automatically using neural networks.
#
# the notes for this class can be found at:
# https://deeplearningcourses.com/c/data-science-deep-learning-in-python
# https://www.udemy.com/data-science-deep-learning-in-python
import numpy as np
import matplotlib.pyplot as plt
# for binary classification! no softmax here
def forward(X, W1, b1, W2, b2):
# Z = 1 / (1 + np.exp( -(X.dot(W1) + b1) ))
Z = np.tanh(X.dot(W1) + b1)
activation = Z.dot(W2) + b2
Y = 1 / (1 + np.exp(-activation))
return Y, Z
def predict(X, W1, b1, W2, b2):
Y, _ = forward(X, W1, b1, W2, b2)
return np.round(Y)
def derivative_w2(Z, T, Y):
# Z is (N, M)
return (T - Y).dot(Z)
def derivative_b2(T, Y):
return (T - Y).sum()
def derivative_w1(X, Z, T, Y, W2):
# dZ = np.outer(T-Y, W2) * Z * (1 - Z) # this is for sigmoid activation
dZ = np.outer(T-Y, W2) * (1 - Z * Z) # this is for tanh activation
return X.T.dot(dZ)
def derivative_b1(Z, T, Y, W2):
# dZ = np.outer(T-Y, W2) * Z * (1 - Z) # this is for sigmoid activation
dZ = np.outer(T-Y, W2) * (1 - Z * Z) # this is for tanh activation
return dZ.sum(axis=0)
def cost(T, Y):
# tot = 0
# for n in xrange(len(T)):
# if T[n] == 1:
# tot += np.log(Y[n])
# else:
# tot += np.log(1 - Y[n])
# return tot
return np.sum(T*np.log(Y) + (1-T)*np.log(1-Y))
def test_xor():
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
Y = np.array([0, 1, 1, 0])
W1 = np.random.randn(2, 5)
b1 = np.zeros(5)
W2 = np.random.randn(5)
b2 = 0
LL = [] # keep track of likelihoods
learning_rate = 10e-3
regularization = 0.
last_error_rate = None
for i in xrange(30000):
pY, Z = forward(X, W1, b1, W2, b2)
ll = cost(Y, pY)
prediction = predict(X, W1, b1, W2, b2)
er = np.mean(prediction != Y)
if er != last_error_rate:
last_error_rate = er
print "error rate:", er
print "true:", Y
print "pred:", prediction
# if LL and ll < LL[-1]:
# print "early exit"
# break
LL.append(ll)
W2 += learning_rate * (derivative_w2(Z, Y, pY) - regularization * W2)
b2 += learning_rate * (derivative_b2(Y, pY) - regularization * b2)
W1 += learning_rate * (derivative_w1(X, Z, Y, pY, W2) - regularization * W1)
b1 += learning_rate * (derivative_b1(Z, Y, pY, W2) - regularization * b1)
if i % 1000 == 0:
print ll
print "final classification rate:", np.mean(prediction == Y)
plt.plot(LL)
plt.show()
def test_donut():
# donut example
N = 1000
R_inner = 5
R_outer = 10
# distance from origin is radius + random normal
# angle theta is uniformly distributed between (0, 2pi)
R1 = np.random.randn(N/2) + R_inner
theta = 2*np.pi*np.random.random(N/2)
X_inner = np.concatenate([[R1 * np.cos(theta)], [R1 * np.sin(theta)]]).T
R2 = np.random.randn(N/2) + R_outer
theta = 2*np.pi*np.random.random(N/2)
X_outer = np.concatenate([[R2 * np.cos(theta)], [R2 * np.sin(theta)]]).T
X = np.concatenate([ X_inner, X_outer ])
Y = np.array([0]*(N/2) + [1]*(N/2))
n_hidden = 8
W1 = np.random.randn(2, n_hidden)
b1 = np.random.randn(n_hidden)
W2 = np.random.randn(n_hidden)
b2 = np.random.randn(1)
LL = [] # keep track of likelihoods
learning_rate = 0.00005
regularization = 0.2
last_error_rate = None
for i in xrange(160000):
pY, Z = forward(X, W1, b1, W2, b2)
ll = cost(Y, pY)
prediction = predict(X, W1, b1, W2, b2)
er = np.abs(prediction - Y).mean()
LL.append(ll)
W2 += learning_rate * (derivative_w2(Z, Y, pY) - regularization * W2)
b2 += learning_rate * (derivative_b2(Y, pY) - regularization * b2)
W1 += learning_rate * (derivative_w1(X, Z, Y, pY, W2) - regularization * W1)
b1 += learning_rate * (derivative_b1(Z, Y, pY, W2) - regularization * b1)
if i % 100 == 0:
print "i:", i, "ll:", ll, "classification rate:", 1 - er
plt.plot(LL)
plt.show()
if __name__ == '__main__':
# test_xor()
test_donut()