Exercises
Exercises
Conceptual
Exercise 1 Conceptual
Consider the loss \(\mathcal L(\theta) = \frac1n\sum_i (y_i - \theta_0 - \theta_1 x_i)^2\).
- Compute \(\partial \mathcal L / \partial \theta_0\) and \(\partial \mathcal L / \partial \theta_1\).
- Write down one step of gradient descent for both parameters.
- Set the two derivatives to zero and solve. Compare to the closed form for simple linear regression.
Exercise 2 Conceptual
Take the one-dimensional loss \(\mathcal L(\theta) = \theta^2\).
- Write down the gradient descent update with learning rate \(\eta\).
- Show that \(\theta_{t} = (1 - 2\eta)^t \theta_0\).
- For which \(\eta\) does this converge. For which \(\eta\) does it oscillate. For which \(\eta\) does it diverge.
- What does this tell us about the largest usable learning rate for a general quadratic loss.
Exercise 3 Conceptual
Explain in two sentences each.
- Why do we reset the gradient to zero in every step.
- Why is the learning curve of stochastic gradient descent noisy.
- Why does early stopping act like regularization.
Applied
Exercise 4 Applied
Generate 100 points from \(y = 2x_1 - 3x_2 + 1 + \epsilon\) with normal noise.
- Write the loss function on the training set for a given parameter vector. Use matrix multiplication.
- Run gradient descent. Plot the learning curve to see whether it has converged. If you see large fluctuations at the end, lower the learning rate. If the curve is not flat at the end, increase the number of steps. Use a log scale on the vertical axis.
- Estimate the coefficients with
LinearRegressionand compare. - Now run it with a learning rate ten times larger and ten times smaller, and describe what happens.
Exercise 5 Applied
Compare batch sizes on the same data.
- Run gradient descent with batch sizes 1, 10, 100 and \(n\), with the same learning rate and the same number of epochs.
- Plot the four learning curves on one figure.
- Plot the same curves against wall clock time instead of epochs. Does the ranking change.
Exercise 6 Applied · optional
The XOR problem. Generate points in two dimensions with class 1 if \(x_1 x_2 > 0\) and class 0 otherwise.
- Fit a logistic regression and report the accuracy. Plot the decision boundary.
- Add the product \(x_1 x_2\) as a third predictor and fit again.
- Explain what the third predictor changed, in terms of the geometry of the problem.