Open In App

Tensorflow.js tf.mirrorPad() Function

Last Updated : 21 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads