Open In App

Tensorflow.js tf.metrics.binaryCrossentropy() Function

Last Updated : 18 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.binaryCrossentropy() function is binary crossentropy metric function which uses binary tensors and returns tf.Tensor object.

Syntax:  

tf.metrics.binaryCrossentropy (yTrue, yPred)

Parameters:  

  • yTrue: It is the stated binary tensor input of truth, and it can be of type tf.Tensor.
  • yPred: It is the stated binary tensor input of prediction, 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 binary tensors
const y = tf.tensor2d([[4], [5], [6], [7]]);
const z = tf.tensor2d([[1], [2], [0], [1.8]]);
  
// Calling metrics.binaryCrossentropy() 
// method
const binry_crsntropy = 
    tf.metrics.binaryCrossentropy(y, z);
  
// Printing output
binry_crsntropy.print();


Output:

Tensor
    [-27.6301231, -36.8401985, 55.2615433, -55.2603455] 

Example 2: 

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling metrics.binaryCrossentropy() 
// method and printing output
tf.metrics.binaryCrossentropy(
    tf.tensor2d([[-13], [-2.787]]), 
    ([[-0.6], [-12]])).print();


Output:

Tensor
    [-119.7330246, -25.6688404]

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads