1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| alpha = 0.0001 iters = 1000 Mini_theta = np.zeros((14,1)) batch_size = 16
Mini_theta, Mini_train = stochastic_descent(X_train, y_train, Mini_theta, alpha, iters, batch_size, X_valid, y_valid) Mini_valid = compute_loss(X_valid, y_valid, Mini_theta) print(Mini_train.min()) print(Mini_valid)
Momentum_theta = np.zeros((14,1))
Momentum_theta, Momentum_train = stochastic_descent(X_train, y_train, Momentum_theta, alpha, iters, batch_size, X_valid, y_valid, 'Momentum') Momentum_valid = compute_loss(X_valid, y_valid, Momentum_theta) print(Momentum_train.min()) print(Momentum_valid)
right = iters iteration = np.arange(0, right, step = 1) fig, ax = plt.subplots(figsize = (12,8)) ax.set_title('Momentum vs MiniBatch') ax.set_xlabel('iteration') ax.set_ylabel('loss') plt.plot(iteration, Momentum_train[0:right],'b', label='Momentum_train') plt.plot(iteration, Mini_train[0:right],'r', label='Mini_train') plt.legend() plt.show()
|