Fitting a straight line in Julia: Flux machine learning
julia
Flux
linear model
Author
Jong-Hoon Kim
Published
January 4, 2024
Fitting a straight line in Julia
This post is my attempt to learn machine learning in Julia. The contents of this page came from the Flux. Flux is a machine learning package written in Julia.
Create training and test data
usingFlux, Distributions, Random, Statistics# create the data# true parameter values are 4 and 2linear_model(x) =rand(Normal(4x+2,1))[1]
# define the loss function to use it for trainingloss(m, x, y) =mean(abs2.(m(x) .- y))
loss (generic function with 1 method)
loss(model, x_train, y_train)
335.3910074651219
Train the model
Flux package has the Flux.train! function for model training. The function requires an optimizer, which can be, for example, created using Descent function.