代做EE4802/IE4213 Learning from Data, Spring 2025 Homework 1: Regression调试Python程序

EE4802/IE4213 Learning from Data, Spring 2025

Homework 1: Regression


Problem 1.1 (30 points)  LASSO (Least Absolute Shrinkage and Selection Operator) promotes sparsity.

Assume the training data points are denoted as the rows of a n × d matrix X and their corresponding responses as an n-dimensional vector y. For the sake of simplicity, assume columns of data have been standardized to have mean 0 and variance 1, and are uncorrelated (i.e. X TX = nI).

For LASSO regression, the optimal weight vector is given by:

where λ > 0.

•  First, show that standardized training data nicely decouples the loss function, making wi(*)  deter- mined by the i-th feature and the output regardless of other features. That is to say, we need to explicitly reformulate Jλ (win the following form for appropriate functions and f :

 

where Xi  is the i-th column of X .

  Assume that wi(*)  > 0. What is the value of wi(*)  in this case?

  Assume that wi(*)  < 0. What is the value of wi(*)  in this case?

  From the previous two parts, what is the condition for wi(*)  to be zero?

•  Compute the  closed-form.  solution  w*   of LASSO regression given  a fixed  λ  >  0  if  X TX  is invertible.

Problem 1.2 (30 points) Ridge Regression.  The ridge regression optimization problem with parameter λ > 0 is given by

1.  Show that R(ˆ)ridge (w) is convex with regards tow. You can use the fact that a twice differentiable function is convex if and only if its Hessian H ∈ Rd×d  satisfies w THw ≥ 0 for all w ∈ Rd  (is positive semi-definite).

2.  Derive the closed form solution wr(*)idge  of (RidgeR).

3.  Show that (RidgeR) admits the unique solution wr(*)idge  for any matrix X, even X TX ∈ Rd×d  is singular (i.e., rank(XTX) < d).

4. What is the role of the term λ∥w∥2(2) in R(ˆ)ridge (w)? What happens towr(*)idge  as λ → 0 and λ → ∞?

Problem 1.3 (40 points)  Coding Problem

Before you start:  please install anaconda python (python 3.x version is recommended) by following the instructions at https://www.anaconda.com/download/. You may also choose to use other IDES (like VS Code) as long as you are familiar with them.

If you decide to install Anaconda Python, ensure that the following packages are installed:  numpy, py- torch (torch), scipy, matplotlib, and jupyter notebook. You can install these packages within Anaconda by running commands like conda  install  numpy.  Note that some of these packages may already be included, depending on the version of Anaconda you install.


The included archive contains skeleton Python code, hw1.ipynb, which you must complete. To begin, navigate to the directory where this file is located in your terminal, and run the command jupyter notebook. This will automatically open a local webpage in your web browser. Click on the hw1.ipynb file to open the notebook.

Sections you need to complete are marked with TODO comments (search for all occurrences of TODO). Complete the scripts below each TODO marker.  Additionally, there are some questions that you need to answer. You can either type your answers directly into the .ipynb file by adding a Markdown block or include them in your write-up. If you choose the latter, please specify this clearly in your .ipynb file.

After completing all tasks, run all the blocks and then export your notebook as an HTML file showing the outputs. Submit both the completed .ipynb file and the corresponding HTML file.

1.  First Task:  Variants of Linear Regression.

In the first task, you will compare the optimal solutions of linear regression, ridge regression, and lasso regression. A dataset of 1, 000 samples following a linear model will be provided:

Y = Xw + ϵ,

where XT  = (x1  |  · · ·  | xn ) ∈ R2 ×n  and Y T  = (y1  |  · · ·  | yn ) ∈ R1 ×n  with n = 1000 and w ∈ R2 and the error term ϵ ∈ Rn  arei.i.dzero-mean random variables.

In this part, you are asked to code the following linear regression and its two variants,

(a)  Read 1 .Functions in the file, please implement linear regression, ridge regression and lasso regression in the function lin_reg,  rid_reg,  lasso_reg separately.  Generate the datasets and perform numeric experiment in 2 .Datasets and 3.Experiment-(a).

(b) In 4.Experiment-(b), we gradually increase the value of parameter λ from 1.1 −3  to 1.190 , and then perform ridge regression and lasso regression.  Observe the plots shown in 5 .Plot. It shows the optimal solutions of ridge regression/lasso regression under different lambdas. [Questions:  Explain the  behavior of the optimal solutions as  λ increases.  Describe how λ affects the optimal solutions for the two proposed variants of linear regression.  Why do their behaviors differ, and in what ways are their tendencies distinct from one another?]

2.  Second Task: Vanilla Gradient Descent (VGD)&Stochastic Gradient Descent (SGD) for Linear Regression.

In the second task, you are provided with a dataset of 1, 000, 000 samples.  Its input and output also follow a linear model

Y = Xw + ϵ,

where XT  = (x1  |  · · ·  |  xn ) ∈ R10 ×n  and Y T  = (y1   |  · · ·  |  yn ) ∈ R1 ×n  with n = 1, 000, 000 and the error term ϵ ∈ Rn  arei.i.dzero-mean random variables.

(a)  Read 1 .Functions in the file.  The function VGD_auto and VGD_manual train the model us- ing vanilla gradient descent (VGD), as discussed in the lecture.  In the function  VGD_auto, a built-in gradient optimization method in PyTorch to compute the gradients is implemented.

Your task is to calculate the exact expression of the gradient w.grad = ∂L/∂w manually, and implement VGD in VGD_manual.  [Hint:  Remember to include the constant factor of 2 when computing the derivative of the OLS loss function.]  Next, generate the datasets and perform numeric experiment in 2 .Datasets and 3 .Experiment.  Check if the losses of VGD_manual and VGD_auto are the same.

(b) You might realize that the result is not satisfying.  In this part, your task is to find proper parameters(e.g.,  number of iterations,  learning_rate) in  2 .Datasets such that the result meets the following requirement:

•  The running time requirements are within 0.1 second and 10 seconds separately for SGD and VGD(automatically or manually computed).

•  The losses are below 2 for all algorithms.

Observe the plots shown in 4.Plot.  [Questions:  Compare the differences in numerical per- formances (losses and running time) between the VGD and SGD. Why do their behaviors differ, and in what ways are they distinct from one another? ]


热门主题

课程名

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
站长地图