Question: please solve in Julia(in VS Code) function huber_loss(y_pred::T, y_true::T; ::Float64 = 0.1) where T

please solve in Julia(in VS Code)
function huber_loss(y_pred::T, y_true::T; ::Float64 = 0.1) where T <: Real
return nothing
end
function huber_loss(y_pred::AbstractArray{T}, y_true::AbstractArray{T}) where T <: Real
return nothing
end
function unit_test_huber_loss()::Bool
Random.seed!(0)
@assert huber_loss(1.0,1.0) == 0.0
@assert huber_loss(1.0,2.0; = 0.9) == 0.49500000000000005
@assert 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 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: and bias.
::AbstractVector
bias::Real
linear_regression(n::Int64) = new(0.004*randn(n), zero(1))
end
### write the forwardpass function
function (lr::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)
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
norm(lr.)^2 + lr.bias^2
end
=#
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!!!
return nothing
end
function fit!(lr::linear_regression,
X::AbstractMatrix,
y::AbstractVector;
learning_rate::Float64 = 0.00001,
max_iter::Integer = 5,
::Float64 = 0.01)
##
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)
### Things seem to work fine!!!
function unit_test_for_fit()
Random.seed!(0)
lr = linear_regression(20)
X = randn(100, 20)
y = randn(100)
fit!(lr, X, y; learning_rate = 0.0001, max_iter = 10000, = 0.1)
@assert norm(lr.)^2 + lr.bias^2 < 0.01 "Your penalty method does not work!!!"
@assert mean((lr(X) - y).^2) < 1.3 "Yo do not fit perfectly!!!!"
@info "Okito dokito buddy!!!"
return 1
end
## Run next line to see what happens??? ##
unit_test_for_fit()
## -- ##

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!