代做COMP9417、Python编程设计代写

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): 1 mark
? Question 1 d): 2 marks
? Question 1 e): 2 marks
? Question 1 f): 4 marks
? Question 1 g): 3 marks
? Question 1 h): 1 mark
? Question 1 i): 3 marks
? Question 1 j): 4 marks
? Question 2 a): 2 marks
? Question 2 b): 1 mark
? Question 2 c): 2 marks
What to Submit
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
.py file(s) containing all code you used for the project, which should be provided in a separate .zip
file. This code must match the code provided in the report.
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 ques-
tions 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 ques-
tions. This will result in an automatic grade of zero for the relevant question. You must do the
derivations manually.
When and Where to Submit
Due date: Week 4, Monday June 19th, 2023 by 5pm. Please note that the forum will not be actively
monitored on weekends.
Late submissions will incur a penalty of 5% per day from the maximum achievable grade. For ex-
ample, 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.
Page 2
Question 1. Gradient Based Optimization
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 g′(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 step-
size 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
x∈Rn
f(x),
where
f(x) =
1
2
‖Ax? b‖22 +
γ
2
‖x‖22,
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: ‖?f(x(k))‖2 < 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) = · · ·
...
Page 3
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.
(b) In the previous part, we used the termination condition ‖?f(x(k))‖2 < 0.001. What do you think
this condition means in terms of convergence of the algorithm to a minimizer of f ? How would
making the right hand side smaller (say 0.0001) instead, change the output of the algorithm? Ex-
plain.
What to submit: some commentary.
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:
Sales: Unit sales (in thousands) at each location
CompPrice: Price charged by competitor at each location
Income: Local income level (in thousands of dollars)
Advertising: advertising budget (in thousands of dollars)
Population: local population size (in thousands)
Price: price charged by store at each site
ShelveLoc: A categorical variable with Bad, Good and Medium describing the quality of the
shelf location of the car seat
Age: Average age of the local population
Education: Education level at each location
Urban A categorical variable with levels No and Yes to describe whether the store is in an
urban location or in a rural one
US: A categorical variable with levels No and Yes to describe whether the store is in the US or
not.
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 = arg min
β
1
n
‖y ?Xβ‖22 + φ‖β‖22,
where β ∈ Rp, X ∈ Rn×p, y ∈ Rn and φ > 0.
(c) We first need to preprocess the data. Remove all categorical features. Then use
sklearn.preprocessing.StandardScaler to standardize the remaining features. Print out
the mean and variance of each of the standardized features. Next, center the target variable (sub-
tract its mean). Finally, create a training set from the first half of the resulting dataset, and a test set
from the remaining half and call these objects X train, X test, Y train and Y test. Print out the first
and last rows of each of these.
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.
(d) It should be obvious that a closed form expression for β?Ridge exists. Write down the closed form
expression, and compute the exact numerical value on the training dataset with φ = 0.5.
Page 4
What to submit: Your working, and a print out of the value of the ridge solution based on (X train, Y train).
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
L(β) =
1
n
n∑
i=1
Li(β),
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
?L(β) = 1
n
n∑
i=1
?Li(β).
We can now define some popular variants of gradient descent .
(i) Gradient Descent (GD) (also referred to as batch gradient descent): here we use the full gradi-
ent, as in we take the average over all n terms, so our update rule is:
β(k+1) = β(k) ? αk
n
n∑
i=1
?Li(β(k)), k = 0, 1, 2, . . . .
(ii) Stochastic Gradient Descent (SGD): instead of considering all n terms, at the k-th step we
choose an index ik randomly from {1, . . . , n}, and update
β(k+1) = β(k) ? αk?Lik(β(k)), k = 0, 1, 2, . . . .
Here, we are approximating the full gradient?L(β) using?Lik(β).
(iii) Mini-Batch Gradient Descent: GD (using all terms) and SGD (using a single term) represents
the two possible extremes. In mini-batch GD we choose batches of size 1 < B < n randomly
at each step, call their indices {ik1 , ik2 , . . . , ikB}, and then we update
β(k+1) = β(k) ? αk
B
B∑
j=1
?Lij (β(k)), k = 0, 1, 2, . . . ,
so we are still approximating the full gradient but using more than a single element as is done
in SGD.
(e) The ridge regression loss is
L(β) =
1
n
‖y ?Xβ‖22 + φ‖β‖22.
Page 5
Show that we can write
L(β) =
1
n
n∑
i=1
Li(β),
and identify the functions L1(β), . . . , Ln(β). Further, compute the gradients?L1(β), . . . ,?Ln(β)
What to submit: your working.
(f) In this question, you will implement (batch) GD 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 1000
epochs (an epoch is one pass over the entire data, so a single GD step). Repeat this for the following
step sizes:
α ∈ {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:
(i) The train MSE: 1n‖ytrain ?Xtrainβ(K)‖22
(ii) The test MSE: 1n‖ytest ?Xtestβ(K)‖22
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.
(g) 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.
(h) Based on your GD and SGD results, which algorithm do you prefer? When is it a better idea to use
GD? When is it a better idea to use SGD? What to submit: some commentary
(i) Note that in GD, SGD and mini-batch GD, we always update the entire p-dimensional vector β at
each iteration. An alternative popular approach is to update each of the p parameters individually.
Page 6
To make this idea more clear, we write the ridge loss L(β) as L(β1, β2 . . . , βp). We initialize β(0),
and then solve for k = 1, 2, 3, . . . ,
Note that each of the minimizations is over a single (1-dimensional) coordinate of β, and also that
as as soon as we update β(k)j , we use the new value when solving the update for β
(k)
j+1 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:
L(β) =
1
n
‖y ?Xβ‖22 + φ‖β|22
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 = arg min
β1
L(β1, β2, . . . , βp).
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.
(j) Implement the algorithm outlined in the previous question on the training dataset. In your imple-
mentation, be sure to update the βj ’s in order and use an initial estimate of β(0) = 1p (th vector of
ones), and φ = 0.5. Terminate the algorithm after 10 cycles (one cycle here is p updates, one for each
βj), so you will have a total of 10p updates. Report the train and test MSE of your resulting model.
Here we would like to compare the three algorithms: new algorithm to batch GD and SGD from
your previous answers with optimally chosen step sizes. Create a plot of k vs. ?(k) as before, but
this time plot the progression of the three algorithms. Be sure to use the same colors as indicated
here in your plot, and add a legend that labels each series clearly. For your batch GD and SGD
include the step-size in the legend. Your x-axis only needs to range from k = 1, . . . 10p. Further,
report both train and test MSE for your new algorithm. Note: Some of you may be concerned that we
are comparing one step of GD to one step of SGD and the new aglorithm, we will ignore this technicality for
the time being. What to submit: a single plot, the train and test MSE requested.
Question 2
Given λ > 0 and v ∈ R, consider the following optimization problem:
min
β∈R
{
|β|+ 1

(β ? v)2
}
.
Page 7
(a) Denote the solution to the above problem by β?. Write down an expression for β?. Your answer
should be of the form:
What to submit: your expression for β?. You must include all working out to receive credit.
(b) Using the above result show that, for any λ > 0 and v = (v1, . . . , vp) ∈ Rp, the solution of the
minimization problem
min
β∈Rp
{
‖β‖1 + 1

‖β ? v‖22
}
is
β? = Tλ(v) := (Tλ(v1), Tλ(v2), . . . , Tλ(vp)).
What to submit: your working out.
(c) Let v = (1, 2, 4,?7, 2, 4,?1, 8, 4,?10,?5). What are the results for Tλ(v) with λ = 1, 3, 6, 9? What
do you observe? What to submit: your results and some commentary

热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图