Saturday, August 30, 2025
HomeLanguagesJavascriptTensorflow.js tf.spaceToBatchND() Function

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 library
import * as tf from "@tensorflow/tfjs"
 
// Initializing a Tensor
const x = tf.tensor4d(
    [5, 10, 15, 20, 25, 30],
    [1, 3, 2, 1]
);
 
// Initializing blockShape and paddings parameters
const blockShape = [1, 1];
const paddings = [[0, 0], [0, 0]];
 
// Calling the .spaceToBatchND() function over
// the above parameters and Tensor
x.spaceToBatchND(blockShape, paddings).print();


Output: 

Tensor
   [[[[5 ],
      [10]],

     [[15],
      [20]],

     [[25],
      [30]]]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing a Tensor
const x = tf.tensor4d([2, 4, 6, 8], [1, 2, 2, 1]);
 
// Using the blockShape and paddings as the
// parameter for the .spaceToBatchND() function
x.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, 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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS