Categories: Julia

Regression with Julia

Today we’ll be taking a quick look at how to do regressions in Julia. But instead of using our advanced knowledge of Flux, or using a pre-made model from GLM.jl. We’ll use the bare basics, and hand-roll a naive toy regression. So as to show you how you yourself can use these basic tools and make your own models rather than being dependent.

For the purpose of this tutorial, instead of picking meaningful data, and carefully working with it, we’ll take random data, and purposefully overfit to it in order to lay bare the workings.

First of all, we’ll import Flux and Gadfly, for ease of differentiation and plotting respectively:

using Flux
using Gadfly

Now we’ll define our simple model to have just one dense layer, but we’ll not use Flux for this purpose, and instead do it ourselves:

model(X) = W*X .+ b

Let’s initialise all our variables to have random values:

function init(n::Int)
 W = rand(n)
 b = zeros(n)
 X, y = rand(n), rand(n)
 return W,b,X,y
end
W,b,X,y=init(100)

We also must define a loss function, so we’ll go with our trusty Euclidean norm:

loss(W,b,X,y) = sum(((W.-X).^2).+((b.-y).^2))

Now we introduce our only usage of Flux in this article:

d = gradient(() -> loss(W,b,X,y), params(W, b))

This stores the derivative of W and b in d. We then extract this derivative compactly via implicit destructuring:

dW,db = d[W],d[b]

Now. we update our weights and biases using a learning rate of 0.01:

W .-= 0.01 * dW
b .-= 0.01 * db

Also, we store the loss in a variable so that Pluto shows it to us easily:

l=loss(W,b,X,y)

Finally, we’ll plot using Gadfly, as the chosen problem lends itself particularly well to visualization:

plot(layer(x=X,y=y),layer(x=W,y=b,Geom.line))

And that’s it, you can take my word for this. working, and have the following animation as proof:

Or you can check our the code yourself at the following link:

https://github.com/mathmetal/Misc/blob/master/MLG/SimpleRegression.jl
Editorial Staff

Share
Published by
Editorial Staff

Recent Posts

MapReduce Algorithm

In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…

1 month ago

Linear Programming using Pyomo

Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…

8 months ago

Networking and Professional Development for Machine Learning Careers in the USA

In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…

11 months ago

Predicting Employee Churn in Python

Analyze employee churn, Why employees are leaving the company, and How to predict, who will…

1 year ago

Airflow Operators

Airflow operators are core components of any workflow defined in airflow. The operator represents a…

1 year ago

MLOps Tutorial

Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…

1 year ago