Write and deploy machine learning models easily in Nodejs using Tensorflow.js.
This talk has been presented at Node Congress 2021, check out the latest edition of this JavaScript Conference.
Write and deploy machine learning models easily in Nodejs using Tensorflow.js.
This talk has been presented at Node Congress 2021, check out the latest edition of this JavaScript Conference.
TensorFlow.js is an open-source library that allows for easy integration of machine learning models directly in JavaScript. It enables the use of pre-trained models or the development of new models from scratch, without the need to learn another programming language like Python.
TensorFlow.js is versatile and can be run on various platforms such as browsers, mobile devices, and IoT devices like Raspberry Pi. This allows for the deployment of machine learning applications on these platforms without the need for plugins.
Tensor/Flow.js architecture includes three primary APIs: pre-trained models that are ready-to-use JavaScript classes, the Layers API for building and training models using high-level blocks similar to Keras, and the Ops or Core API for fine-tuned control of model architecture and mathematical operations.
Using TensorFlow.js on Node.js allows for the handling of larger models that require more computational power, leverages the performance boost from Node.js's just-in-time compiler, and integrates seamlessly with the extensive NPM ecosystem, enhancing overall performance and usability.
The TensorFlow.js GPU package accelerates tensor operations on the GPU using CUDA, significantly boosting performance compared to CPU-based executions. This is crucial for running large-scale models or models requiring intensive computational resources.
For Node.js, TensorFlow.js offers three main packages: TensorFlow CPU for CPU-accelerated operations, TensorFlow GPU for GPU-accelerated operations, and a vanilla package that runs operations in vanilla JavaScript on the CPU, suitable for environments without TensorFlow binaries.
TensorFlow.js can provide better performance for certain models, such as the BERT language model, where it has shown up to two times better performance on Node.js compared to Python-based implementations. This is due to optimized JavaScript execution and hardware acceleration.
When using Node.js bindings for TensorFlow.js in production, it's important to implement them synchronously to avoid blocking the main thread. Setting up a job queue or using worker threads can help manage operations efficiently in server environments.
Hello, everyone. The topic of my lightning talk is machine learning in Node.js using TensorFlow.js. TensorFlow.js is an open-source library that enables easy integration of machine learning models directly in JavaScript. It allows easy access to machine learning applications on different platforms without any need for plugins. The architecture of TensorFlow.js includes pre-trained models, a layers API for building and training models, and an ops or core API for fine-tuned control. Utilizing Node.js to run TensorFlow.js provides the ability to write machine learning models directly in JavaScript, run larger models, utilize the NPM ecosystem, and achieve better performance.
Hello, everyone. The topic of my lightning talk is machine learning in Node.js using TensorFlow.js.
Hi, I'm Shivailamba. I'm currently a TensorFlow.js SIG member and also a Google Codename Mentor at TensorFlow.
So, the first question that comes to everyone's mind is what exactly is TensorFlow.js? So, TensorFlow.js is an open-source library that enables easy integration of machine learning models directly in JavaScript. Hence, it reduces the need to learn a separate language like Python to host machine learning models. And it allows very easy integration of some kind of pre-trained models or writing your own models from scratch directly in JavaScript.
And as we know that JavaScript is a really versatile language, it can be run across different platforms like on the browser, on the mobile, or let's say on even IoT devices running Raspberry Pi. So, all of these different platforms now get easy access to machine learning based applications without any need of plugins, right? And machine learning that can be used in TensorFlow.js allows not just preexisting models, but it can also use a transfer learning to retrain some of the existing models based on your own data set. And you can actually also write machine learning models directly in JavaScript using JavaScript and without the need of any other language like Python.
Now this is the architecture of TensorFlow.js. There are three different APIs that are provided. So the top layer that you see are the TensorFlow.js pre trained models that are completely ready to use JavaScript classes. The next one is the layers API that enables to easily build and train models using high level building blocks similarly to how Keras has been built on top of TensorFlow. And finally we have the ops or the core API that helps to give you fine tuned control of model architecture or let's say for doing mathematical calculations like linear algebra. And this can be run either on the client side and on the server side. So on the client side we have basically the browser or let's say the mobile based applications that use the CPU or let's say the web accelerated graphics that is web GL or WebAssembly. And on the server side there is support for TensorFlow CPU that are run via the C bindings and we also have the support for TensorFlow GPU that help get acceleration with the help of CUDA that is also being run on Python based machine learning models.
And finally, you know, what are some of the main advantages of utilizing node.js to actually run TensorFlow.js. So the first one is, you know, we can write the machine learning models directly from scratch in JavaScript and we are only coding in one language. There's no need to use any other language to run and train and also test out these machine learning models. Now by using the TensorFlow.js on node.js we are also giving the ability to run much larger models that require a lot more power. For example, let's say if you're running some kind of neural networks that are having a lot of epochs and will require graphics based acceleration. So, with the help of the server hardware, like in Python, we can utilize the power of these servers to accelerate our larger models that cannot be done in the client side. And also we have the support for the NPM ecosystem because the NPM ecosystem is such a large like the node modules are so heavily used and are there. So, a wide variety of node models can be used directly with your TensorFlow.js based code as well to help assist with your TensorFlow.js application. And most importantly, we get much better performance because Node.js uses the just-in-time compiler and the models that are written in TensorFlow.js utilizing the Node.js can also get this performance boost. And that is quite evident with a few examples. For example, this chart shows you the comparison of TensorFlow running on Python-based models and also on the TensorFlow.js for a mobile net, which is a convolutional neural network model. And as you can see that the comparison for the time that it actually takes for the model to actually run is not a lot different.
TensorFlow.js provides better performance than Python versions in certain cases, such as BERT classification. TensorFlow.js in Node.js can be utilized through three different packages: TensorFlow CPU, TensorFlow GPU, and vanilla package. The CPU package accelerates mathematical computations, while the GPU package runs tensor operations on the GPU for even better performance. The vanilla package, which does not rely on TensorFlow, can be used on other devices that support Node.js. Node.js bindings for TensorFlow.js should be set up to avoid blocking the main thread. APIs, such as dfNode and TensorBoard, are available once the package is imported. Feel free to connect with me on social platforms for any queries regarding TensorFlow.js.
So that means that TensorFlow.js itself is quite optimized for running very industrial standard models as well, and also for newer models at the same time. And in some cases, the TensorFlow.js model actually provides a lot more better performance as compared to the Python versions. For example, BERT, which is a state-of-the-art language model for natural language processing, we can see that over here, there is actually a two times better performance boost by actually using Node.js as compared to a Python-based model that is running this BERT classification.
Now, coming on to the most important part, that is, you know, how can we start utilizing the TensorFlow.js in Node.js. So we get namely three different packages that you can install, like the npm packages. So the first one is the TensorFlow CPU. The TensorFlow CPU is, you know, whenever we are importing this package, the module that we get is basically accelerated by the TensorFlow C binary and it runs on the CPU. And the TensorFlow on the CPU uses the hardware acceleration to, let's say, accelerate any kind of mathematical computations, for example, linear algebra.
Now, the extension to that is the TensorFlow GPU package. Like the CPU package, this particular GPU package will be accelerated via the TensorFlow C binary. But it also runs the tensor operations on the GPU with the help of CUDA, that heavily accelerates your performance. And this binding is definitely the fastest as compared to any other package that we have. And now finally, we also have the vanilla package. This package is very similar to the one that we run on the browser. And in this package, the operations are run in vanilla JavaScript and on the CPU. Now, as compared to the other modules, that is the TensorFlow CPU and the TensorFlow GPU, this does not have support for the TensorFlow binary, which actually makes the overall package very smaller. And because of this, since it does not rely on TensorFlow, it can also be actually used on a lot of other devices that support Node.js.
Now, one of the important considerations that need to be made is that the Node.js bindings run on the backend for TensorFlow.js that implements them synchronously. That means that whenever we are running the Node.js bindings on a production application like a web server, we should actually set up a drop queue or a server or like worker threads so that it does not block your main thread. Now, there are also support for APIs because once we have imported the package, all of the normal TensorFlow.js symbols that we use can be appear once we have imported the specific module. Like, for example, one of them is like the dfNode that contains Node.js specific APIs and TensorBoard is actually a notable example of the Node.js specific APIs.
Now, this is a sample code that should give you an example where we have defined like a model and we are training it by utilizing the tf.node.tensorboard. That helps in your model training. With that, that finishes off my presentation and I hope that you have liked it and you can connect with me on these social platforms and to ask any questions regarding TensorFlow.js. Again, do follow any kind of TensorFlow.js models using the hashtag madewithTensorFlow.js on Twitter and on LinkedIn. And again, feel free to connect with me for any queries regarding TensorFlow.js. I hope you have liked it.
We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career
Comments