Google News
logo
TensorFlow.js Interview Questions
TensorFlow.js is a JavaScript Library for training and deploying "machine learning" models in the browser and in Node.js. Tensorflow lets us add machine learning functions to any Web Application.
 
Developed in 2015 by the Google Brain Team, it ensures to provide an easy-to-use low-level toolkit that can handle complex mathematical operations and learning architectures.
 
TensorFlow can be used in a wide variety of programming languages, most notably Python, as well as Javascript, C++, and Java. This flexibility lends itself to a range of applications in many different sectors.
Prerequisite : Before starting Tensorflow.js, you need to know the following :
 
For browser :
 
* HTML : Basics knowledge of HTML is required
* JavaScript : Good knowledge of JS is required
 
 
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.
There are two main ways to get TensorFlow.js in your browser based projects:
 
* Using script tags.
 
* Installation from NPM and using a build tool like Parcel, WebPack, or Rollup.
 
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.
 
Usage via Script Tag : 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>
See code sample for script tag setup
// 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
});
  
Installation from NPM : You can use either the npm cli tool or yarn to install TensorFlow.js.
yarn add @tensorflow/tfjs
or
npm install @tensorflow/tfjs
See sample code for installation via NPM
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
});
  
Node.js Setup : You can use either the npm cli tool or yarn to install TensorFlow.js.
 
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
See sample code for Node.js usage
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:
 
Constant Tensor : Constant Tensors are used as constants, as the name suggests. They create a node that takes a value and does not change it. A constant can be created using tf.constant.
tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
It accepts the five arguments.

Variable Tensor : Variable Tensors are the nodes which provide their current value as output. It means that they can retain their value over multiple executions of a graph.

Place Holder Tensor : Placeholders Tensors are essential than variables. These are used to assign data in a later time. Placeholders are the nodes whose value is fed at the time of execution. Assume, we have inputs to our network which are dependent on some external data. Also, we do not want our graph to depend on any real value while developing the graph, then Placeholders are useful datatype. We can even build a graph without any data.

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 has APIs support for a wide variety of languages such as Matlab and C++. Researchers are continuously trying to making it better. A javascript library, 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:
 
* Platform independency
* Auto-differentiation capability
* Open-source and large community
* Support for asynchronous computations
* Usage of GPU for distributed computing
* Highly customizable based on requirements
TensorFlow has some limitations, as mentioned here :
 
* It does not provide support for OpenCL (Open Computing Language).
* It requires prior knowledge of advanced calculus and linear algebra along with a pretty good understanding of Machine learning.
* It has GPU memory conflicts with Theano if imported in the same scope.
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.
 
Typical servables include the following :

* a TensorFlow SavedModelBundle (tensorflow::Session)
* a lookup table for embedding or vocabulary lookups
 
It improves the functionality and performance with the help of improvements:
 
* streaming results
* experimental APIs
* asynchronous modes of operation

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.