$30
COMP9417 - Machine Learning
Homework 1: Regularized Regression & Numerical
Optimization
Introduction In this homework we will explore some algorithms for gradient based optimization. These algorithms have been crucial to the development of machine learning in the last few decades. The most famous example is the backpropagation algorithm used in deep learning, which is in fact just an application of a simple algorithm known as (stochastic) gradient descent. We will first implement gradient descent from scratch on a deterministic problem (no data), and then extend our implementation to solve a real world regression problem.
Points Allocation There are a total of 28 marks.
Question 1 a): 2 marks
Question 1 b): 1 mark
Question 1 c): 4 marks
Question 1 d): 1 mark
Question 1 e): 1 mark
Question 1 f): 2 marks
Question 1 g): 3 marks
Question 1 h): 3 marks
Question 1 i): 1 mark
Question 1 j): 4 marks
Question 1 k): 5 marks
A single PDF file which contains solutions to each question. For each question, provide your solution in the form of text and requested plots. For some questions you will be requested to provide screen shots of code used to generate your answer — only include these when they are explicitly asked for.
1
You may be deducted points for not following these instructions.
You may be deducted points for poorly presented/formatted work. Please be neat and make your solutions clear. Start each question on a new page if necessary.
You cannot submit a Jupyter notebook; this will receive a mark of zero. This does not stop you from developing your code in a notebook and then copying it into a .py file though, or using a tool such as nbconvert or similar.
We will set up a Moodle forum for questions about this homework. Please read the existing questions before posting new questions. Please do some basic research online before posting questions. Please only post clarification questions. Any questions deemed to be fishing for answers will be ignored and/or deleted.
Please check Moodle announcements for updates to this spec. It is your responsibility to check for announcements about the spec.
Please complete your homework on your own, do not discuss your solution with other people in the course. General discussion of the problems is fine, but you must write out your own solution and acknowledge if you discussed any of the problems in your submission (including their name(s) and zID).
As usual, we monitor all online forums such as Chegg, StackExchange, etc. Posting homework questions on these site is equivalent to plagiarism and will result in a case of academic misconduct.
You may not use SymPy or any other symbolic programming toolkits to answer the derivation questions. This will result in an automatic grade of zero for the relevant question. You must do the derivations manually.
Late submissions will incur a penalty of 5% per day from the maximum achievable grade. For example, if you achieve a grade of 80/100 but you submitted 3 days late, then your final grade will be 80 − 3 × 5 = 65. Submissions that are more than 5 days late will receive a mark of zero.
Submission must be done through Moodle, no exceptions.
The general framework for a gradient method for finding a minimizer of a function f : Rn → R is defined by
x(k+1) = x(k) − αk∇f(xk), k = 0,1,2,..., (1)
where αk > 0 is known as the step size, or learning rate. Consider the following simple example of√
minimizing the function g(x) = 2 x3 + 1. We first note that g0(x) = 3x2(x3 + 1)−1/2. We then need to choose a starting value of x, say x(0) = 1. Let’s also take the step size to be constant, αk = α = 0.1. Then we have the following iterations:
x(1) = x(0) − 0.1 × 3(x(0))2((x(0))3 + 1)−1/2 = 0.7878679656440357 x(2) = x(1) − 0.1 × 3(x(1))2((x(1))3 + 1)−1/2 = 0.6352617090300827
x(3) = 0.5272505146487477
...
and this continues until we terminate the algorithm (as a quick exercise for your own benefit, code this up and compare it to the true minimum of the function which is x∗ = −1). This idea works for functions that have vector valued inputs, which is often the case in machine learning. For example, when we minimize a loss function we do so with respect to a weight vector, β. When we take the stepsize to be constant at each iteration, this algorithm is known as gradient descent. For the entirety of this question, do not use any existing implementations of gradient methods, doing so will result in an automatic mark of zero for the entire question. (a) Consider the following optimisation problem:
min f(x), x∈Rn
where
,
and where A ∈ Rm×n, b ∈ Rm are defined as
,
and γ is a positive constant. Run gradient descent on f using a step size of α = 0.1 and γ = 0.2 and starting point of x(0) = (1,1,1,1). You will need to terminate the algorithm when the following condition is met: k∇f(x(k))k2 < 0.001. In your answer, clearly write down the version of the gradient steps (1) for this problem. Also, print out the first 5 and last 5 values of x(k), clearly indicating the value of k, in the form:
k = 0, |
x(k) = [1,1,1,1] |
k = 1, |
x(k) = ··· |
k = 2, |
x(k) = ··· |
...
What to submit: an equation outlining the explicit gradient update, a print out of the first 5 (k = 5 inclusive) and last 5 rows of your iterations. Use the round function to round your numbers to 4 decimal places. Include a screen shot of any code used for this section and a copy of your python code in solutions.py.
What to submit: some commentary.
import torch import torch.nn as nn from torch import optim A = ### b = ### tol = ### gamma = 0.2 alpha = 0.1 class MyModel(nn.Module): def __init__(self): super().__init__() self.x = #### def forward(self, ###): return ### model = MyModel() optimizer = ### terminationCond = False k = 0 while not terminationCond: ### compute loss, find gradients, update, check termination cond. etc |
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
What to submit: a print out of the first 5 (k = 5 inclusive) and last 5 rows of your iterations. Use the round function to round your numbers to 4 decimal places. Include a screen shot of any code used for this section and a copy of your python code in solutions.py.
In the next few parts, we will use gradient methods explored above to solve a real machine learning problem. Consider the CarSeats data provided in CarSeats.csv. It contains 400 observations with each observation describing child car seats for sale at one of 400 stores. The features in the data set are outlined below:
Population: local population size (in thousands)
The target variable is Sales. The goal is to learn to predict the amount of Sales as a function of a subset of the above features. We will do so by running Ridge Regression (Ridge) which is defined as follows
βˆRidge = argmin
,
where β ∈ Rp,X ∈ Rn×p,y ∈ Rn and φ > 0.
What to submit: a print out of the means and variances of features, a print out of the first and last rows of the 4 requested objects, and some commentary. Include a screen shot of any code used for this section and a copy of your python code in solutions.py.
We will now solve the ridge problem but using numerical techniques. As noted in the lectures, there are a few variants of gradient descent that we will briefly outline here. Recall that in gradient descent our update rule is
β(k+1) = β(k) − αk∇L(β(k)), k = 0,1,2,...,
where L(β) is the loss function that we are trying to minimize. In machine learning, it is often the case that the loss function takes the form
,
i.e. the loss is an average of n functions that we have lablled Li. It then follows that the gradient is also an average of the form
.
We can now define some popular variants of gradient descent .
β(k+1) = β(k) − αk∇Lik(β(k)), k = 0,1,2,....
Here, we are approximating the full gradient ∇L(β) using ∇Lik(β).
so we are still approximating the full gradient but using more than a single element as is done in SGD.
The ridge regression loss is
.
Show that we can write
,
and identify the functions L1(β),...,Ln(β). Further, compute the gradients ∇L1(β),...,∇Ln(β) What to submit: your working.
α ∈ {0.000001,0.000005,0.00001,0.00005,0.0001,0.0005,0.001,0.005,0.01}
To monitor the performance of the algorithm, we will plot the value
∆(k) = L(β(k)) − L(βˆ),
where βˆ is the true (closed form) ridge solution derived earlier. Present your results in a 3 × 3 grid plot, with each subplot showing the progression of ∆(k) when running GD with a specific step-size. State which step-size you think is best and let β(K) denote the estimator achieved when running GD with that choice of step size. Report the following:
What to submit: a single plot, the train and test MSE requested. Include a screen shot of any code used for this section and a copy of your python code in solutions.py.
We will now implement SGD from scratch to solve the ridge regression problem. Use an initial estimate β(0) = 1p (the vector of ones) and φ = 0.5 and run the algorithm for 5 epochs (this means a total of 5n updates of β, where n is the size of the training set). Repeat this for the following step sizes:
α ∈ {0.000001,0.000005,0.00001,0.00005,0.0001,0.0005,0.001,0.006,0.02}
Present an analogous 3 × 3 grid plot as in the previous question. Instead of choosing an index randomly at each step of SGD, we will cycle through the observations in the order they are stored in X train to ensure consistent results. Report the best step-size choice and the corresponding train and test MSEs. In some cases you might observe that the value of ∆(k) jumps up and down, and this is not something you would have seen using batch GD. Why do you think this might be happening?
What to submit: a single plot, the train and test MSE requested and some commentary. Include a screen shot of any code used for this section and a copy of your python code in solutions.py.
= argmin
= argmin
β2
...
= argmin.
βp
Note that each of the minimizations is over a single (1-dimensional) coordinate of β, and also that as as soon as we update, we use the new value when solving the update for
and so on. The idea is then to cycle through these coordinate level updates until convergence. In the next two parts we will implement this algorithm from scratch for the Ridge regression problem:
Note that we can write the n × p matrix X = [X1,...,Xp], where Xj is the j-th column of X. Find the solution of the optimization
βˆ1 = argminL(β1,β2,...,βp).
β1
Based on this, derive similar expressions for βˆj for j = 2,3,...,p.
Hint: Note the expansion: Xβ = Xjβj + X−jβ−j, where X−j denotes the matrix X but with the j-th column removed, and similarly β−j is the vector β with the j-th coordinate removed. What to submit: your working out.
Explain. What to submit: your commentary