The silence of the FANS - part #1 - Google Colab Javascript QuickStart
Tags: tensorflow, machinelearning, javascript
Published: 2020-02-17 18:12:49

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7kj9t4rvhagz23d2lpog.gif)

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](https://dev-to-uploads.s3.amazonaws.com/i/g4tzivokqm1945f7xaz9.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/aa83k1g9ypg44moso6o4.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/vjoocp20y8q6smzo5h3w.png)


Let's fix this by running the first cell

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

You should see this at the bottom of cell output
```bash
...
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
```

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
```
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
```

##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)

```javascript
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());
};
```

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

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

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

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

You can use it to run async code
```javascript
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());
})
```

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:

- Javascript is most used language in the world. According to Github and StackOverflow
- TensorflowJS is on par and some times even faster then Tensorflow (python) version 
    - https://blog.tensorflow.org/2020/01/run-tensorflow-savedmodel-in-nodejs-directly-without-conversion.html?m=1
{% twitter 1228077414560878597 %}

- With JavaScript I can get clean 20-30 fps for Face detection and share it with everyone in the world without buying $$$ hardware
    - https://twitter.com/TensorFlow/status/1214290578374041600?s=20
- We have awesome Russian and English speaking TensroflowJS communities here in Telegram chat-app with 400 people and growing
    - https://t.me/tensorflow_js
    - https://t.me/tensorflowjs_en
- Javascript is awesome!

##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](https://dev-to-uploads.s3.amazonaws.com/i/52u4wldqtrvxe8bbk6or.png)

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