Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.metrics.precision() Function

  • Last Updated : 01 Aug, 2021

Tensorflow.js is an open-source library developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .metrics.precision() function is used to calculate the precision of the expectancy with reference to the names.

Syntax:  

tf.metrics.precision(yTrue, yPred)

Parameters:  

  • yTrue: It is the stated ground truth tensor which is supposed to hold values from 0 to 1 and it can be of type tf.Tensor.
  • yPred: It is the stated prediction tensor which is supposed to hold values from 0 to 1 and it can be of type tf.Tensor.

Return Value: It returns the tf.Tensor object.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining truth and prediction tensors
const y = tf.tensor2d([[0, 1], [1, 1]]);
const z = tf.tensor2d([[1, 0], [0, 1]]);
  
// Calling metrics.precision() method
const pre = tf.metrics.precision(y, z);
  
// Printing output
pre.print();

Output:

Tensor
    0.5

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling metrics.precision() method with
// its parameter directly and then 
// Printing output
const output = tf.metrics.precision(tf.tensor(
    [
      [0, 1, 0, 0],
      [0, 1, 1, 0],
      [0, 0, 0, 1],
      [1, 1, 0, 0],
      [0, 0, 1, 0]
    ]
), tf.tensor(
    [
      [0, 0, 1, 1],
      [0, 1, 1, 0],
      [0, 0, 0, 1],
      [0, 1, 0, 1],
      [1, 1, 0, 0]
    ]
)).print();

Output:

Tensor
    0.4444444477558136

Reference: https://js.tensorflow.org/api/latest/#metrics.precision

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!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!