()
| 22 | |
| 23 | |
| 24 | def main(): |
| 25 | Xtrain, Xtest, Ytrain, Ytest = get_normalized_data() |
| 26 | print("Performing logistic regression...") |
| 27 | |
| 28 | N, D = Xtrain.shape |
| 29 | Ytrain_ind = y2indicator(Ytrain) |
| 30 | Ytest_ind = y2indicator(Ytest) |
| 31 | |
| 32 | # 1. full |
| 33 | W = np.random.randn(D, 10) / np.sqrt(D) |
| 34 | W0 = W.copy() # save for later |
| 35 | b = np.zeros(10) |
| 36 | test_losses_full = [] |
| 37 | lr = 0.9 |
| 38 | reg = 0. |
| 39 | t0 = datetime.now() |
| 40 | last_dt = 0 |
| 41 | intervals = [] |
| 42 | for i in range(50): |
| 43 | p_y = forward(Xtrain, W, b) |
| 44 | |
| 45 | gW = gradW(Ytrain_ind, p_y, Xtrain) / N |
| 46 | gb = gradb(Ytrain_ind, p_y) / N |
| 47 | |
| 48 | W += lr*(gW - reg*W) |
| 49 | b += lr*(gb - reg*b) |
| 50 | |
| 51 | p_y_test = forward(Xtest, W, b) |
| 52 | test_loss = cost(p_y_test, Ytest_ind) |
| 53 | dt = (datetime.now() - t0).total_seconds() |
| 54 | |
| 55 | # save these |
| 56 | dt2 = dt - last_dt |
| 57 | last_dt = dt |
| 58 | intervals.append(dt2) |
| 59 | |
| 60 | test_losses_full.append([dt, test_loss]) |
| 61 | if (i + 1) % 10 == 0: |
| 62 | print("Cost at iteration %d: %.6f" % (i + 1, test_loss)) |
| 63 | p_y = forward(Xtest, W, b) |
| 64 | print("Final error rate:", error_rate(p_y, Ytest)) |
| 65 | print("Elapsted time for full GD:", datetime.now() - t0) |
| 66 | |
| 67 | # save the max time so we don't surpass it in subsequent iterations |
| 68 | max_dt = dt |
| 69 | avg_interval_dt = np.mean(intervals) |
| 70 | |
| 71 | |
| 72 | # 2. stochastic |
| 73 | W = W0.copy() |
| 74 | b = np.zeros(10) |
| 75 | test_losses_sgd = [] |
| 76 | lr = 0.001 |
| 77 | reg = 0. |
| 78 | |
| 79 | t0 = datetime.now() |
| 80 | last_dt_calculated_loss = 0 |
| 81 | done = False |
no test coverage detected