Tensorflow.js tf.spaceToBatchND() 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.spaceToBatchND() function is used to split the spatial dimensions of the specified input space into a matrix of blocks having shape of blockShape, where blockshape is the parameter. Here the spatial dimensions are padded according to the specified padding array.
Syntax:
tf.spaceToBatchND (x, blockShape, paddings)
Parameters: This function accepts three parameters which are illustrated below:
- x: A specified Tensor of N-Dimension with shape of “[batch] + spatialShape + remainingShape”, where spatialShape is having M dimensions.
- blockShape: It is 1-D array which must be having shape of [M], for all the values must be greater than or equal to 1.
- paddings: A 2-D array having shape of [M, 2], for all the values must be greater than or equal to 0. Here padding is equal to [padStart, padEnd].
Return Value: It returns a Tensor of the splitted version of the specified input space.
Example 1:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs"// Initializing a Tensorconst x = tf.tensor4d( [5, 10, 15, 20, 25, 30], [1, 3, 2, 1]);// Initializing blockShape and paddings parametersconst blockShape = [1, 1];const paddings = [[0, 0], [0, 0]];// Calling the .spaceToBatchND() function over// the above parameters and Tensorx.spaceToBatchND(blockShape, paddings).print(); |
Output:
Tensor
[[[[5 ],
[10]],
[[15],
[20]],
[[25],
[30]]]]
Example 2:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs"// Initializing a Tensorconst x = tf.tensor4d([2, 4, 6, 8], [1, 2, 2, 1]);// Using the blockShape and paddings as the// parameter for the .spaceToBatchND() functionx.spaceToBatchND([2, 2], [[0, 0], [0, 0]]).print(); |
Output:
Tensor
[ [ [[2],]],
[ [[4],]],
[ [[6],]],
[ [[8],]]]
Reference: https://js.tensorflow.org/api/latest/#spaceToBatchND
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



