Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.sparseFillEmptyRows() Function

Tensorflow.js tf.sparseFillEmptyRows() 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. 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

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!

RELATED ARTICLES

Most Popular

Recent Comments