Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

function update _ grads! ( lr::LinearRegression, learning _ rate::Float 6 4 , grads ) ## Here you will implement update _ grads, this function returns

function update_grads!(lr::LinearRegression, learning_rate::Float64, grads)
## Here you will implement update_grads, this function returns nothing.
## Search for setfield! and getfield functions.
## x -= learning_rate*grads will happen here!!!
lr.\theta .-= learning_rate * grads[1] # Update \theta
lr.bias -= learning_rate * grads[2] # Update bias
return nothing
return nothing
end
function fit!(lr::linear_regression,
X::AbstractMatrix,
y::AbstractVector;
learning_rate::Float64=0.00001,
max_iter::Integer =5,
\lambda ::Float64=0.01)
progress = Progress(max_iter, 1, "Computing...", 50)
for iter in 1:max_iter
# Forward pass
y_pred = lr(X)
# Compute Huber loss
loss = huber_loss(y_pred, y)
# Compute gradients using Zygote
grads = Zygote.gradient(lr -> huber_loss(lr(X), y), lr)
# Update gradients with regularization term
grads_tuple =(grads[1].+2*\lambda * lr.\theta , grads[2])
# Update model parameters
update_grads!(lr, learning_rate, grads_tuple)
# Update progress bar
next!(progress)
end
return nothing
end
## Let's give a try!!!
lr = linear_regression(20)
X = randn(100,20)
y = randn(100)
fit!(lr, X, y; learning_rate =0.00001, max_iter =10000)
ERROR: ArgumentError: broadcasting over dictionaries and `NamedTuple`s is reserved
Stacktrace:

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

Students also viewed these Databases questions