Open In App

Tensorflow.js Introduction

Improve
Improve
Like Article
Like
Save
Share
Report

TensorFlow.js brings machine learning to the browser and Node.js. It allows developers to build and run ML models directly in JavaScript and access AI for web applications.

What is Tensorflow.js ?

TensorFlow.js is a JavaScript library for training and deploying machine learning models on web applications and in Node.js. You can develop the machine learning models from scratch using tensorflow.js or can use the APIs provided to train your existing models in the browser or on your Node.js server.

To learn more you can directly go to this link: https://www.tensorflow.org/resources/learn-ml/basics-of-tensorflow-for-js-development.

Prerequisite: Before starting Tensorflow.js, you need to know the following:

For browser:

For server-side:

  • Node.js: Having a good command of Node.js. Also since Node.js is a JS runtime, so having command over JavaScript would help a lot.

Other requirements:

  • NPM or Yarn: These are packages that need to be installed in your system.

Setting Up Tensorflow.js:

Browser Setup There are two ways to add TensorFlow.js in your browser-based application:

  • Using script tags.
  • Installation from NPM

1. Using Script tags

Add the following script tag to your main HTML file.

<script src=”https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js”></script>

Example:

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <script src=
"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js">
    </script>
</head>

<body>
    <script>

        // Value of a scalar
        let value = "Hello Geeks! Tensorflow here."

        // Creating the value of a scalar
        let tens = tf.scalar(value)

        document.write(tens)
    </script>
</body>

</html>

 Output:

2. Using NPM/ yarn

We can either use NPM or Yarn to install tensorflow.js.

yarn add @tensorflow/tfjs

or

npm install @tensorflow/tfjs

Node.js Setup:

Option 1: Install TensorFlow.js with native C++ bindings.

yarn add @tensorflow/tfjs-node

or

npm install @tensorflow/tfjs-node

Option 2: (Linux Only) If your system has a NVIDIA® GPU with CUDA support, use the GPU package even for higher performance.

yarn add @tensorflow/tfjs-node-gpu

or

npm install @tensorflow/tfjs-node-gpu

Option 3: Install the pure JavaScript version. This is the slowest option performance-wise.

yarn add @tensorflow/tfjs

or

npm install @tensorflow/tfjs

Last Updated : 19 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads