Skip to content
Related Articles

Related Articles

Tensorflow.js tf.metrics.binaryAccuracy() Function

Improve Article
Save Article
  • Last Updated : 25 May, 2021
Improve Article
Save Article

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.

The tf.metrics.binaryAccuracy() function is used to calculate how often predictions match binary labels. And the function takes two tensors as a parameter and the value of tensors is between 0 and 1.

Syntax:

tf.metrics.binaryAccuracy (True, Prediction)

Parameters: 

  • True: It is the binary tensor of truth and the tensor can contain values between 0 and 1.
  • Prediction: It is the tensor of predictions and the tensor can contain values between 0 and 1.

Return Value: It returns a tensor.

Example 1: In this example, we are giving two 1d tensors that contain values between 0 and 1 as a parameter, and the metrics.binaryAccuracy function will calculate the predictions match and return a tensor.

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Defining the value of the tensor
const True = tf.tensor1d([1, 0, 1, 1, 0, 1, 0, 0]);
const Prediction = tf.tensor1d([0.2, 0.4, 0.6, 0.3, 0.7, 0.3, 0.4, 0.7]);
  
// Calculating predictions match
const accuracy = tf.metrics.binaryAccuracy(True, Prediction);
  
// Printing the tensor
accuracy.print();


Output:

Tensor
    0.375

Example 2: In this example, we are giving two 2d tensors that contain values 0 and 1 as a parameter, and the metrics.binaryAccuracy function will calculate the predictions match and return a tensor.

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Defining the value of the tensor
const True = tf.tensor2d([[1, 0, 1, 1], [1, 0, 1, 0]], [2, 4]);
const Prediction = tf.tensor2d([[1, 0, 1, 0], [0, 1, 0, 1]], [2, 4]);
  
// Calculating predictions match
const accuracy = tf.metrics.binaryAccuracy(True, Prediction);
  
// Printing the tensor
accuracy.print();


Output:

Tensor
    [0.75, 0]

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!