Toy Machine Learning with Haskell

Chris Smith
1 min readJun 5, 2020

In this post, I show how you can use Haskell and the ad package (automatic differentiation) to build a toy machine learning model in Haskell. I’ve tried to write enough that someone without a machine learning background can follow the code. If you do have an ML background, just skim those parts!

I’m going to try something different. Instead of writing in Medium, I’ve written up this post in comments inside of CodeWorld.

Part 1: Model Structure

In this part, I explain what a machine learning model is, define the Haskell types for the parts of the model, and write the code to make the model work. We end with a visualization of a pre-trained model to recognize which points are in a circle.

Link: https://code.world/haskell#PisFfAHxvUXzYBjnDoeRmew

Part 2: Training

In this part, I show how to use automatic differentiation to train the model that we defined in the previous section.

Link: https://code.world/haskell#PsgrIqxf3C_eFoRPqK3q0eQ

--

--

Chris Smith

Software engineer, volunteer K-12 math and computer science teacher, author of the CodeWorld platform, amateur ring theorist, and Haskell enthusiast.