Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.layers.zeroPadding2d() Function

  • Last Updated : 19 Oct, 2021

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

The tf.layers.zeroPadding2d( ) function is used for adding rows and columns for zeros at the top, bottom, left, and right side of and image tensor.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax: 

tf.layers.zeroPadding2d(args);

 Parameters: This method accepts the args as a parameter which has the following properties:



  • Padding: This variable accepts an integer, or an Array of 2 integers, or Array of 2 Arrays, each of which is an array of 2 integers. Interpretation of this variable is:
    • If a variable is an integer, the same symmetric is applied to width and height.
    • If a variable is an array of 2 integers, interpreted as two different symmetric values of height and width.
    • If a variable is an array of 2 arrays, interpreted as topPad, bottomPad for the first array, and leftPad, rightPad for the second array.
  • dataFormat: This variable define the format of the shape of the input Tensor. If can be  channelsLast or  channelsFirst. This value defines the ordering of the dimensions in the inputs. The channelsLast define shape [ batch, height, width, channels] while channelsFirst define [ batch, channels, height, width].
  • inputShape: It can be null or an array of numbers. It is used to create an input layer to insert before the following layer. This variable is only used for the first layer of a model.
  • batchInputShape: This variable accept null or array of number. This variable works the same as inputShape but If both inputShape and batchInputShape are defined, batchInputShape is used. This variable is only used for the first layer of a model.
  • batchSize: It is a number, and It helps batchInpuShape variable. if batchInputShape is not defined it is used for creating batchInputShape.
  • dtype: This variable is used for defining data type for the padding layer. It is Default to ‘float32’. This variable is only used for first layer of a model.
  • name: It defined the name of layer.
  • trainable: It is a boolean type. It defined layer data are updatable by fit or not. Defaults value is true.
  • weights: It is tf.Tensor type. It declares the Initial weight values of the layer.
  • inputDType: It defines the data type of the input layer. Legacy support. Do not use for new code.

Returns: It returns ZeroPadding2D object.

Example 1: In this example, we add a zero-padding layer with default values.

Javascript




// Importing tensorflow
const tf = require("@tensorflow/tfjs")
 
// Input 4d Tensor    
const img4d = tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]);
 
// Adding padding in Tensor
const pad = tf.layers.zeroPadding2d();
const imgpad = pad.apply(img4d);
 
// Printting 4d Tensor with padding</div>
imgpad.print()

 
 

Output:

 

Tensor    [[[[0], [0], [0], [0]],
            [[0], [1], [2], [0]],
            [[0], [3], [4], [0]],
            [[0], [0], [0], [0]]]]

Example 2: In this example, we add zero paddings in tensor of specific data type and with define data-Format.

 

Javascript




// Importing tensorflow
const tf = require("@tensorflow/tfjs")
 
// Input 4d Tensor    
const img4d = tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]);
 
// Adding padding in Tensor
const pad = tf.layers.zeroPadding2d({
    padding: 2,
    dataFormat: 'channelsFirst', dtype: 'int32'
});
 
const imgpad = pad.apply(img4d);
 
// Printting 4d Tensor with padding
imgpad.print()

 
 

Output: 

 

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

      [[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]]]]

 Reference: https://js.tensorflow.org/api/latest/#layers.zeroPadding2d

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!