Monday, November 17, 2025
HomeLanguagesJavascriptTensorflow.js tf.metrics.binaryCrossentropy() Function

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:  

  • 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32402 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6769 POSTS0 COMMENTS
Nicole Veronica
11920 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11990 POSTS0 COMMENTS
Shaida Kate Naidoo
6897 POSTS0 COMMENTS
Ted Musemwa
7150 POSTS0 COMMENTS
Thapelo Manthata
6851 POSTS0 COMMENTS
Umr Jansen
6843 POSTS0 COMMENTS