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

Tensorflow.js tf.batchToSpaceND() Function

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.batchToSpaceND() function is used to restructure the given “batch” from their zero dimension to “M+1” dimensions having shape of “block-Shape + [batch]”, where block-Shape is the parameter and batch is the specified Tensor. Here cropping is done on the in-between result as per the given crop array. 

tf.batchToSpaceND (x, blockShape, crops)

Parameters: This function accepts three parameters which are illustrated below:

  • x: A specified batch Tensor of N-Dimension with having shape of “[batch] + spatialShape + remainingShape”, where spatialShape is of M dimensions.
  • blockShape: A 1-D array must be having shape of [M], for all the values must be greater than or equal to 1.
  • crops: A 2-D array shape of [M, 2] for which all the values must be greater than or equal to 0. Here crops[i] = [cropStart, cropEnd] which defines the portion to crop from input dimension i + 1.

Return Value: It returns a Tensor of the reshaped version of the specified batch.

Example 1: 

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing a 3D Tensor of batch to reshape
const x = tf.tensor3d([5, 10, 15, 20, 25], [5, 1, 1]);
 
// Initializing blockShape and crops parameter
const blockShape = [1, 1];
const crops = [[0, 0], [0, 0]];
 
// Calling the .batchToSpaceND() function over
// the above parameters and Tensor
x.batchToSpaceND(blockShape, crops).print();


Output:

Tensor
    [ [[5 ],],

      [[10],],

      [[15],],

      [[20],],

      [[25],]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing a 4D Tensor of batch to restructure
const x = tf.tensor4d([0, 2, 4, 6, 8, 10], [6, 1, 1, 1]);
 
// Using the blockShape and crops as the parameter
// for the .batchToSpaceND() function
x.batchToSpaceND([3, 2], [[0, 0], [0, 0]]).print();


Output:

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

      [[4 ],
       [6 ]],

      [[8 ],
       [10]]]]

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

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