Open In App

Tensorflow.js tf.inTopKAsync() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:  

  • predictions: It is the stated 2-D or upper tensor input whose last size is not lesser than k and it can be of type tf.Tensor, TypedArray, or Array.
  • targets: It is the stated 1-D or upper tensor input and it can be of type tf.Tensor, TypedArray, or Array.
  • k: It is the alternative number of top elements which are to be considered for calculating precision. The by default value is 1 and it is of type number.

Return Value: It returns Promise tf.Tensor object.

Example 1:

Javascript




// 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:

Javascript




// 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


Last Updated : 25 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads