Answered step by step
Verified Expert Solution
Question
1 Approved Answer
using LinearAlgebra, Zygote, ForwardDiff, Printf using CSV , DataFrames using StatsBase: mean using Parameters using Distributions using Random using BSON using ProgressMeter using ProgressBars ###
using LinearAlgebra, Zygote, ForwardDiff, Printf
using CSV DataFrames
using StatsBase: mean
using Parameters
using Distributions
using Random
using BSON
using ProgressMeter
using ProgressBars
### Below you will implement your linear regression object from scratch.
###
### you will also imply some penalty methods as well.
#### ###
#### Before getting started you should write your studentnumber in integer format
const studentnumber::Int ## replace by your studentnumber
### ###
### This hw is a bit harder than the usual ones, so you may want to
### take a look at Julia official website
### Before you get started, you gotta look at do end syntax of Julia
### Instead of mean square loss we shall use Huber loss which
### sometimes performs better when your dataset contains some out of distribution points
### We split it into two parts the first one is for scalars the second one is for vectors
### remember that you will multiple dispatch!!!
function huberlossypred::T ytrue::T; delta ::Float where T : Real
loss absypred ytrue
if loss delta
return loss
else
return delta loss delta
end
end
function huberlossypred::AbstractArrayT ytrue::AbstractArrayT where T : Real
if lengthypred lengthytrue
throwArgumentErrorInput arrays must have the same length"
end
n lengthypred
totalloss
for i in :n
totalloss huberlossypredi ytruei
end
return totalloss n
end
function unittesthuberloss::Bool
Random.seed!
@assert huberloss
@assert huberloss; delta
@assert isapproxhuberlossrandnrandn atol e
@assert isapproxhuberlossrandnrandn atol e
@info "You can not stop now comrade!!! jump to the next exercise!!!"
return
end
## See you have implemented huberloss well??
unittesthuberloss
### create a roof for the logistic regression LogisticClassifier
abstract type LinearRegression end
mutable struct linearregression : LinearRegression
## This part is given to you!!!
## Realize that we have fields: theta and bias.
theta ::AbstractVector
bias::Real
linearregressionn::Int newrandnn zero
end
# Define the predict function for linear regression
function predictmodel::linearregression, X::AbstractVector
return dotmodeltheta X model.bias
end
### write the forwardpass function
function lr::linearregressionX::MatrixT where T:Real
## This dude is the forward pass function!!!
m n sizeX
predictions zerosm
for i in :m
predictionsi predictlr Xi :
end
return predictions
end
function unittestforwardpass::Bool
try
linearregressionrandn
catch ERROR
errorSEGFAULT!!!!"
end
@info "Real test started!!!!"
for i in ProgressBar:
sleep
lr linearregression
x randn
@assert lrx xlrtheta lrbias
end
@info "Oki doki!!!"
return
end
### Let's give a try!!!!!!
unittestforwardpass
## we shall now implement fit! method!!!
## before we get ready run the next lines to see in this setting grad function returns a dictionary:
#
lr linearregression
val, grad Zygote.withgradientlr do lr
normlrtheta lrbias
end
#
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
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 gradstheta lambda lrtheta grads.bias
# Update model parameters
lrtheta lrbias 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
what is wrong with this code?
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