Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.sparseReshape() Function

  • Last Updated : 19 Aug, 2021

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .sparseReshape() function possesses identical syntax like reshape() function on the rendered compressed tensor. Where the input indices are recalculated on the basis of the required new shape. 

Note:

  • In case, one element of the stated new shape is the distinct value i.e. -1, then the size of such dimension is calculated such that the count of the dense size stays constant.
  • At best only one component of the stated new shape can be -1.
  • Here, the count of compressed components which is indicated by the stated new shape should be the same as the count of compressed components which is originally indicated by the stated input shape.
  • The reshaping fails to impact the order of the values in the stated sparse tensor.
  • In case, the input tensor possesses rank R_in, as well as N, loaded values, and the new shape, possesses length R_out, then the shape of the input indices is [N, R_in], the length of input shape is R_in, the shape of the output indices is [N, R_out], and the length of the output shape is R_out.

Syntax:

tf.sparseReshape(inputIndices, inputShape, newShape)

Parameters: This method accepts the following parameter:

  • inputIndices: It is the stated 2-D. N x R_in matrix along with the indices of the loaded values in a sparse tensor. It can be of type tf.Tensor2D, TypedArray, or an Array.
  • inputShape: It is the stated 1-D. R_in Tensor1D along with the input sparse tensor’s compressed shape. It can be of type tf.Tensor1D, TypedArray, or an Array.
  • newShape: It is the stated 1-D. R_out Tensor1D along with the demanded new compressed shape. It can be of type tf.Tensor1D, TypedArray, or an Array.

Return Value: It returns tf.Tensor object.

Example 1: In the following example, we have called sparse.sparseReshape() function with all its parameter and printed the output response without its indices and shape.

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
  
// Calling sparse.sparseReshape() function
// with all its parameter
const res = tf.sparse.sparseReshape(
    [[1, 0, 1], [2, 0, 1], [1, 1, 2], [0, -1, 0], [-3, 1, 2]],
    [1, 2, 9], [-1, 9]);
  
// Printing output
console.log(res);

Output:

{
  "outputIndices": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      5,
      2
    ],
    "dtype": "float32",
    "size": 10,
    "strides": [
      2
    ],
    "dataId": {
      "id": 82
    },
    "id": 82,
    "rankType": "2",
    "scopeId": 36
  },
  "outputShape": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      2
    ],
    "dtype": "float32",
    "size": 2,
    "strides": [],
    "dataId": {
      "id": 83
    },
    "id": 83,
    "rankType": "1",
    "scopeId": 36
  }
}

Example 2: In the following example, we have called sparse.sparseReshape() function with all its parameter and printed the output response with its indices and shape.

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
  
// Calling sparse.sparseReshape() function
// with all its parameter
const res = tf.sparse.sparseReshape(
    [[1.1, 0, 1.2], [2.1, 0.2, 1.3], [1.4, 2.3, 2.5], [null, -1, 0], [-3, 1, 2]],
    [1.0, 3, 3], [-1, 9]);
  
// Printing output indices
res['outputIndices'].print();
  
// Printing output shape
res['outputShape'].print();

Output:

Tensor
    [[1 , 2 ],
     [2 , 2 ],
     [2 , 3 ],
     [0 , -3],
     [-2, -4]]
Tensor
    [1, 9]

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!