Intro ML in JavaScript

Photo by Pietro Jeng on Unsplash

Machine learning has become a transformative field that has the potential to revolutionize how we solve complex problems and make predictions. Traditionally associated with languages like Python and R, machine learning is no longer limited to these languages. In recent years, JavaScript has emerged as a viable option for building machine learning models, thanks to libraries and frameworks like TensorFlow.js and Brain.js. In this article, we will explore introductory machine learning with JavaScript on the client-side, providing examples and explanations along the way.

Getting Started with Client-Side JavaScript for Machine Learning

To get started with machine learning in client-side JavaScript, you’ll need foundational knowledge of the language itself. JavaScript is a versatile programming language that runs in web browsers and can be used for a wide range of applications. Here are some essential concepts and tools you’ll need:

Web Browsers

You’ll be working in a web browser environment, so having a basic understanding of HTML and CSS is essential for creating user interfaces for your machine learning applications.

JavaScript Libraries

JavaScript libraries and frameworks make it easier to work with machine learning:

TensorFlow.js: Developed by Google, TensorFlow.js is a JavaScript library for building and training machine learning models. It allows you to use pre-trained models, create custom models, and run inference directly in web browsers.

Brain.js: Brain.js is a neural network library for JavaScript. It simplifies the process of creating and training neural networks, making it accessible to developers with varying levels of expertise.

Now, let’s dive into practical examples to demonstrate how you can use client-side JavaScript for introductory machine learning tasks.

Example 1: Linear Regression with TensorFlow.js (Client-Side)

Linear regression is a fundamental machine learning algorithm used for predicting numerical values based on input data. Here’s a simple client-side example using TensorFlow.js:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Linear Regression with TensorFlow.js</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
</head>
<body>
<script>
// Generate sample data
const x = tf.tensor([1, 2, 3, 4]);
const y = tf.tensor([2, 4, 6, 8]);
// Define a linear regression model
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
// Compile the model
model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' });
// Train the model
model.fit(x, y, { epochs: 100 }).then(() => {
/
/ Make predictions
const prediction = model.predict(tf.tensor([5]));
prediction.print();
});
</script>
</body>
</html>

In this example, we create a simple HTML document that includes TensorFlow.js. We generate sample data, create a linear regression model, compile it, train it, and make predictions directly in the browser.

Example 2: Neural Network with Brain.js (Client-Side)

Let’s build a neural network using Brain.js in a client-side JavaScript environment:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Neural Network with Brain.js</title>
<script src="https://cdn.jsdelivr.net/npm/brain.js"></script>
</head>
<body>
<script>
// Create a neural network
const net = new brain.NeuralNetwork();
// Train the network
net.train([
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] }
]);
// Make predictions
const output = net.run([0, 1]);
console.log(output); // Output will be close to 1
</script>
</body>
</html>

In this example, we create another HTML document that includes Brain.js. We define a neural network, train it to perform logical XOR operations, and make predictions directly in the browser.

JavaScript is not just a language for building interactive web applications; it is also a powerful tool for introductory machine learning tasks on the client-side. With libraries like TensorFlow.js and Brain.js, you can leverage the power of machine learning within web browsers. In this article, we explored the basics of getting started with client-side machine learning in JavaScript and provided practical examples of linear regression and neural network implementation. As you continue your journey into the world of machine learning, remember that JavaScript can be a valuable tool in your toolkit, enabling you to develop intelligent web applications and solve complex problems directly in the browser.


Intro ML in JavaScript was originally published in CarlosRojasDev on Medium, where people are continuing the conversation by highlighting and responding to this story.

Scroll to Top