The silence of the FANS - part #1 - Google Colab Javascript QuickStart

obenjiro

Alexey Okhrimenko

Posted on February 17, 2020

The silence of the FANS - part #1 - Google Colab Javascript QuickStart

Alt Text

Google have awesome service called Google Colab. It is a free (with some limitations, so you can't use it for production) cloud service, based on Jupyter Notebook. It provides a service for machine learning and free-of-charge access to a blazingly fast GPU and TPU.

Alt Text

With this service, you can easily train your model within seconds. It dose supports Python2 and Python3 out of the box, so everything should be good right?

No! I want JavaScript

But there is one problem. There is no native support in Google Colab for javascript. Thankfully I was able to hack it. Thanks to the research done in this thread (https://github.com/googlecolab/colabtools/issues/13) and some mad javascript science we can actually use Google Colab, TensorflowJs and JavaScript together

QuickStart

I have created a template that will help you to start, just open this link

https://tinyurl.com/tf-js-colab

And clone it to you Google Drive with "Save a copy in Drive..." command
Alt Text

Now you have your own copy of this Jupyter Notebook on your Google Drive. Execute first cell and hit F5. And thats it! πŸŽ‰πŸŽ‰πŸŽ‰

Troubleshooting

You will probably see this message "Unrecognized runtime javascript; defaulting to python". Don't panic! This is happening because our template uses "javascript" kernel for Jupyter notebook, but since you just created this notebook your instance (your computer/server) that is assigned to you simple doesn't have it installed.

Alt Text

Let's fix this by running the first cell



!npm install -g npm@latest
!npm cache verify
!npm install -g --unsafe-perm ijavascript
!ijsinstall --install=global
!jupyter-kernelspec list


Enter fullscreen mode Exit fullscreen mode

You should see this at the bottom of cell output



...
added 64 packages from 62 contributors in 3.604s
Available kernels:
  ir            /usr/local/share/jupyter/kernels/ir
  javascript    /usr/local/share/jupyter/kernels/javascript
  python2       /usr/local/share/jupyter/kernels/python2
  python3       /usr/local/share/jupyter/kernels/python3
  swift         /usr/local/share/jupyter/kernels/swift


Enter fullscreen mode Exit fullscreen mode

Now just hit F5. And thats it! πŸŽ‰πŸŽ‰πŸŽ‰

Your runtime will be reloaded and your Google Colab instance will now be running under ijavascript kernel (you can read more about it here https://github.com/n-riesco/ijavascript). Now you can write javascript for Node.js version 8 and use power of Google Colab.

If you don't see



  javascript    /usr/local/share/jupyter/kernels/javascript


Enter fullscreen mode Exit fullscreen mode

Check formatting of the code. For some reason Google Colab tends to add/remove spaces and this brakes everything.

And if you have problems with NPM request limit "npm ERR! 429 Too Many Requests", just add Chinese NPM server to every npm install command



!npm install -g --unsafe-perm ijavascript --registry=https://registry.npm.taobao.org


Enter fullscreen mode Exit fullscreen mode

Known problems

But before you go and experiment. There is two more things I need to warn you about:

1) ijavascript kernel doesn't have clean and simple way to execute shell commands, so I made a helper function for you (you can find it in template)



var { spawn } = require('child_process');
var sh = (cmd) => {
    $$.async();
    var sp = spawn(cmd, { cwd: process.cwd(), stdio: 'pipe', shell: true, encoding: 'utf-8' });
    sp.stdout.on('data', data => console.log(data.toString()));
    sp.stderr.on('data', data => console.error(data.toString()));
    sp.on('close', () => $$.done());
};


Enter fullscreen mode Exit fullscreen mode

With it you can init package.json, install dependencies and run some commands.



sh('npm init -y');
sh('node -v; npm -v');
sh('npm install @tensorflow/tfjs-node-gpu');


Enter fullscreen mode Exit fullscreen mode

2) Do not use let, const. Use oldschool var instead. This is really important because of Jupyter Notebook workflow suggest that you can run code in any order you want, and run multiple times. You won't be able to do that with let or const

3) To run asynchronous code you should use $$.async(), $$.done() helpers. Otherwise you will lose cell output. To make this process easy I made another helper function



var run_async = async (pf) => {
  $$.async();
  await pf();
  $$.done();
};


Enter fullscreen mode Exit fullscreen mode

You can use it to run async code



var data;
// may take a lot of time to download files for the first time
run_async(async function () {
  data = new MnistDataset();
  await data.loadData();  // async code that fetches MNINST dataset

  console.log(data.getTrainData());
})


Enter fullscreen mode Exit fullscreen mode

4) After some time your instance (computer/server) will be destroyed and when you attempt to execute any javascript you will see parse error messages. To fix them just execute first cell ones more and hit F5.

Why javascript

Well there is a some of reasons why I prefer JavaScript (TensorflowJS) for Machine Learning:

So what's the benefit?

Why should we suffer, use all this helpers. Why not use your MacBook? The reason is that even for small dataset like MNIST and simple CNN model you can get 3-7x performance improvements. Nice! :)

Alt Text

And with this, finally, you can achieve Silence of the FANS...

PS: On the next article I will tell you how to download and upload data to Google Colab

πŸ’– πŸ’ͺ πŸ™… 🚩
obenjiro
Alexey Okhrimenko

Posted on February 17, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related