Created
June 29, 2023 22:56
-
-
Save tpoisot/86e74422d345b5ec49afbaa2a745530c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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