Back in March 2019, Google announced the release of version 1.0 of TensorFlow.js, a JavaScript implementation of their famous machine learning framework TensorFlow. It has taken us a while, but we finally decided to sit down last week and mess around with it.
There are several reasons why we are excited about TensorFlow.js, but the number one reason is that it completely changes how developers are able to deliver artificial intelligence to our customers. Typically, you would need to run your artificial intelligence on a server somewhere and have your website users send requests to the artificial intelligence to process. This can cause a delay as the request is passed to the AI running on the server and then the results are sent back to the user. This can also be very expensive to run, as any number of users could trying to access the AI application at once, causing the program to perform a lot of computation.
However, TensorFlow.js frees us from this traditional architecture, by allowing us to run our machine learning models inside the browser. The user simply downloads the AI from a server and they now have full access to it on the website. No more delays waiting for requests to reach the server, no more heavy computation on the servers.
To test this library out, we built a relatively simple neural network to recognise hand-drawn digits. We did this by training our model on the famous MNIST dataset, which contains 60,000 training images of hand-drawn numbers. The image below contains an example of each of these numbers.
Feel free to play around with your AI bellow, and see if you are able to get the model to classify all 10 numbers correctly.
Draw a number in the middle of the block on the left and click "Classify". The AI will then try to classify your number and display it's result on the right.
Draw a number in the middle of the block on the top and click "Classify". The AI will then try to classify your number and display it's result on the bottom.
Tip: For best results, make sure your number is in the center of the block.
Waiting for classification
Unfortunately, TensorFlow.js is not the ultimate solution which will solve all our problems. It does have a major issue: the AI models it must download can get fairly large. As your AI gets more complex, the size of the model grows exponentially. This is especially an issue if you know that your customers will primarily access your web application through their mobile phones as these models will quickly use up your customer's mobile data, causing a lot of frustration. Therefore, this library is only really useful when the models which you are serving your customers are relatively small.
For those interested, you can find the code we used to train our model with TensorFlow.js over on our Github page.