Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using LinearAlgebra, Zygote, ForwardDiff, Printf using CSV , DataFrames using StatsBase: mean using Parameters using Distributions using Random using BSON ### Below you will implement

using LinearAlgebra, Zygote, ForwardDiff, Printf
using CSV, DataFrames
using StatsBase: mean
using Parameters
using Distributions
using Random
using BSON
### 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 student_number in integer format
const student_number: :Int64=0,##-- replace by your student_number
### ---- ###
### 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 huber_loss(y_pred::T, y_true::T; ::F loat64=0.1) where T : Real
return nothing
end
function huber_loss(y_pred: :AbstractArray {T},y-true::Abs tractArray{T}) where T : Real
return nothing
end
function unit_test_huber_loss() : :Bool
Random.seed! ()
@assert huber_loss (1.0,1.0)==0.0
@assert huber_loss (1.0,2.0;=0.9)==0.49500000000000005
Qassert isapprox(huber_loss(randn(100,100), randn(100,100)),0.10791842, atol =1e-2)
@assert isapprox(huber_loss(randn(100), randn(100)),0.107945, atol =1e-2)
@info "You can not stop now comrade!!! jump to the next exercise!!!"
return 1function unit_test_huber_loss()::Bool
Random.seed! ()
@assert huber_loss (1.0,1.0)==0.0
@assert huber_loss (1.0,2.0;=0.9)=0.49500000000000005
Qassert isapprox(huber_loss(randn (100,100),randn(100,100), atol =1e-2)
@assert isapprox(huber_loss(randn(100), randn(100)),0.107945, atol =1e-2)
@info "You can not stop now comrade!!! jump to the next exercise!!!""
return 1
end
## See you have implemented huber_loss() well??
unit_test_huber_loss()
### create a roof for the Logistic regression Logisticclassifier
abstract type LinearRegression end
mutable struct linear_regression : LinearRegression
## This part is given to you!!!
# Realize that we have fields: 8 and bias.
: : AbstractVector
bias: : Real
linear_regression , zero(1))
end
## write the forwardpass function
function (1r::Linear_regression)(X: Matrix {T} where T:Real
# This dude is the forward pass function!!!
return nothing
end
function unit_test_forward_pass() : :Bool
try
linear_regression (20)(randn(10,20))
catch ERROR
error("SEG-FAULT!!!!!")
end
@info "Real test started!!!!!"
for i in ProgressBar (1:10000)
sleed(0.0001)function unit_test_forward_pass ()::Bool
try
linear_regression (20)(randn(10,20))
catch ERROR
error("SEG-FAULT! !!!")
end
@info "Real test started!!!!"
for i in ProgressBar (1:10000)
sleep (0.0001)
lr = linear_regression(3)
x=randn(2,3)
@assert lr(x)==x**lr*.+lr. bias
end
@info "Oki doki!!!"
return 1
end
### Let's give a try!!!!!!!
unit_test_forward_pass ()
## we shall now implement fit! method!!!!
## before we get ready run the next 5 lines to see in this setting grad function returns a dictionary:
#=
Lr = Linear_regression(10)
val, grad = Zygote.withgradient (Lr) do Lr
end
=#
function update_grads!(1r::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!!!
return nothing
end
function fit!(lr:: Linear_regression,
x : :AbstractMatrix,
y: :AbstractVector;
1nunnina natn..ElnntaA - A agah1function 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!!!
return nothing
end
fu
image text in transcribed

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

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions