-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor_donut.py
157 lines (121 loc) · 4.4 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
# for binary classification! no softmax here
def forward(X, W1, b1, W2, b2):
# sigmoid
# Z = 1 / (1 + np.exp( -(X.dot(W1) + b1) ))
# tanh
# Z = np.tanh(X.dot(W1) + b1)
# relu
Z = X.dot(W1) + b1
Z = Z * (Z > 0)
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
dZ = np.outer(T-Y, W2) * (Z > 0) # this is for relu 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
dZ = np.outer(T-Y, W2) * (Z > 0) # this is for relu activation
return dZ.sum(axis=0)
def get_log_likelihood(T, Y):
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 log-likelihoods
learning_rate = 1e-2
regularization = 0.
last_error_rate = None
for i in range(30000):
pY, Z = forward(X, W1, b1, W2, b2)
ll = get_log_likelihood(Y, pY)
prediction = predict(X, W1, b1, W2, b2)
er = np.mean(prediction != Y)
LL.append(ll)
# get gradients
gW2 = derivative_w2(Z, Y, pY)
gb2 = derivative_b2(Y, pY)
gW1 = derivative_w1(X, Z, Y, pY, W2)
gb1 = derivative_b1(Z, Y, pY, W2)
W2 += learning_rate * (gW2 - regularization * W2)
b2 += learning_rate * (gb2 - regularization * b2)
W1 += learning_rate * (gW1 - regularization * W1)
b1 += learning_rate * (gb1 - 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 log-likelihoods
learning_rate = 0.00005
regularization = 0.2
last_error_rate = None
for i in range(3000):
pY, Z = forward(X, W1, b1, W2, b2)
ll = get_log_likelihood(Y, pY)
prediction = predict(X, W1, b1, W2, b2)
er = np.abs(prediction - Y).mean()
LL.append(ll)
# get gradients
gW2 = derivative_w2(Z, Y, pY)
gb2 = derivative_b2(Y, pY)
gW1 = derivative_w1(X, Z, Y, pY, W2)
gb1 = derivative_b1(Z, Y, pY, W2)
W2 += learning_rate * (gW2 - regularization * W2)
b2 += learning_rate * (gb2 - regularization * b2)
W1 += learning_rate * (gW1 - regularization * W1)
b1 += learning_rate * (gb1 - regularization * b1)
if i % 300 == 0:
print("i:", i, "ll:", ll, "classification rate:", 1 - er)
plt.plot(LL)
plt.show()
if __name__ == '__main__':
# test_xor()
test_donut()