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

Function test_donut

ann_class/xor_donut.py:104–152  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

102
103
104def test_donut():
105 # donut example
106 N = 1000
107 R_inner = 5
108 R_outer = 10
109
110 # distance from origin is radius + random normal
111 # angle theta is uniformly distributed between (0, 2pi)
112 R1 = np.random.randn(N//2) + R_inner
113 theta = 2*np.pi*np.random.random(N//2)
114 X_inner = np.concatenate([[R1 * np.cos(theta)], [R1 * np.sin(theta)]]).T
115
116 R2 = np.random.randn(N//2) + R_outer
117 theta = 2*np.pi*np.random.random(N//2)
118 X_outer = np.concatenate([[R2 * np.cos(theta)], [R2 * np.sin(theta)]]).T
119
120 X = np.concatenate([ X_inner, X_outer ])
121 Y = np.array([0]*(N//2) + [1]*(N//2))
122
123 n_hidden = 8
124 W1 = np.random.randn(2, n_hidden)
125 b1 = np.random.randn(n_hidden)
126 W2 = np.random.randn(n_hidden)
127 b2 = np.random.randn(1)
128 LL = [] # keep track of log-likelihoods
129 learning_rate = 0.00005
130 regularization = 0.2
131 last_error_rate = None
132 for i in range(3000):
133 pY, Z = forward(X, W1, b1, W2, b2)
134 ll = get_log_likelihood(Y, pY)
135 prediction = predict(X, W1, b1, W2, b2)
136 er = np.abs(prediction - Y).mean()
137 LL.append(ll)
138
139 # get gradients
140 gW2 = derivative_w2(Z, Y, pY)
141 gb2 = derivative_b2(Y, pY)
142 gW1 = derivative_w1(X, Z, Y, pY, W2)
143 gb1 = derivative_b1(Z, Y, pY, W2)
144
145 W2 += learning_rate * (gW2 - regularization * W2)
146 b2 += learning_rate * (gb2 - regularization * b2)
147 W1 += learning_rate * (gW1 - regularization * W1)
148 b1 += learning_rate * (gb1 - regularization * b1)
149 if i % 300 == 0:
150 print("i:", i, "ll:", ll, "classification rate:", 1 - er)
151 plt.plot(LL)
152 plt.show()
153
154
155if __name__ == '__main__':

Callers

nothing calls this directly

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