()
| 65 | |
| 66 | |
| 67 | def 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 | |
| 104 | def test_donut(): |
no test coverage detected