Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.inTopKAsync() Function

  • Last Updated : 25 May, 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 .inTopKAsync() function is used to check if the stated targets are in the given top K predictions.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :