Appendix A

ml5.js

ml5.js is an open-source library for the web browser, is based on TensorFlow.js, and is written using JavaScript. Unlike TensorFlow.js however, ml5.js is a high-level library, which means that most operations that typically require multiple operations and code statements can be performed by a single line of code.

The ml5.js library borrows its name from another JavaScript library called p5.js, created by the same developers but has nothing to do with machine learning. Like ml5.js, pl5.js is also a high-level coding library and while a 360o view of pl5.js is out of scope of this text, you will find enough pointers to get started.

Note You can get more details online about the libraries mentioned above at their websites at https://p5js.org and https://ml5js.org.

Firstly, there are two methods in the pl5.js library to setup and execute JavaScript code; setup and draw. These two pl5.js methods are shown below in Listing A-1a.

Listing A-1a: Writing JavaScript code using pl5.js

function setup() {
 // Executes once
}
function draw() {
 // Executes repeatedly
}

As mentioned in the code comments above, the setup function executes only the first time the page loads, whereas the draw function keeps on executing.

Note You can certainly use ml5.js for machine learning without using pl5.js in your JavaScript code. However, the ml5.js example code contained within these pages uses the pl5.js library to keep the code simple and concise.

In Listing A-1b below, each of the two functions have a single JavaScript statement added to show an output in the web browser.

Listing A-1b: Writing JavaScript code using pl5.js

function setup() {
 createCanvas(640, 480);
}
function draw() {
 background(0);
}

In addition to the setup and draw JavaScript functions, p5.js includes other events as functions like preload, keyPressed, keyTyped, etc. You can get a list of properties and functions on the p5.js help page at https://p5js.org/reference

Tip If you want to learn more about pl5.js, a good place to start would be the book “Learn JavaScript with p5.js” by Engin Arslan published by Apress.

Why ml5.js

As already mentioned, ml5.js is a high-level library and does not require writing as much code for machine learning as required by the TensorFlow.js library. The following list captures some of the reasons behind the ml5.js library as it compares to other machine learning libraries, particularly TensorFlow.js

  1. Less Code: Though mentioned once earlier, it would be incumbent to mention that the biggest reason for using the ml5.js library would be the fact that it is high-level and built on top of TensorFlow.js, which means that programmers can write lesser code to achieve the same result.

  2. Faster Performance: Less JavaScript code also entails a much faster performance when executed in a web browser. This also means that programmers can take an existing model and train it with new data much faster.

  3. Client Execution: Since ml5.js executes in the web browser, it does not need to be installed on the system that needs to execute it and requires only a reference to the ml5.js CDN in the web page markup. Unlike the TensorFlow.js library, however, ml5.js cannot be executed using Node.js on the server.

Syntax

Refer to the following figures A-1a and A-1b to get a summarized view of the ml5.js library.

Figure A-1a: Classes by Code Namespaces in the ml5.js Library

Objects and methods in ml5.js are categorized by the following:

  1. Code Namespaces (Figure A-1a)

  2. Data Types (Figure A-1b)

Figure A-1b: Classes by Data Types in the ml5.js Library

You can read up on the library’s various classes and examples online, and the relevant sections here, based on the knowledge you gained throughout this book.

[todo]

Code Examples

[todo]

Note A machine learning experiment involves splitting the data into 2 sets, training and test. The test set is what determines the accuracy of an algorithm.

The Outcome of a machine learning experiment is the one with the highest accuracy and efficiency, selected from a variety of outputs or results of different algorithms chosen based on the training data and the value to be predicted from that data.

Tip Regardless of the machine learning problem you are trying to solve, you have to train an algorithm with the available data to create a prediction model. Training a ML model involves splitting the data into two parts: training and test (the training set is larger than the test set); the algorithm or model is first trained with the training data, and tested for prediction accuracy with the test data.

Key Takeaways

The takeaways from this short appendix is:

  • Why use a machine learning library based on TensorFlow.js

  • Syntax for ml5.js

  • Use cases

Last updated