Thursday, July 4, 2024
HomeLanguagesJavascriptTensorflow.js tf.mirrorPad() Function

Tensorflow.js tf.mirrorPad() Function

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .mirrorPad() function is used to pad the stated tensor input with the help of mirror padding. Moreover, this method is beneficial in implementing the REFLECT as well as SYMMETRIC modes of pad.

Syntax :  

tf.mirrorPad(x, paddings, mode)

Parameters:  

  • x: It is the stated tensor which is to be padded, and it can be of type tf.Tensor, TypedArray, or Array.
  • paddings: It is an array whose length is R, that is the order of the stated tensor. Where, all the elements form a tuple of length two i.e. ints [padBefore, padAfter] that specifies to what extent it is to be padded with every size of the stated tensor. Moreover, in the reflect mode of padding, the padded sections should exclude the extremities, whereas in the symmetric mode of padding the padded sections should not exclude the extremities. And it is of type array.
  • mode: It specifies the mode of padding i.e. either reflect or symmetric. And it is of type string.

  
 

Note:

 

  • Firstly, if the stated tensor input is [4, 5, 6] and paddings is [0, 1], then the output will be [4, 5, 6, 5] in the reflect mode of padding and [4, 5, 6, 6] in the symmetric mode of padding.
  • Secondly, if the mode of padding is reflect then the paddings[D, 0] as well as paddings[D, 1] must not be higher than x.shape[D] – 1, whereas if the mode of padding is symmetric then the paddings[D, 0] as well as paddings[D, 1] must not be higher than x.shape[D].

 

Return Value: It returns tf.Tensor object.

 

Example 1:

 

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Defining tensor input
const y = tf.tensor1d([4, 5, 6]);
 
// Defining paddings and mode of
// padding
const padding = [[0, 1]];
const mode = 'reflect';
 
// Calling tf.mirrorPad() method
var res = tf.mirrorPad(y, padding, mode);
 
// Printing output
res.print();


 

 

Output:

 

Tensor
    [4, 5, 6, 5] 

 

Example 2:

 

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling tf.mirrorPad() method and
// Printing output
tf.mirrorPad(tf.tensor(
    [2.4, 6.8, 9.3, 5.3]),
    [[0, 2]], 'symmetric').print();


 

 

Output:

 

Tensor
    [2.4000001, 6.8000002, 9.3000002, 
    5.3000002, 5.3000002, 9.3000002] 

 

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

 

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!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments