Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.eye() Function

Tensorflow.js tf.eye() Function

Tensorflow.js is an open-source library for creating machine learning models in Javascript that allows users to run the models directly in the browser.

The tf.eye() is a function defined in the class tf.Tensor. It’s used to create an identity matrix of specified rows and columns.

An identity matrix of shape [m, n] consists of value one at all diagonal elements and zero at remaining places.

Syntax:

tf.eye(numRows, numColumns, batchShape, dtype)

Parameters:

  • numRows: Number of rows of returning identity matrix.
  • numColumns: Number of columns of returning identity matrix. It’s an optional parameter. If it was not specified then the shape of the returning identity matrix is [numRows, numRows].
  • batchShape: It’s an array that defines the shape that’ll be appended to the beginning of the shape of the returned identity matrix by replicating the identity matrix. It’s an optional parameter. Suppose batchShape = [a, b] then the returning identity matrix shape is [a, b, numRows, numColumns]
  • dtype : Specified as the datatype of elements in returning identity matrix. It’s optional.

Return value : It returns a tensor of identity matrix.

Example 1: Creating an identity matrix of square shape

  • Create an identity matrix of shape [3, 3] by defining numRows = 3 in the tf.eye() method.

Javascript




// Dynamic loading the "@tensorflow/tfjs" module
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
 
// Creating an identity matrix  of shape [3,3]
var matrix = tf.eye(numRows = 3)
   
// Printing the identity matrix
matrix.print()


Output :

Tensor
    [[1, 0, 0],
     [0, 1, 0],
     [0, 0, 1]]

Example 2: Creating an identity matrix of rectangular shape

  • Create an identity matrix of shape [3, 4] by defining numRows = 3 and numColumns = 4 in the tf.eye() method.

Javascript




// Dynamic loading the "@tensorflow/tfjs" module
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
 
// Creating an identity matrix  of shape [3,4]
var matrix = tf.eye(numRows = 3, numColumns = 4)
   
// Printing the identity matrix
matrix.print()


Output :

Tensor
    [[1, 0, 0, 0],
     [0, 1, 0, 0],
     [0, 0, 1, 0]]

Example 3: Creating an identity matrix by specifying batchShape

  • Create an identity matrix of shape [2, 3] by defining numRows = 2 and numColumns = 3 in the tf.eye() method.
  • Replicate the above identity matrix 3 times by including batchShape = [3]
  • Then the shape of returning identity matrix is [3, 2, 3]

Javascript




// Dynamic loading the "@tensorflow/tfjs" module
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
 
// Creating an identity matrix  of shape [2,3]
// And include batchShape = [3] to replicate identity matrix 3 times
// Shape of returning tensor is [3, 2, 3]
var matrix = tf.eye(numRows = 2, numColumns = 3, batchShape = [3])
   
// Printing the identity matrix
matrix.print()


Output :

Tensor
    [[[1, 0, 0],
      [0, 1, 0]],

     [[1, 0, 0],
      [0, 1, 0]],

     [[1, 0, 0],
      [0, 1, 0]]]

Reference:https://js.tensorflow.org/api/latest/#eye

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments