Open In App

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:



Return Value: It returns a Tensor of the splitted version of the specified input space.

Example 1:




// 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:




// 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
 

Article Tags :