Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This question is about programming Gradient Descent in Python, given a loss function. I'm trying to code a gradient descent algorithm using a randomly generated

This question is about programming Gradient Descent in Python, given a loss function.

I'm trying to code a gradient descent algorithm using a randomly generated dataset and using the loss function specified below:

image text in transcribed

I've calculated the gradient of the loss function with respect to w and tried using it in the python code. I'm getting an error "

Unable to coerce to Series, length must be 10: given 250"

Can someone help me with making the code below work, for the randomly generated dataset of 250 datapoints i.e. generate_data(251)?

Here's the code I have so far:

w = np.random.normal(0,1,10) norm = np.linalg.norm(w) norm_w = worm

#Generating random data for x, y def generate_data(m): x_i = [] y_i = [] a = [0, 1] for i in range(1, m): x = np.random.normal(0,1,10) x_i.append(x) b = np.dot(x, norm_w) sigma = 1/(1 + np.exp(-b)) p_y1 = sigma p_y0 = 1 - (sigma) c = random.choices(a, weights = [p_y0, p_y1]) y_i.append(c) i = i + 1 df = pd.DataFrame(x_i) df['y_i'] = y_i return df

df = generate_data(251)

#Loss_function = (1/2)(((1/(1+exp(-Xw)))-y)^2) #Gradient of the loss function with respect to w grad = (X*np.exp(-(np.dot(X, norm_w)))*((y-1)*np.exp(-(np.dot(X, norm_w)))+y))/((np.exp(-(np.dot(X, norm_w)))+1)**3)

#Update Rule

w_old = norm_w eta = 0.01 w_new = w_old - 1/250*(eta)*(grad)

Please add comments in the completed code

Define square loss as L;w") = }(C14" . x) )? Algorithm 2 is gradient descent with respect to square loss (code this up yourself -- run for 1000 iterations, use step size eta = 0.01). Define square loss as L;w") = }(C14" . x) )? Algorithm 2 is gradient descent with respect to square loss (code this up yourself -- run for 1000 iterations, use step size eta = 0.01)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

Students also viewed these Databases questions

Question

What is liability of newness?

Answered: 1 week ago

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago