GeeksforGeeks App
Open App
Browser
Continue

Related Articles

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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

Syntax:

Parameters: This function accepts the following three parameters:

• tensor: It is a Tensor to pad.
• paddings: It is an array of length R, the rank of the given tensor, where each element is of length 2 of ints ([pad_Before, pad_After]), specifies how much padding should be given along each dimension of the tensor.
• constantValue: It is the padding value to be used.  The default value is 0.

Return Value: It returns tf.Tensor object.

Example 1:

Javascript

 // Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs" // Initializing 3d tensor and then using// .pad() function to print the resulttf.tensor3d([1, 2, 3, 4], [2, 2, 1])  .pad([[0, 0], [1, 1], [2, 2]])      .print();

Output:

Tensor
[[[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, 3, 0, 0],
[0, 0, 4, 0, 0],
[0, 0, 0, 0, 0]]]

Example 2:

Javascript

 // Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs" // Initializing 2d tensorlet geek1 = tf.tensor2d([[1, 2], [3, 4]]); // Using .pad() function.let geek2 = geek1.pad([[0, 1], [2, 1]]); // Printing the result.geek2.print();

Output:

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