October 18, 2024

Machine learning – Javascript support for Jupyter

Javascript coding in Jupyter Notebook is possible with Deno runtime! Let’s dive together into the mechanics and capabilities of Deno in Jupyter. We’re going to use Tensorflow JS to train a machine learning model.

In my last ML article, we learned how to install Miniconda and create a new Jupyter Notebook. We then added Tensorflow support and ran a sample in Python.

The code for this article can be found in this Github notebook.

Install Deno

We will need to install Deno first. For this to be correct, make sure you are in the right environment in Conda:

$ conda activate afiether

❗ I used above the “afiether” environment which has been created with Python 3.10. It may work with Python 3.12 as well, but I had some issues, so I downgraded. Let me know if it works for you with 3.12!

Next, make sure you follow the instructions from Deno’s documentation. For me, on Linux, I just ran this command:

$ curl -fsSL https://deno.land/install.sh | sh

If everything goes well, deno command should be available in the PATH. In order to complete the Jupyter installation, just hit this lovely command:

$ deno jupyter --unstable --install

Cool, it should take just a couple of seconds for this command to finish! You should now be able to create a new notebook with the Deno kernel in Jupyter:

Showcasing the Deno’s capabilities

You can create either a new notebook to follow through or check out the “1_Setup Javascript with Deno” notebook in the Github repository from above.

Simple Javascript commands

Deno has support for both Javascript and Typescript. These 2 can be used interchangeably as we will see if we work enough with it. Let’s try to run 2 console output commands with a simple string and one interpolated. The result should look like this:

Stay in the loop in order to discover even more capabilities.

ℹ️ Bear in mind that Deno uses prefixes for imports to determine their location. You can read more here.

NodeJS native modules

Out of the box, Deno has the capability to work with native NodeJS modules. These are specified with the “node:” prefix, like in the example below:

NPM packages

Every project today uses packages from the NPM store to any extent. Deno made a good job to include support for these NPM with the “npm:” prefix, as we can see below:

We can observe that we imported the nodejs-polars package which provides support for data frames in a very similar fashion that Python provides with the pandas package.

Display markdown code

Another useful capability of Deno is to display markdown code by leveraging the native display function which has to be imported separately:

Download a TSV file and initialize a Dataframe

In our machine learning projects, we will need to use data in order to analyze and train the model. Deno includes the awaitable fetch function which can be used to get files from the internet in a very simple fashion.

With this under our sleeve, creating a Dataframe is easy-breezy. Furthermore, we can manipulate data with group by statements, aggregations and sorts. It’s incredibly how easy it is with the polars library!

Plot a magnificent chart

Wow, the capabilities are great so far, so what can we further achieve in this setup? The next thing is the plotting integration with the Plot native Deno library. Let’s check it out by plotting a chart with the continent population sorted in descending order.

The cherry on top: Tensorflow JS

So far so good, we are on the roll! In theory, importing the Tensorflow’s Javascript library is super easy by running this instruction:

✅ But there were some complications along the way which I explained and fixed in the video tutorial at this stage.

Moving on from this, we now use Tensorflow to train a model which will predict the kilowatts value for a specific Horse power input. The formula is KW = hp(I) × 0.7457.

We’ll use 5 HP to KW values in order to train the model and a validation split of 30% in 3250 epochs. Training the model takes around 5-10 seconds.

Finally, we can check how our model performs by predicting 6 values for a vector. These values should not be in the training set in any situation if you want to check the model’s preciseness. And you can also note that the model is not aware of the formula in any way! This process is also called inference in machine learning.

After checking the final results, I was impressed with how accurate these values were predicted by the model, having in mind that our training data set consisted of only 5 elements 💯. The predicted value are extremely close to the truth, so well done Tensorflow!

Conclusion

As you can notice, working with Javascript Notebooks is definitely fun and adventurous as well. You should encounter no problems importing the majority of NodeJS’s modules, however, the ones requiring native bindings can have issues so be cautious.

Also, I did mention in the video that working with Canvas is still problematic and I didn’t manage to solve the issue I had. I guess this is an optional thing to do because you can use the Plot library if you just need charts.

⚠️ One thing that I did notice during my research was that the same code for training our HP to KW model in Python took much more time than the Javascript version. So another reason to use Javascript with Jupyter and Tensorflow!

Other than that, I think having Javascript Notebooks is super powerful and great for people familiar with Javascript. I will definitely use it for the tutorials I have in plan for the future, so let’s enjoy it at full potential 😊.

Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. If you found my content useful please consider a small donation. Any support is greatly appreciated! Cheers  😉

afivan

Enthusiast adventurer, software developer with a high sense of creativity, discipline and achievement. I like to travel, I like music and outdoor sports. Because I have a broken ligament, I prefer safer activities like running or biking. In a couple of years, my ambition is to become a good technical lead with entrepreneurial mindset. From a personal point of view, I’d like to establish my own family, so I’ll have lots of things to do, there’s never time to get bored 😂

View all posts by afivan →