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.
Installing TenserFlow.js: There are following two methods by which we can install and use TenserFlow.js.
Method 1: We can use the CDN link to run the Tensorflow.js code without installing it. Add the following CDN link into the script tag to the head section of the HTML file.
<script src=”https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js”></script>
Method 2: We can install it using npm. Before installing TensorFlow.js through npm, first, make sure you have installed Node.js and npm.
npm install @tensorflow/tfjs
Tensorflow.js tf.Functional class: The tf.Function class in TensorFlow.js is a way to wrap a function that constructs a TensorFlow computation, making it easier to serialize and execute the computation. A TensorFlow computation is a series of operations that take input Tensor objects and produce output Tensor objects. For example, you might want to define a computation that adds two numbers, multiplies the result by a third number, and then applies the ReLU activation function. Normally, you would define such a computation by creating TensorFlow operations and executing them in a TensorFlow session. However, this can be cumbersome, especially if you want to serialize the computation and execute it in a different environment. The tf.Function class provides a way to wrap the computation in a function, making it easier to serialize and execute. To create a tf.Function, you define a function that takes input Tensor objects and returns output Tensor objects. You then pass this function to the tf.Function constructor.
Example 1: The TensorFlow.js library to define a simple function addAndRelu which takes in two tensors as inputs, adds them together, and applies the ReLU activation function. The function is then wrapped in a TensorFlow.js tf.function for better performance. Then two tensors are created with values 1 and 2 and passed as inputs to the function addAndRelu and the resulting tensor is stored in variable c. The dataSync method is then called on c to retrieve the value as a JavaScript number and it is logged in the console.
Javascript
import * as tf from '@tensorflow/tfjs' ; // Define a simple function that adds two numbers and //applies the ReLU activation function. function addAndRelu(a, b) { const c = a.add(b); return c.relu(); } // Wrap the function in a tf.Function. const func = tf. function (addAndRelu); // Define the input tensors. const a = tf.tensor(1); const b = tf.tensor(2); // Execute the function and get the result. const c = func(a, b); // Print the result. console.log(c.dataSync()); |
Output:
[3]
Example 2: The TensorFlow.js library to define a function sumOfSquares which takes in an array of numbers as input and it returns the sum of the squares of the numbers. The function uses a for loop to iterate through the elements of the array and keeps adding the square of each element to a variable sum. Then the function is wrapped in a TensorFlow.js tf.function for better performance. Then a tensor is created with an array of numbers [1, 2, 3] and passed as an input to the function sumOfSquares and the resulting tensor is stored in variable y. The dataSync method is then called on y to retrieve the value as a JavaScript number and it is logged in the console.
Javascript
import * as tf from '@tensorflow/tfjs' ; // Define a function that takes an array of numbers and //returns the sum of the squares of the numbers. function sumOfSquares(x) { let sum = 0; for (let i = 0; i < x.length; i++) { sum += x[i] * x[i]; } return sum; } // Wrap the function in a tf.Function. const func = tf. function (sumOfSquares); // Define the input tensor. const x = tf.tensor([1, 2, 3]); // Execute the function and get the result. const y = func(x); // Print the result. console.log(y.dataSync()); |
Output:
[14]
Example 3: The TensorFlow.js library to define a function rowSums which takes in a 2D tensor as input and returns the sum of each rows of the tensor. The function uses the sum method of tensors, with the axis=1 parameter which means the sum will be taken row-wise. Then the function is wrapped in a TensorFlow.js tf.function for better performance.
Then a tensor is created with a 2D array [[1, 2, 3], [4, 5, 6]] and passed as an input to the function rowSums and the resulting tensor is stored in variable y. The dataSync method is then called on y to retrieve the value as a JavaScript array and it is logged in the console. So it will output the array with two elements [6,15] which are the sum of rows in the original tensor.
Javascript
import * as tf from '@tensorflow/tfjs' ; // Define a function that takes a 2D Tensor //and returns the sum of the rows. function rowSums(x) { return x.sum(axis = 1); } // Wrap the function in a tf.Function. const func = tf. function (rowSums); // Define the input tensor. const x = tf.tensor([[1, 2, 3], [4, 5, 6]]); // Execute the function and get the result. const y = func(x); // Print the result. console.log(y.dataSync()); |
Output:
[6, 15]
Reference: https://js.tensorflow.org/api/latest/