Web developers, rejoice! If you’ve been looking for a way to make a foray into the world of Machine Learning and Deep Learning, your learning curve has gotten that much more gentle with the introduction of the TensorFlow library in JavaScript.
Introduction to Javascript
JavaScript is a high level, interpreted type programming language. It was initially created to make web pages “alive” and is one of the three core technologies, along with HTML as well as CSS of the World Wide Web.
Starting off as a language only implemented client side in web browsers, they are now embedded in many other types of host software and provide functionalities that have moved far beyond its original use as a language that makes web pages more interactive.
While there are many machine learning libraries already available in the JavaScript ecosystem, the introduction of TensorFlow in JS marks a huge step forward for implementing machine learning algorithms directly in the browser.
Introduction to TensorFlow
TensorFlow is an open source software framework that helps implement machine learning algorithms in a variety of environments. Originally developed by researchers and engineers from the Google Brain team within Google’s AI organization, it has found wide usage and is used by companies such as Airbnb, Nvidia, Uber, Kakao to name a few.
Now, with the launch of TensorFlow.js, it has become possible to launch, train, and execute machine intelligence within the browser using just JavaScript and a high-level layers API.
Why use Machine Learning in Javascript?
Conventionally, machine learning is often associated with languages such as R and Python, so why bother implementing it in a language such as JavaScript?
For starters, implementing these algorithms in web browsers allows us to try out machine learning from client-side servers, which means that one can use the platform for rapid prototyping, interactive explanations, as well as visualizations. From a user’s perspective, this means that a person does not need to install any libraries or drivers to implement machine intelligence.
Even the performance of the algorithms is accelerated due to its integration with WebGL, which means that the code is accelerated whenever a GPU is available. This means that the user does not require a CUDA or a cuDNN installations, and can even work on most mobile GPUs(!). However, the performance will not be comparable to that of CUDA/cuDNN.
What can you do with TensorFlow.js?
- You can convert your pre-existing TensorFLow and Keras models to implement them in the browser for inference.
- You can use transfer learning to re-train an existing model by providing it data collected in the software. You can use this method, called Image Retraining, to create new image classifications using the same model as before.
- You can create models directly in the browser.
How can I install TensorFlow.js?
You can either start using TensorFlow.js via the script tags or install it from the NPM.
Via Script Tag
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.9.0"> </script>
<!-- Place your code in the script tag below. You can also use an external .js file -->
<script>
// Notice there is no 'import' statement. 'tf' is available on the index-page
// because of the script tag above.
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
model.predict(tf.tensor2d([5], [1, 1])).print();
});
</script>
</head>
<body>
</body>
</html>
Source: https://js.tensorflow.org/
Via NPM
yarn add @tensorflow/tfjs
npm install @tensorflow/tfjs
Our Thoughts
This is a great opportunity for developers familiar with Javascript to make a foray into the world of data science and machine learning. Making a leap into a different domain is a difficult task, more so for a field that requires as much technical knowledge as machine learning. However, the familiarity of the language can make things easier and that’s what we’re hoping for with the introduction of TensorFlow.js.
To get a better understanding of learning data science from scratch, check out our course details right here.