Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1) (10 pts) Given a dataset with three one-dimensional instances {([1],4),([2],2),([4],1)} (e.g., the feature vector and ground-truth label of the first instance is [1] and
1) (10 pts) Given a dataset with three one-dimensional instances {([1],4),([2],2),([4],1)} (e.g., the feature vector and ground-truth label of the first instance is [1] and 4, respectively). Suppose we use a linear predictor f=wx+w0 to fit the data (w0 is called the offset), with squared loss as the loss function. Complete the codes in "hw1_gradientDescent.py" to solve w and w0 using gradient descent. Report in "YourID_HW1.pdf": (1) the codes you write to fulfill the functionality; (2) the outputs at iteration 0,1990 and 2000. hw1_gradientDescent.py >dF 1 import numpy as np 2 import math 3 "I" 4 TODO: Complete the three lines of codes below. And run the program to get the result. NOTE: ***Don't change other parts of this program. *** "n" \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# \# Data and Modeling: what we want to compute points =[(np.array([1]),4),(np.array([2]),2),(nparray([4]),1)] d=1 \# Function def F(w,w_) : \# 1. Implement the loss function below. return \# Gradient of the function above w.r.t. w and w_. def dF(w2,w) : \# 2. Implement the computation of gradient below for the loss function, with respect to w and w_ . return \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# \# Algorithms: how we compute it def gradientDescent (F,dF,d) : w,w=npzeros(d),. eta =0.02 for t in range(2001): value =F(w,w) gradient_w, gradient_w_ =dF(w,w) w=w - eta gradient_w \# 3. Implement the update of w using gradient descent. w= if t%10==0: print( 'iteration \{\}:w={},w={},F(w,w)={} '.format (t,w,w, value ) ) gradientDescent (F,dF,d)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started