An Introduction to Machine Learning with JavaScript

What is Machine Learning?

Machine learning is a subset of AI that is concerned with algorithms that can improve through experience. Experience in this case generally translates to being fed lots of data. What’s more interesting than what machine learning is though, is what it can do. Beating humans at board games, driving cars, filtering spam from your email, or picking out food items in an image; the applications for machine learning are seemingly endless. When it comes to web development some of the more common use cases for machine learning are things like categorizing or filtering user input or user submitted content, generating recommendations, or creating interesting and interactive experiences for users like playing a video game with your webcam.

Where does JS fit in the picture?

Up until a few years ago, it didn’t, or at least not well. Python has always been the language of choice for ML. A large reason for this is the existence of libraries like numpy and scikit-learn. They provide rich support for computation and machine learning and bind to lower level implementations, removing the downsides of using an interpreted language like Python for performance dependent code.

Another reason is momentum. Since the most comprehensive, best-maintained machine learning libraries tended to be written in Python, anybody doing machine learning would learn Python, creating a feedback loop. These things are still true, but over the past few years, the situation has gotten much better for JavaScript developers who want to learn machine learning without switching languages.

Perhaps the biggest force in increasing the feasibility of using JS for ML has been the improvements brought by the TensorFlow.js project. Supporting WebGL acceleration, the 1.0.0 version of TensorFlow JS was released in early 2019. Around the same time, support for binding to a C API and leveraging a GPU was added to the node version of the library. These developments, as well as the continued emergence of other libraries for ML with JS, haven’t exactly leveled the playing field with Python, but they have made it much more plausible to seriously engage with ML using JS.

As these libraries and others continue to evolve, and standards like WebGPU continue to move forward, this trend will likely continue. Even if you find you want to build and train a model in Python, it’s possible to convert that to a format that can be used for prediction or even further trained on the web. When no server-side call is required for making a classification, a lot of possibilities open up for using ML to enhance the user experience without increasing server load and without concerns about privacy.

What if I don’t need a neural network?

An artificial neural network, often referred to as just a neural network, is a type of system inspired by the way neurons are connected in the brain, which can be used for a wide variety of machine learning tasks. Due to the success and popularity of deep learning, which uses neural networks, it’s also become synonymous with machine learning in some people’s minds. There are other algorithms and techniques used in machine learning though, and the no free lunch theorem tells us that a neural network is not always the right solution. TensorFlow does provide support for other common machine learning algorithms but their focus is clearly on applications of neural networks. Other libraries like ml.js and machinelearn.js provide implementations of a wide array of ML algorithms in JS, and machinelearn.js supports C++ and GPU binding. However, there is still no one-stop shop for machine learning with JavaScript that parallels the performance and versatility of a library like scikit-learn.

Conclusion

Learning and engaging with machine learning in JS is more practical now than it’s ever been. The resources available for quickly getting an interactive model running, the wide range of libraries available, and the continued investment and open source development in this area mean it’s now reasonable for a JavaScript developer who wants to learn machine learning to do so without having to learn a new language at the same time.

Many practical use cases may be adequately addressed by these tools as well, particularly if you find yourself in need of a neural network and you’re not trying to teach your computer to drive. However, as you start to deal with larger amounts of data or more particular problem spaces, you may still find yourself pushed towards Python and its more mature ecosystem.

For more information, check out a podcast we did with Charlie Gerard on the extent of machine learning training required for a system to get ‘good’ and how to have fun while programming.

SitePen is a full development team that has extensive experience working with enterprise teams. Feel free to contact us if we can help you out with a project or for more information!

Originally published at https://www.sitepen.com on October 19, 2020.