Answered step by step
Verified Expert Solution
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 updategrads!lr::LinearRegression, learningrate::Float grads ## Here you will implement updategrads, this function returns nothing. ## Search for setfield! and getfield functions. ## x learningrategrads will happen here!!! lrtheta learningrate grads # Update theta lrbias learningrate grads # Update bias return nothing return nothing end function fit!lr::linearregression, X::AbstractMatrix, y::AbstractVector; learningrate::Float maxiter::Integer lambda ::Float progress Progressmaxiter, "Computing...", for iter in :maxiter # Forward pass ypred lrX # Compute Huber loss loss huberlossypred, y # Compute gradients using Zygote grads Zygote.gradientlr huberlosslrX y lr # Update gradients with regularization term gradstuple gradslambda lrtheta grads # Update model parameters updategrads!lr learningrate, gradstuple # Update progress bar next!progress end return nothing end ## Let's give a try!!! lr linearregression X randn y randn fit!lr X y; learningrate maxiter ERROR: ArgumentError: broadcasting over dictionaries and NamedTuples is reserved Stacktrace:
function updategrads!lr::LinearRegression, learningrate::Float grads
## Here you will implement updategrads, this function returns nothing.
## Search for setfield! and getfield functions.
## x learningrategrads will happen here!!!
lrtheta learningrate grads # Update theta
lrbias learningrate grads # Update bias
return nothing
return nothing
end
function fit!lr::linearregression,
X::AbstractMatrix,
y::AbstractVector;
learningrate::Float
maxiter::Integer
lambda ::Float
progress Progressmaxiter, "Computing...",
for iter in :maxiter
# Forward pass
ypred lrX
# Compute Huber loss
loss huberlossypred, y
# Compute gradients using Zygote
grads Zygote.gradientlr huberlosslrX y lr
# Update gradients with regularization term
gradstuple gradslambda lrtheta grads
# Update model parameters
updategrads!lr learningrate, gradstuple
# Update progress bar
next!progress
end
return nothing
end
## Let's give a try!!!
lr linearregression
X randn
y randn
fit!lr X y; learningrate maxiter
ERROR: ArgumentError: broadcasting over dictionaries and NamedTuples is reserved
Stacktrace:
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