Open In App

Tensorflow.js tf.spaceToBatchND() Function

Last Updated : 21 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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
 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads