Open In App

Tensorflow.js tf.inTopKAsync() Function

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 .inTopKAsync() function is used to check if the stated targets are in the given top K predictions.



Syntax :  

tf.inTopKAsync(predictions, targets, k?)

Parameters:  



Return Value: It returns Promise tf.Tensor object.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining predictions and targets
const pred = tf.tensor2d([
    [11, 22, 33, 55], 
    [33, 66, 22, -11]
]);
  
const targ = tf.tensor1d([1, 1]);
  
// Calling tf.inTopKAsync() method
const res = await tf.inTopKAsync(pred, targ);
  
// Printing output
res.print();

Output:

Tensor
    [false, true]

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.inTopKAsync() method with 
// all its parameters
const res = await tf.inTopKAsync(
    tf.tensor2d([[1.1, 2.2], [3.3, 6.6]]), 
    tf.tensor1d([0, 1]), 2);
  
// Printing output
res.print();

Output:

Tensor
    [true, true]

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

Article Tags :