Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Created June 29, 2023 22:56
Show Gist options
  • Save tpoisot/86e74422d345b5ec49afbaa2a745530c to your computer and use it in GitHub Desktop.
Save tpoisot/86e74422d345b5ec49afbaa2a745530c to your computer and use it in GitHub Desktop.
using Zygote
using Distributions
lm(x, m, b) = m .* x .+ b
x = rand(Uniform(-2, 2), 100)
ε = rand(Normal(0.0, 0.05), length(x))
p₀ = [-1.2, 0.05]
y = lm(x + ε, p₀...)
L2(y, x, f, p...) = sum(
(y .- f.(x, p...)).^2.0
)/length(x)
p = rand(length(p₀))
∇L(y, x, f, p...) = gradient(
(p...) -> L2(y, x, f, p...),
p...
)
η = 0.01
for epoch in 1:50_000
p .-= η .* ∇L(y, x, lm, p...)
end
round(p[1]; digits=5)
round(p[2]; digits=5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment