()
| 102 | |
| 103 | |
| 104 | def main(): |
| 105 | # create the data |
| 106 | Nclass = 500 |
| 107 | D = 2 # dimensionality of input |
| 108 | M = 3 # hidden layer size |
| 109 | K = 3 # number of classes |
| 110 | |
| 111 | X1 = np.random.randn(Nclass, D) + np.array([0, -2]) |
| 112 | X2 = np.random.randn(Nclass, D) + np.array([2, 2]) |
| 113 | X3 = np.random.randn(Nclass, D) + np.array([-2, 2]) |
| 114 | X = np.vstack([X1, X2, X3]) |
| 115 | |
| 116 | Y = np.array([0]*Nclass + [1]*Nclass + [2]*Nclass) |
| 117 | N = len(Y) |
| 118 | # turn Y into an indicator matrix for training |
| 119 | T = np.zeros((N, K)) |
| 120 | for i in range(N): |
| 121 | T[i, Y[i]] = 1 |
| 122 | |
| 123 | # let's see what it looks like |
| 124 | plt.scatter(X[:,0], X[:,1], c=Y, s=100, alpha=0.5) |
| 125 | plt.show() |
| 126 | |
| 127 | # randomly initialize weights |
| 128 | W1 = np.random.randn(D, M) |
| 129 | b1 = np.random.randn(M) |
| 130 | W2 = np.random.randn(M, K) |
| 131 | b2 = np.random.randn(K) |
| 132 | |
| 133 | learning_rate = 1e-3 |
| 134 | costs = [] |
| 135 | for epoch in range(1000): |
| 136 | output, hidden = forward(X, W1, b1, W2, b2) |
| 137 | if epoch % 100 == 0: |
| 138 | c = cost(T, output) |
| 139 | P = np.argmax(output, axis=1) |
| 140 | r = classification_rate(Y, P) |
| 141 | print("cost:", c, "classification_rate:", r) |
| 142 | costs.append(c) |
| 143 | |
| 144 | # this is gradient ASCENT, not DESCENT |
| 145 | # be comfortable with both! |
| 146 | # oldW2 = W2.copy() |
| 147 | |
| 148 | gW2 = derivative_w2(hidden, T, output) |
| 149 | gb2 = derivative_b2(T, output) |
| 150 | gW1 = derivative_w1(X, hidden, T, output, W2) |
| 151 | gb1 = derivative_b1(T, output, W2, hidden) |
| 152 | |
| 153 | W2 += learning_rate * gW2 |
| 154 | b2 += learning_rate * gb2 |
| 155 | W1 += learning_rate * gW1 |
| 156 | b1 += learning_rate * gb1 |
| 157 | |
| 158 | plt.plot(costs) |
| 159 | plt.show() |
| 160 | |
| 161 |
no test coverage detected