Open In App

Tensorflow.js tf.sparseFillEmptyRows() Function

Last Updated : 25 May, 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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.sparseFillEmptyRows() function is used to get the input SparseTensor that is represented via the map of inputs, (indices, values, denseShape).

Syntax:

tf.sparseFillEmptyRows(indices, values, denseShape, defaultValue)

Parameters:

  • indices: The indices of the sparse tensor.
  • values: The value of the sparse tensor.
  • denseShape: The shape of the sparse tensor.
  • defaultValue: Default value to insert into location.

Return Value: It returns tf.Tensor.

Example 1:

Javascript




const result = tf.sparse.sparseFillEmptyRows(
    [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]],
    [0, 1, 2, 3, 4], [5, 6], -1);
  
result['outputIndices'].print();
result['outputValues'].print();
result['reverseIndexMap'].print();


Output:

Tensor
    [[0, 1],
     [1, 2],
     [2, 3],
     [3, 4],
     [4, 5]]
Tensor
    [0, 1, 2, 3, 4]
Tensor
    [0, 1, 2, 3, 4]

Example 2:

Javascript




const result = tf.sparse.sparseFillEmptyRows(
    [[1], [1], [4], [3], [2]],
    [4, 6, 8, 5, 3], [7, 2], -1);
  
console.log(result);


Output:

{
  "outputIndices": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      5,
      1
    ],
    "dtype": "float32",
    "size": 5,
    "strides": [
      1
    ],
    "dataId": {
      "id": 12
    },
    "id": 12,
    "rankType": "2",
    "scopeId": 2
  },
  "outputValues": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      5
    ],
    "dtype": "float32",
    "size": 5,
    "strides": [],
    "dataId": {
      "id": 13
    },
    "id": 13,
    "rankType": "1",
    "scopeId": 2
  },
  "emptyRowIndicator": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      7
    ],
    "dtype": "bool",
    "size": 7,
    "strides": [],
    "dataId": {
      "id": 14
    },
    "id": 14,
    "rankType": "1",
    "scopeId": 2
  },
  "reverseIndexMap": {
    "kept": false,
    "isDisposedInternal": false,
    "shape": [
      5
    ],
    "dtype": "float32",
    "size": 5,
    "strides": [],
    "dataId": {
      "id": 15
    },
    "id": 15,
    "rankType": "1",
    "scopeId": 2
  }
}

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads