()
| 197 | |
| 198 | |
| 199 | def benchmark_full(): |
| 200 | Xtrain, Xtest, Ytrain, Ytest = get_normalized_data() |
| 201 | |
| 202 | print("Performing logistic regression...") |
| 203 | # lr = LogisticRegression(solver='lbfgs') |
| 204 | |
| 205 | |
| 206 | # convert Ytrain and Ytest to (N x K) matrices of indicator variables |
| 207 | N, D = Xtrain.shape |
| 208 | Ytrain_ind = y2indicator(Ytrain) |
| 209 | Ytest_ind = y2indicator(Ytest) |
| 210 | |
| 211 | W = np.random.randn(D, 10) / np.sqrt(D) |
| 212 | b = np.zeros(10) |
| 213 | LL = [] |
| 214 | LLtest = [] |
| 215 | CRtest = [] |
| 216 | |
| 217 | # reg = 1 |
| 218 | # learning rate 0.0001 is too high, 0.00005 is also too high |
| 219 | # 0.00003 / 2000 iterations => 0.363 error, -7630 cost |
| 220 | # 0.00004 / 1000 iterations => 0.295 error, -7902 cost |
| 221 | # 0.00004 / 2000 iterations => 0.321 error, -7528 cost |
| 222 | |
| 223 | # reg = 0.1, still around 0.31 error |
| 224 | # reg = 0.01, still around 0.31 error |
| 225 | lr = 0.00004 |
| 226 | reg = 0.01 |
| 227 | for i in range(500): |
| 228 | p_y = forward(Xtrain, W, b) |
| 229 | # print "p_y:", p_y |
| 230 | ll = cost(p_y, Ytrain_ind) |
| 231 | LL.append(ll) |
| 232 | |
| 233 | p_y_test = forward(Xtest, W, b) |
| 234 | lltest = cost(p_y_test, Ytest_ind) |
| 235 | LLtest.append(lltest) |
| 236 | |
| 237 | err = error_rate(p_y_test, Ytest) |
| 238 | CRtest.append(err) |
| 239 | |
| 240 | W += lr*(gradW(Ytrain_ind, p_y, Xtrain) - reg*W) |
| 241 | b += lr*(gradb(Ytrain_ind, p_y) - reg*b) |
| 242 | if i % 10 == 0: |
| 243 | print("Cost at iteration %d: %.6f" % (i, ll)) |
| 244 | print("Error rate:", err) |
| 245 | |
| 246 | p_y = forward(Xtest, W, b) |
| 247 | print("Final error rate:", error_rate(p_y, Ytest)) |
| 248 | iters = range(len(LL)) |
| 249 | plt.plot(iters, LL, iters, LLtest) |
| 250 | plt.show() |
| 251 | plt.plot(CRtest) |
| 252 | plt.show() |
| 253 | |
| 254 | |
| 255 | def benchmark_pca(): |
no test coverage detected