Open In App

Tensorflow.js tf.metrics.binaryCrossentropy() Function

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:  



Return Value: It returns the tf.Tensor object.

Example 1:  




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




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

Article Tags :