TensorFlow.js Interview Questions

**TensorFlow.js**

is a JavaScript Library for training and deploying**Node.js**

. Tensorflow lets us add machine learning functions to any Web Application.**Tensorflow.js**

, you need to know the following :**Node.js**

. Also since **Node.js**

is a JS runtime, so having command over JavaScript would help a lot.Other requirements :

There are two main ways to get TensorFlow.js in your browser based projects:

If you are new to web development, or have never heard of tools like webpack or parcel, we recommend you use the script tag approach. If you are more experienced or want to write larger programs it might be worthwhile to explore using build tools.

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

```
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Train the model using the data.
model.fit(xs, ys, {epochs: 10}).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
model.predict(tf.tensor2d([5], [1, 1])).print();
// Open the browser devtools to see the output
});
```

`yarn add @tensorflow/tfjs`

or

`npm install @tensorflow/tfjs`

```
import * as tf from '@tensorflow/tfjs';
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Train the model using the data.
model.fit(xs, ys, {epochs: 10}).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
model.predict(tf.tensor2d([5], [1, 1])).print();
// Open the browser devtools to see the output
});
```

`yarn add @tensorflow/tfjs-node`

or

`npm install @tensorflow/tfjs-node`

`yarn add @tensorflow/tfjs-node-gpu`

or

`npm install @tensorflow/tfjs-node-gpu`

`yarn add @tensorflow/tfjs`

or

`npm install @tensorflow/tfjs`

```
const tf = require('@tensorflow/tfjs');
// Optional Load the binding:
// Use '@tensorflow/tfjs-node-gpu' if running with GPU.
require('@tensorflow/tfjs-node');
// Train a simple model:
const model = tf.sequential();
model.add(tf.layers.dense({units: 100, activation: 'relu', inputShape: [10]}));
model.add(tf.layers.dense({units: 1, activation: 'linear'}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
const xs = tf.randomNormal([100, 10]);
const ys = tf.randomNormal([100, 1]);
model.fit(xs, ys, {
epochs: 100,
callbacks: {
onEpochEnd: (epoch, log) => console.log(`Epoch ${epoch}: loss = ${log.loss}`)
}
});
```

**Source : Tensorflow**

Tensors are similar to arrays in programming languages, but here, they are of higher dimensions. It can be considered as a generalization of matrices that form an n-dimensional array. TensorFlow provides methods that can be used to create tensor functions and compute their derivatives easily. This is what sets tensors apart from the NumPy arrays.

TensorBoard is a **Graphical User Interface (GUI)** that is provided by TensorFlow to help users visualize graphs, plots, and other metrics easily without having to write a lot of code. TensorBoard provides an ample number of advantages in terms of readability, ease of use, and performance metrics.

There are **three types of Tensors** used to create neural network models:

**tf.constant**

.`tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)`

It accepts the five arguments.

Therefore, placeholders do not require any initial value. They only need a datatype (such as float32) and a tensor shape, so the graph still knows what to compute with even though it does not have any stored values.

**tensorflow.js**

, has also been introduced for training and deploying machine learning models. TensorFlow has numerous advantages, and this is why it is the most used framework for Machine Learning in the world. Some of its advantages are given below:

TensorFlow has some limitations, as mentioned here :

It is the central unit in the architecture of TensorFlow Serving, which serves as objects used by the clients in the process of computations. It offers flexible size and granularity. It consists of one lookup table to a tuple of interference models.

It improves the functionality and performance with the help of improvements:

TensorFlow serving allows the system of TensorFlow, which we use for machine learning. It allows us to create, use, and execute the algorithms for the system a user wants to build. It extends its functionality with the help of collaborating environments such as TensorBoard.