Introduction to Machine Learning for Web Developers with ml5.js
Welcome to the world of machine learning accessible to web developers! This chapter will guide you through the fundamentals of incorporating machine learning into web applications using ml5.js, a user-friendly JavaScript library. Developed by Henry Li, this course and chapter aim to demystify machine learning and empower web developers to create intelligent and interactive web experiences.
ml5.js: A JavaScript library that simplifies the integration of machine learning models into web applications. It is built on top of TensorFlow.js, providing an accessible interface for developers without requiring deep knowledge of machine learning mathematics.
1. Getting Started with ml5.js
This chapter assumes a foundational understanding of HTML, CSS, and JavaScript, including basic DOM manipulation. To follow along with the practical exercises, you should:
-
Clone the GitHub Repository: Obtain the practice files and final projects by cloning the repository associated with this course. This repository is structured to facilitate hands-on learning.
GitHub Repository: A web-based platform for version control and collaboration using Git. Repositories store project files and their revision history, allowing for easy sharing and collaborative development.
-
Explore the Repository Structure: Familiarize yourself with the repository’s organization, which includes:
final-projects/
: Contains completed examples of various machine learning projects.practice/
: The primary directory for hands-on exercises, broken down into specific machine learning concepts.- Pre-trained Models: Projects focusing on utilizing existing machine learning models.
- Transfer Learning: Exercises exploring the adaptation of pre-trained models to new tasks.
- Custom Models: Projects involving building machine learning models from scratch.
- Neural Networks: Examples demonstrating the use of neural networks in web applications.
- Rock Paper Scissors Game with Teachable Machines: A practical project integrating a Teachable Machine model into a web game.
-
Practice Exercises: Within the
practice/
directory, you’ll find projects with partially completed code (part1.js
,part2.js
, etc.). Your goal is to fill in these gaps to understand and implement the core components of integrating machine learning into web projects. -
Reference Final Projects: If you encounter difficulties, the
final-projects/
directory provides fully functional examples for guidance and troubleshooting. It also includes additional resources and projects like Kaggle integration and model saving techniques.
2. Fundamentals of Machine Learning: A High-Level Overview
Machine learning, often perceived as a complex and intimidating field, becomes approachable with libraries like ml5.js. Let’s break down the core concepts in a simplified manner.
2.1 What is Machine Learning?
Machine learning is fundamentally about enabling computers to learn patterns from data. Instead of explicitly programming every step, we train computers to identify relationships and make predictions or decisions autonomously based on the data they’ve been exposed to.
Machine Learning: A field of computer science that focuses on enabling computer systems to learn from data without being explicitly programmed. It involves algorithms that can improve their performance on a specific task as they are exposed to more data.
2.2 The Machine Learning Learning Process
The learning process in machine learning can be summarized in three fundamental steps, often iterative:
- Guess: The machine learning model makes an initial prediction or “guess” based on the input data and its current understanding.
- Compare and Evaluate: This guess is then compared to the actual, correct answer. The difference between the guess and the actual answer is measured to determine how “wrong” the guess was.
- Optimize and Refine: Based on the error identified in the comparison step, the model adjusts its internal parameters to improve its future guesses. This adjustment aims to minimize the error and make more accurate predictions in subsequent iterations.
This cycle of guessing, comparing, and optimizing is repeated many times, allowing the model to progressively learn from the data and improve its predictive capabilities.
2.3 Features and Weights: Deconstructing the Learning Mechanism
To understand how machines learn patterns, consider the “creature guessing game” example presented in the transcript. In this game, you were presented with descriptions of creatures, and you learned to predict the answer based on patterns in the descriptions.
-
Features: These are the distinct, measurable attributes or characteristics of the data that the machine learning model analyzes. In the creature game, the features were:
- Body Color
- Body Parts
- Number of Body Parts
- Hobbies
-
Weights: Weights represent the importance or relevance assigned to each feature in determining the final prediction. During the learning process, the model adjusts these weights to prioritize features that are most predictive and de-emphasize less relevant ones. In the creature game, you intuitively assigned higher weights to “Body Color” and “Body Parts” because they were directly related to the answer, while “Number of Body Parts” and “Hobbies” were irrelevant and effectively assigned a weight of zero.
This process can be represented mathematically as an equation, where the predicted answer is a weighted sum of the features:
Answer = (Color * Color Weight) + (Body Part * Body Part Weight) + (Number of Body Parts * Number of Body Parts Weight) + (Hobby * Hobby Weight)
In essence, machine learning models automatically learn and adjust these feature weights through repeated exposure to data and feedback from the comparison and optimization steps.
2.4 Inspiration from the Human Brain: Neural Networks
The learning process in machine learning, particularly the concept of adjusting weights based on input features, draws inspiration from the structure and function of the human brain, specifically neurons and neural networks.
Neurons: In the context of biology, neurons are nerve cells that transmit information through electrical and chemical signals. In machine learning, artificial neurons are mathematical functions that mimic this behavior, processing inputs and producing outputs.
Neural Networks: Interconnected networks of artificial neurons, organized in layers, that are designed to recognize patterns in data. Inspired by the biological neural networks in the brain, these networks learn by adjusting the connections (weights) between neurons.
Simplified Neural Network Architecture:
Imagine a simplified neural network with the following components:
- Input Layer: Receives the raw input data (features).
- Hidden Layer(s): One or more intermediate layers where complex computations and pattern recognition occur.
Hidden Layer: In a neural network, layers between the input and output layers are called hidden layers. These layers perform complex transformations on the input data, enabling the network to learn intricate patterns.
- Output Layer: Produces the final prediction or output.
The Learning Cycle within a Neural Network:
- Input: Data is fed into the input layer.
- Processing (Hidden Layers): Data flows through the hidden layers, undergoing transformations and computations within each neuron, influenced by the weights and connections.
- Prediction: The output layer generates a prediction.
- Loss Function: The prediction is compared to the true value using a loss function, which quantifies the error or difference between the prediction and the actual value.
Loss Function: A mathematical function that measures the difference between the predicted output of a machine learning model and the actual target value. The goal of training is to minimize this loss, indicating better model performance.
- Optimizer: An optimizer algorithm uses the loss score to adjust the weights within the neural network. The goal of the optimizer is to modify these weights in a direction that reduces the loss, thereby improving the model’s accuracy in subsequent predictions.
Optimizer: An algorithm used to adjust the parameters (weights and biases) of a machine learning model during training to minimize the loss function. Common optimizers include Gradient Descent and its variants.
- Iteration (Training): This entire process is repeated iteratively across the training dataset, refining the weights and improving the model’s ability to make accurate predictions. This iterative refinement process is known as training.
Training: The process of teaching a machine learning model to learn patterns from data. It involves feeding the model training data, calculating the loss, and optimizing the model’s parameters to minimize the loss and improve its performance over time.
Key Components of Machine Learning:
In summary, machine learning, at its core, involves:
- Data: The raw material from which the model learns patterns.
- Training: The iterative process of exposing the model to data, adjusting its parameters, and improving its predictive accuracy.
- Model: The trained algorithm that embodies the learned patterns and is capable of making inference or predictions on new, unseen data.
Inference (Prediction): The process of using a trained machine learning model to make predictions or decisions on new, unseen data. This is also referred to as “prediction” or “testing” phase.
3. Teachable Machines: Visualizing the Machine Learning Pipeline
Teachable Machines is a user-friendly web application that provides a visual and intuitive way to understand the machine learning pipeline – from data input to model training and prediction. It’s an excellent tool for beginners to grasp the practical aspects of machine learning without writing code initially.
Teachable Machines: A web-based tool developed by Google that allows users to train machine learning models directly in the browser without writing code. It simplifies the process of data collection, model training, and model export, making machine learning accessible to non-experts.
Using Teachable Machines (Image Project Example):
- Access Teachable Machines: Go to the Teachable Machines website and select “Get Started.”
- Choose Project Type: Select “Image Project” and then “Standard Image Model.”
- Data Collection:
- Classes/Labels: Define categories or classes you want the model to learn to distinguish (e.g., “Dogs,” “Cats”).
- Data Input: For each class, upload images relevant to that category. You can use your webcam to capture images directly or upload image files. Aim for a diverse and representative dataset for each class.
- Training: Click the “Train Model” button. Teachable Machines will use transfer learning (explained in detail later) behind the scenes, leveraging a pre-trained model to learn to differentiate between your defined classes based on your uploaded images.
- Preview and Prediction: Once training is complete, you can test your model within the Teachable Machines interface. Upload or use your webcam to provide new images. The model will predict the class of the input image and display its confidence level for each class.
- Model Export: Teachable Machines allows you to export your trained model for use in your web projects. You can export in various formats, including TensorFlow.js, which is directly compatible with ml5.js.
Export: The process of saving a trained machine learning model in a format that can be used outside of the training environment, such as in web applications or other software.
TensorFlow.js: A JavaScript library for training and deploying machine learning models in the browser and on Node.js. ml5.js is built on top of TensorFlow.js, providing a higher-level, more user-friendly API.
TensorFlow.js Model File Structure:
When you export a TensorFlow.js model from Teachable Machines (or download one), you’ll typically find three key files:
model.json
(Model Architecture): This file describes the structure and layers of the neural network model. It defines the model’s blueprint.Model Architecture (model.json): Describes the structure of a machine learning model, including the types of layers, their connections, and the overall organization of the network.
metadata.json
(Model Metadata): Contains metadata about the model, such as the class labels (e.g., “dogs,” “cats”) that the model was trained to recognize.Metadata (metadata.json): Information about the model itself, rather than the data it processes. This can include class labels, input shapes, and other configuration details.
weights.bin
(Model Weights): This is a binary file that holds the numerical values of the model weights learned during the training process. These weights are crucial for the model’s ability to make predictions.Model Weights (weights.bin): Numerical parameters within a machine learning model that are learned during training. They represent the strength of connections between neurons in a neural network and are adjusted to minimize the loss function.
These files together constitute the complete trained machine learning model, ready to be loaded and used in web applications using libraries like ml5.js.
4. Project 1: Rock Paper Scissors Game with Teachable Machines and ml5.js
This project demonstrates how to integrate a Teachable Machines model into a web-based Rock Paper Scissors game using ml5.js.
4.1 Project Setup and Code Structure
- Repository and Project Files: Ensure you have cloned the GitHub repository and navigate to the
practice/rock-paper-scissors-game/
directory. index.html
: This file sets up the basic HTML structure:- Includes the ml5.js CDN (Content Delivery Network) link to access the ml5.js library in your project.
CDN (Content Delivery Network): A geographically distributed network of servers that delivers web content to users based on their location. Using a CDN for libraries like ml5.js ensures faster loading times and efficient content delivery.
- Sets up a
<video>
element to capture webcam input for gesture recognition. - Displays elements to show game results (win, lose, tie) and user gesture predictions.
- Includes a button to initiate each round of the game.
- Includes the ml5.js CDN (Content Delivery Network) link to access the ml5.js library in your project.
script.js
: This file contains the JavaScript code to:- Load the Teachable Machines model.
- Capture video from the webcam.
- Use ml5.js to classify hand gestures (rock, paper, scissors) from the video stream.
- Implement the game logic for Rock Paper Scissors.
4.2 Implementing Machine Learning in script.js
The key machine learning components within script.js
are:
-
Model URL (
modelURL
variable): This variable will store the URL of your Teachable Machines model. You will need to train a Teachable Machines image model to recognize “rock,” “paper,” and “scissors” hand gestures, export it as a TensorFlow.js model, and upload it to a hosting service (or use local hosting during development). The URL of themodel.json
file will be used asmodelURL
. -
Classifier Initialization (
classifier
variable):let classifier; ml5.imageClassifier(modelURL, modelLoaded);
ml5.imageClassifier(modelURL, modelLoaded)
: This ml5.js function creates an image classifier.Image Classifier: A machine learning model that is trained to categorize images into predefined classes. It takes an image as input and outputs the predicted class label and confidence score.
modelURL
: Specifies the location of your Teachable Machines model (themodel.json
file URL).modelLoaded
: A callback function that is executed once the model is successfully loaded into the project.
-
modelLoaded()
Callback Function:function modelLoaded() { console.log('Model Loaded!'); startVideo(); // Function to start webcam video classifyGesture(); // Function to start gesture classification }
- This function is called after the model is loaded. It typically starts the webcam video stream and initiates the
classifyGesture()
function to begin continuous prediction.
- This function is called after the model is loaded. It typically starts the webcam video stream and initiates the
-
classifyGesture()
Function (Prediction):function classifyGesture() { classifier.classify(video, (error, results) => { if (error) { console.error(error); return; } userChoice = results[0].label; // Get the top prediction label gestureText.innerText = `Gesture: ${userChoice}`; classifyGesture(); // Recursively call for continuous prediction }); }
classifier.classify(video, callback)
: This ml5.js function performs image classification.video
: The HTML<video>
element capturing the webcam stream.callback
: A function that is executed after the classification is complete. It receives two arguments:error
(if any error occurred) andresults
(an array of prediction results).
results
: An array of predictions, typically sorted by confidence.results[0]
represents the prediction with the highest confidence.results[0].label
: Extracts the predicted class label (e.g., “rock,” “paper,” “scissors”) from the top prediction.gestureText.innerText = ...
: Updates the HTML element to display the user’s predicted gesture.classifyGesture()
: The function calls itself recursively usingclassifyGesture()
to create a continuous loop of prediction, allowing for real-time gesture recognition from the video stream.
-
Game Logic (
playGame()
function): This function (already provided in the project) handles the Rock Paper Scissors game logic:- Randomly chooses a gesture for the computer.
- Compares the user’s gesture (
userChoice
) with the computer’s choice. - Determines the game outcome (win, lose, tie).
- Updates the HTML to display the game result.
Workflow:
- Train Teachable Machines Model: Train an image model in Teachable Machines to recognize “rock,” “paper,” and “scissors” hand gestures using your webcam.
- Export and Host Model: Export the Teachable Machines model as a TensorFlow.js model and obtain the URL of the
model.json
file. - Update
modelURL
: Replace the placeholdermodelURL
inscript.js
with the actual URL of your exported model. - Run
index.html
: Openindex.html
in a web browser. Allow webcam access when prompted. - Play the Game: Make hand gestures in front of the webcam. The ml5.js model will predict your gesture, and you can play Rock Paper Scissors against the computer by clicking the “Start Round” button.
5. Exploring Pre-trained Models: MobileNet
Pre-trained models are machine learning models that have been trained on vast datasets by experts. These models have already learned general features and patterns from a wide range of data, saving you the effort and resources of training from scratch. ml5.js provides easy access to several pre-trained models, including MobileNet.
Pre-trained Model: A machine learning model that has been previously trained on a large dataset for a specific task. These models can be used directly for inference or further fine-tuned for new tasks through transfer learning.
MobileNet: A pre-trained image classification model developed by Google, known for its efficiency and suitability for mobile and edge devices. It is trained on a massive dataset (ImageNet) and can recognize thousands of different object categories.
Project 2: Image Classification with MobileNet
This project demonstrates how to use the pre-trained MobileNet model in ml5.js to classify images uploaded to a web page.
5.1 Project Setup and Code Structure
- Repository and Project Files: Navigate to the
practice/pre-trained-models/
directory in the cloned repository. index.html
: This file sets up the HTML:- Includes the ml5.js CDN link.
- Provides an input element (
<input type="file">
) to allow users to upload image files. - Includes an
<img>
element to display the uploaded image. - Provides an element to display the classification results.
script.js
: Contains the JavaScript code to:- Load the pre-trained MobileNet model.
- Handle image file uploads.
- Use ml5.js to classify the uploaded image using MobileNet.
- Display the classification results.
5.2 Implementing MobileNet Classification in script.js
-
Classifier Initialization (MobileNet):
let classifier; ml5.imageClassifier('MobileNet', modelLoaded);
ml5.imageClassifier('MobileNet', modelLoaded)
: This initializes an image classifier using the pre-trained MobileNet model. Note that instead of amodelURL
, we simply provide the string'MobileNet'
to tell ml5.js to load the built-in MobileNet model.
-
modelLoaded()
Callback Function:function modelLoaded() { console.log('Model Loaded!'); message.innerText = 'MobileNet model loaded. Please load a file.'; }
- Indicates that the MobileNet model has been successfully loaded.
-
loadImageFile()
Function (Image Upload and Classification):function loadImageFile(event) { const file = event.target.files[0]; mainImage.src = URL.createObjectURL(file); // Display uploaded image classifier.classify(mainImage, (error, results) => { if (error) { console.error(error); return; } message.innerText = `Classification: ${results[0].label} (Confidence: ${results[0].confidence.toFixed(2)})`; }); }
loadImageFile(event)
: This function is triggered when a user selects an image file using the input element.mainImage.src = ...
: Sets thesrc
attribute of the<img>
element to display the uploaded image in the browser.classifier.classify(mainImage, callback)
: Classifies the uploaded image using the MobileNet model.mainImage
: The<img>
element displaying the uploaded image.callback
: Handles the classification results.
results[0].label
: Extracts the top predicted class label from MobileNet.results[0].confidence.toFixed(2)
: Extracts the confidence score (0 to 1) of the top prediction and formats it to two decimal places.message.innerText = ...
: Updates the HTML element to display the classification result (label and confidence).
Workflow:
- Run
index.html
: Openindex.html
in a web browser. - Upload Image: Click the “Choose File” button and select an image file from your computer.
- Classification: MobileNet will analyze the uploaded image and display its classification prediction (e.g., “Labrador Retriever,” “Cat,” “Tree”) along with a confidence score.
Limitations of Pre-trained Models:
While pre-trained models like MobileNet are powerful, they are limited to the categories they were originally trained to recognize (in MobileNet’s case, thousands of object categories from the ImageNet dataset). If you want to classify images into categories outside of MobileNet’s training data (e.g., specific types of dinosaurs like T-Rex), you’ll need to use transfer learning or train a custom model.
6. Transfer Learning: Adapting Pre-trained Models
Transfer learning is a technique that leverages the knowledge learned by a pre-trained model and applies it to a new, related task. Instead of training a model from scratch, transfer learning allows you to fine-tune an existing pre-trained model on a smaller, task-specific dataset. This approach is often faster, requires less data, and can achieve better performance than training a custom model from scratch, especially when you have limited data for your specific task.
Transfer Learning: A machine learning technique where a model trained for one task is reused as the starting point for a model on a second, related task. It leverages the learned features from the original task to accelerate and improve learning on the new task.
Analogy for Transfer Learning:
Imagine you’ve already learned JavaScript (your “pre-trained model” of programming knowledge). Now you want to learn Python. Because you understand fundamental programming concepts like loops, variables, and conditional statements from JavaScript, learning Python will be much faster and easier than if you were starting from zero. You are “transferring” your existing programming knowledge to a new programming language.
Project 3: Image Classification with Transfer Learning (Llama vs. T-Rex)
This project demonstrates how to use transfer learning with ml5.js to adapt MobileNet to classify images of llamas and T-Rexes, categories that MobileNet might not accurately distinguish out-of-the-box.
6.1 Project Setup and Code Structure
- Repository and Project Files: Navigate to the
practice/transfer-learning/
directory in the cloned repository. index.html
: Sets up the HTML:- Includes an older version of the ml5.js CDN link. Important: This project uses an older version of ml5.js because the latest versions have removed the
featureExtractor
module, which is essential for transfer learning in this example. - Provides input elements for uploading images and entering class labels (“Llama,” “T-Rex”).
- Includes buttons for “Add Image,” “Train Model,” and “Predict.”
- Displays elements to show uploaded images, training progress (loss value), and prediction results.
- Includes an older version of the ml5.js CDN link. Important: This project uses an older version of ml5.js because the latest versions have removed the
script.js
: Contains the JavaScript code to:- Load MobileNet as a feature extractor.
- Create a new classifier on top of the feature extractor.
- Collect image data for “llama” and “t-rex” classes with user-provided labels.
- Train the classifier using the collected data.
- Predict the class of a new uploaded image using the trained classifier.
6.2 Implementing Transfer Learning in script.js
-
Feature Extractor Initialization (MobileNet):
let featureExtractor; let classifier; featureExtractor = ml5.featureExtractor('MobileNet', modelLoaded);
ml5.featureExtractor('MobileNet', modelLoaded)
: Initializes MobileNet as a feature extractor.Feature Extractor: In transfer learning, a feature extractor is the base pre-trained model (e.g., MobileNet) used to extract meaningful features from input data. These extracted features are then used to train a new classifier for the specific task.
- Instead of directly using
ml5.imageClassifier('MobileNet', ...)
, we first create afeatureExtractor
. This allows us to access the pre-trained features of MobileNet without its original classification layer.
-
Classifier Creation:
function modelLoaded() { console.log('Feature Extractor Model Loaded!'); classifier = featureExtractor.classification(videoOrImage); // ... (rest of modelLoaded function) }
classifier = featureExtractor.classification(videoOrImage)
: Creates a new classifier on top of thefeatureExtractor
. This new classifier will be trained to distinguish between “llama” and “t-rex” classes using the features extracted by MobileNet.
-
addImage()
Function (Data Collection):function addImage(label) { classifier.addImage(uploadedImage, label); // Add image to classifier with label // ... (update UI messages) }
classifier.addImage(uploadedImage, label)
: This function adds an uploaded image and its corresponding label (e.g., “llama” or “t-rex”) to the classifier’s training data. ml5.js will use thefeatureExtractor
(MobileNet) to extract features from theuploadedImage
and associate them with the givenlabel
.
-
trainModel()
Function (Training):function trainModel() { classifier.train((lossValue) => { if (lossValue) { // ... (display loss value during training) } else { // ... (training complete) } }); }
classifier.train(callback)
: Starts the training process for the new classifier.callback(lossValue)
: A callback function that is executed during training. It receives thelossValue
as an argument.Loss Value: A numerical value indicating the error rate of the model during training. A lower loss value generally indicates better model performance.
- The code displays the
lossValue
during training to show progress. WhenlossValue
becomesnull
(orundefined
), it indicates that training is complete.
-
predict()
Function (Prediction):function predict() { classifier.classify(uploadedImage, (err, results) => { if (err) { console.error(err); } message.innerText = `Prediction: ${results[0].label} (Confidence: ${results[0].confidence.toFixed(2)})`; }); }
classifier.classify(uploadedImage, callback)
: Performs classification on a new uploaded image using the newly trained classifier (which is built on top of MobileNet’s features). The prediction results will indicate whether the image is classified as “llama” or “t-rex.”
Workflow:
- Run
index.html
: Openindex.html
in a web browser. - Add Llama Images: Enter “llama” in the label input, upload several images of llamas, and click “Add Image” for each.
- Add T-Rex Images: Enter “t-rex” in the label input, upload several images of T-Rexes, and click “Add Image” for each.
- Train Model: Click the “Train Model” button. Observe the loss value decreasing during training.
- Predict: Once training is complete, upload a new image (either a llama or a T-Rex) and click “Predict.” The model will classify the image as either “llama” or “t-rex” based on the transfer learning process.
Key Advantages of Transfer Learning:
- Reduced Training Data: Transfer learning often requires significantly less training data compared to training a custom model from scratch.
- Faster Training: Training time is typically shorter because the pre-trained feature extractor has already learned general image features.
- Improved Performance: Transfer learning can lead to better performance, especially when you have limited data, by leveraging the robust features learned by the pre-trained model.
Misconception about Transfer Learning:
It’s important to understand that transfer learning in this context does not mean “building on top” of MobileNet in a way that expands its original capabilities. Instead, it’s more accurate to think of it as repurposing MobileNet’s learned features for a new, specific task (llama vs. T-Rex classification). The original MobileNet model’s ability to recognize thousands of object categories is not necessarily retained or enhanced in this transfer learning process. In fact, the new classifier is specifically trained for the llama vs. T-Rex task and may not be as effective at classifying other objects that MobileNet was originally trained on.
7. Custom Models: Building Neural Networks from Scratch
When pre-trained models or transfer learning are insufficient for your specific needs, you can build custom machine learning models from scratch. This involves defining the architecture of a neural network and training it on your own data. While more complex, building custom models provides maximum flexibility and control over the learning process.
Custom Model: A machine learning model built and trained from scratch, as opposed to using pre-trained models or transfer learning. This approach allows for greater flexibility in model architecture and training data, tailored to specific tasks.
Project 4: Student Score Prediction with a Custom Neural Network
This project demonstrates how to build a custom neural network using ml5.js to predict student exam scores based on factors like study hours, attendance, and previous exam scores. This is an example of a regression problem, where the goal is to predict a continuous numerical value (the exam score).
Regression: A type of machine learning problem where the goal is to predict a continuous numerical output variable. Examples include predicting house prices, stock prices, or, in this case, exam scores.
7.1 Project Setup and Code Structure
- Repository and Project Files: Navigate to the
practice/neural-network-basic/
directory in the cloned repository. index.html
: Sets up the HTML:- Includes the latest version of the ml5.js CDN link.
- Provides input fields for “Hours Studied Per Week,” “Attendance Percentage,” and “School Exam Scores.”
- Includes a “Predict Score” button.
- Displays an element to show the predicted state exam score.
script.js
: Contains the JavaScript code to:- Set up a custom neural network using ml5.js.
- Define the training data (student study habits and exam scores).
- Train the neural network.
- Predict state exam scores based on user inputs.
- (Optional) Display a graph of the training loss over epochs.
7.2 Implementing a Custom Neural Network in script.js
-
WebGL Backend Setup:
ml5.setBackend('webgl');
ml5.setBackend('webgl')
: Sets the backend for ml5.js to WebGL.WebGL (Web Graphics Library): A JavaScript API for rendering interactive 2D and 3D graphics within any compatible web browser without the use of plug-ins. In ml5.js, using WebGL as a backend can improve performance, especially for computationally intensive tasks like training neural networks, by leveraging the GPU (Graphics Processing Unit). GPU (Graphics Processing Unit): A specialized electronic circuit designed to rapidly manipulate and alter memory to accelerate the creation of images in a frame buffer intended for output to a display device. GPUs are highly efficient for parallel computations, making them beneficial for machine learning tasks.
-
Neural Network Initialization:
const brain = ml5.neuralNetwork({ task: 'regression', debug: true });
ml5.neuralNetwork({ task: 'regression', debug: true })
: Creates a new neural network instance.task: 'regression'
: Specifies that this neural network is for a regression task (predicting a continuous value).debug: true
: Enables debugging output, including a graph of the training loss value displayed in the browser.
-
Data Loading and Normalization:
const trainingData = [ // ... (array of student data objects) ]; trainingData.forEach(item => { const inputs = { hours_studied: item.hours_studied, attendance: item.attendance, school_exam_score: item.school_exam_score }; const output = { state_exam_score: item.state_exam_score }; brain.addData(inputs, output); }); brain.normalizeData();
trainingData
: An array of JavaScript objects, where each object represents a student’s data, including input features (study hours, attendance, school exam score) and the target output (state exam score). Note: The provided data is synthetic (fake).brain.addData(inputs, output)
: Adds each data point to the neural network’s training dataset.brain.normalizeData()
: Normalizes the input and output data.Normalizing Data: A data preprocessing technique used to rescale numerical data to a standard range, typically between 0 and 1 or -1 and 1. This can improve the training process and performance of machine learning models by preventing features with larger ranges from dominating the learning process.
-
Training Options and Training Process:
const trainingOptions = { epochs: 32, // Number of epochs batchSize: 12 // Batch size }; brain.train(trainingOptions, finishedTraining);
trainingOptions
: An object defining the training parameters.epochs: 32
: Sets the number of epochs to 32.Epoch (Epot): One complete pass through the entire training dataset during the training process. Multiple epochs are typically required for a model to learn effectively.
batchSize: 12
: Sets the batch size to 12.Batch Size: The number of training samples processed in one iteration during training. Instead of processing the entire dataset at once, data is divided into batches to improve training efficiency and potentially generalization.
brain.train(trainingOptions, finishedTraining)
: Starts the training process with the specified options and a callback functionfinishedTraining
to be executed when training is complete.
-
finishedTraining()
Callback Function:function finishedTraining() { console.log('Model training complete!'); // ... (optional: enable prediction button) }
- Called when the training process is finished.
-
predictScore()
Function (Prediction):function predictScore() { const hours = Number(hoursInput.value); const attendance = Number(attendanceInput.value); const schoolScore = Number(schoolScoreInput.value); const inputs = { hours_studied: hours, attendance: attendance, school_exam_score: schoolScore }; brain.predict(inputs, (error, results) => { if (error) { console.error(error); return; } predictedScoreOutput.textContent = results[0].value.toFixed(2); }); }
predictScore()
: Called when the “Predict Score” button is clicked.- Collects input values from the HTML input fields.
brain.predict(inputs, callback)
: Uses the trained neural network to predict the state exam score based on the provided input features.results[0].value.toFixed(2)
: Extracts the predicted score from the results and formats it to two decimal places.predictedScoreOutput.textContent = ...
: Updates the HTML element to display the predicted score.
Workflow:
- Run
index.html
: Openindex.html
in a web browser. Training will start automatically when the page loads. Observe the training loss graph. - Enter Input Values: Once training is complete, enter values for “Hours Studied Per Week,” “Attendance Percentage,” and “School Exam Scores” in the input fields.
- Predict Score: Click the “Predict Score” button. The predicted state exam score will be displayed.
Experimenting with Training Parameters:
- Epochs: Increasing the number of epochs might improve accuracy to a certain point, but excessive epochs can lead to overfitting, where the model memorizes the training data too well and performs poorly on new, unseen data.
Overfitting: A phenomenon in machine learning where a model learns the training data too well, including noise and random fluctuations, leading to excellent performance on the training data but poor generalization to new, unseen data.
- Batch Size: Experimenting with different batch sizes can affect training speed and model performance.
- Validation Data: For more robust model development, it’s crucial to split your data into training data and validation data. Validation data is used to evaluate the model’s performance on unseen data during training and helps to detect and prevent overfitting and underfitting.
Validation Data: A portion of the dataset that is held back during training and used to evaluate the model’s performance on unseen data. It helps to assess generalization and tune hyperparameters. Underfitting: A phenomenon in machine learning where a model is too simple to capture the underlying patterns in the training data, leading to poor performance on both training and unseen data.
8. Advanced Concepts: Kaggle Datasets and Saving Models
8.1 Utilizing Kaggle Datasets
Kaggle is a platform for data science and machine learning competitions. It also hosts a vast repository of datasets that you can use for your machine learning projects.
Kaggle: An online community and platform for data scientists and machine learning practitioners. It hosts competitions, datasets, and notebooks, providing resources for learning and collaboration in the field of data science.
Dataset: A collection of data, often organized in a structured format (e.g., tables, CSV files), used for training, validating, and testing machine learning models.
Example: Student Grade Prediction Dataset from Kaggle
The transcript mentions a “Student Grade Prediction” dataset available on Kaggle. This dataset contains real-world data about student demographics, study habits, and grades. You can download datasets from Kaggle and use them to train more realistic and robust machine learning models.
Steps to Use Kaggle Datasets:
- Create a Kaggle Account: Sign up for a free account on the Kaggle website.
- Browse Datasets: Explore the “Datasets” section and search for datasets relevant to your project (e.g., “student grade prediction,” “plant diseases,” etc.).
- Download Dataset: Download the dataset files (often in CSV (Comma Separated Values) format).
CSV (Comma Separated Values): A common file format for storing tabular data, where values are separated by commas and rows are separated by line breaks.
- Data Conversion (Optional): You may need to convert the CSV data into a JSON (JavaScript Object Notation) format for easier use in JavaScript. Online tools can assist with CSV to JSON conversion.
JSON (JavaScript Object Notation): A lightweight data-interchange format that uses human-readable text to transmit data objects consisting of attribute-value pairs and array data types.
- Integrate Data into Project: Load the JSON data into your ml5.js project and use it as your
trainingData
for custom models or for other machine learning tasks.
8.2 Saving and Loading Models
ml5.js provides functionality to save trained custom neural network models. This allows you to train a model once, save it, and then load and reuse it later without retraining, saving significant time and resources.
Saving a Model:
brain.save('student-score-predictor-model'); // Saves model files for download
brain.save('model-name')
: After training is complete, call thesave()
method on your neural network instance (brain
). This will trigger a download of the model files (model.json
,metadata.json
,weights.bin
) to your computer.
Loading a Saved Model:
const brain = ml5.neuralNetwork({ task: 'regression', debug: false });
const modelInfo = {
model: 'model/student-score-predictor-model.json', // Path to model.json
metadata: 'model/student-score-predictor-model_meta.json', // Path to metadata.json
weights: 'model/student-score-predictor-model.weights.bin' // Path to weights.bin
};
brain.loadModel(modelInfo, modelLoaded); // Load model files
function modelLoaded() {
console.log('Pre-trained model loaded!');
// ... (model is ready for prediction)
}
- Create a new neural network instance: Initialize a new
ml5.neuralNetwork
object, similar to how you would for training. modelInfo
object: Create an objectmodelInfo
that specifies the paths to the saved model files (model.json
,metadata.json
,weights.bin
). Ensure these files are placed in your project directory (e.g., in amodel/
folder).brain.loadModel(modelInfo, modelLoaded)
: Use theloadModel()
method to load the saved model files. ThemodelLoaded
callback function will be executed once the model is successfully loaded.- After the
modelLoaded
callback is executed, yourbrain
instance will be loaded with the pre-trained model, and you can directly use it for prediction without retraining.
Benefits of Saving and Loading Models:
- Time Savings: Avoids retraining the model every time you want to use it.
- Resource Efficiency: Reduces computational resources needed for repeated training.
- Deployment: Allows you to deploy pre-trained models in web applications or other environments without needing to include the training process.
9. Conclusion: Embracing Machine Learning for Web Development
Congratulations on reaching the end of this introductory chapter! You’ve explored the fundamental concepts of machine learning and how to integrate them into web projects using ml5.js. You’ve learned about:
- Core Machine Learning Concepts: Data, training, models, features, weights, neural networks, loss functions, optimizers, epochs, batch size, overfitting, and underfitting.
- Tools and Libraries: ml5.js, TensorFlow.js, Teachable Machines, Kaggle.
- Types of Machine Learning Models: Pre-trained models (MobileNet), transfer learning, and custom neural networks.
- Machine Learning Tasks: Image classification and regression.
- Practical Projects: Rock Paper Scissors game, image classification with MobileNet and transfer learning, and student score prediction with a custom neural network.
- Advanced Topics: Kaggle datasets and model saving/loading.
This chapter is intended to spark your curiosity and provide a foundation for further exploration. Machine learning is a vast and constantly evolving field. Continue to experiment, explore the ml5.js documentation, delve into TensorFlow.js, and challenge yourself to build innovative and intelligent web experiences. The possibilities are limitless!