Tutorial 4: Artificial Intelligence
This tutorial is designed for beginners in Artificial Intelligence, particularly those new to p5.js, ml5.js, or Machine Learning in general. It offers a simple, step-by-step approach that you can progress at your own pace. You can download the PDFs and print them out or have them displayed side by side with the p5.js web editor (recommended).
The tutorial is freely available, and the p5.js web editor is also free to download. By the end of this tutorial, you’ll have a solid understanding of Artificial Intelligence, Machine Learning, Neural Networks, and coding in p5.js. The possibilities for what you do with this knowledge are endless, and it’s up to your creativity and motivation to explore them.
The tutorial consists of three modules that guide you through a series of units that develop different concepts, ideas, and applications. While I recommend following the tutorial in a linear fashion, you’re free to skip or jump ahead if you prefer. If you’re new to coding or p5.js, I’ve included some coding snippets that cover the essential code you’ll need for this tutorial. However, I recommend not skipping these sections unless you’re already very familiar with p5.js or a seasoned coder.
Above all, enjoy the learning process and don’t be discouraged by setbacks. You can only grow in experience through doing.
Introduction to the tutorial
These three introductory sections are there to help you understanding what you are doing and why. I recommend reading through them and this will help you in the tutorial as you work through the units. I will assume that you have read through them so I don’t have to repeat myself. They are pretty brief and a quick read so don’t skimp on this despite your eagerness to get going, there are some key bits of information you will need for later on.
Module A building an AI model
Module A is where we start our journey, work through each unit and I recommend that you don’t skip a unit. The coding snippets have been kept as brief as possible so that when you are working through the AI units you have some understanding of p5.js while attempting to grasp the concepts of AI. This module uses ml5.js and explores some simple examples that will demonstrate key features.
Module A Unit #2 linear regression
In this first unit we plot points for a straight line with some variance (randomness). The neural network (ml5.js) will be trained on these data points and will approximate to the line. This is a regression task where you will train it on some synthetic data and then predict the outcome.

Module B pretrained models
Module B looks at some pretrained models which means that they have done all the hard work of collecting the data and training the models. The reason for this module is that you can use these pretrained models to develop other applications and creative endeavours. It is also quite fun.
Module C neuro evolution
Module C is a bit more challenging, it is looking at Reinforcement Learning through the use of Genetic Algorithms. The algorithm we will be using is a neuro evolution approach which means that we put a neural network in a car and a bird respectfully. We allow them to reproduce and over time we select the ones that perform best according to their fitness scores.
Module C Unit #2 Introduction to Genetic Algorithms
Although we will be using a neural network which means that it is a neural evolution approach, the fundamentals are based on genetic algorithms where we select the fittest and mate them to reproduce a better next generation. This is offers a bit more explanation.
