Open In App

Tensorflow.js tf.metrics.categoricalAccuracy() 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 and deep learning neural networks in the browser or node environment.

The tf.metrics.categoricalAccuracy() function is used to return categorical accuracy between two tensor. It takes two tensor as a parameter.

Syntax:

tf.metrics.categoricalAccuracy(a, b);

Parameters: 

  • a: The first specified tensor.
  • b: The second specified tensor. It must have same data type as “a”.

Return Value: It returns the categorical accuracy of the two specified tensors “a” and “b”.

Example 1:

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Initializing two tensors
const a = tf.tensor2d([[1, 0, 0, 1], [0, 1, 0, 1]]);
const b = tf.tensor2d([
    [0.1, 0.6, 0.01, 0.05], 
    [0.1, 0.02, 0.05, 0.3]
]);
  
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
  
// Print tensor 
accuracy.print();


Output:

Tensor
    [0, 0]

Example 2:

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Initializing two tensors
const a = tf.tensor([1, 0, 0, 1]);
const b = tf.tensor([1, 0.6, 0.01, 0.05]);
  
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
  
// Print tensor 
accuracy.print();


Output:

Tensor
    1

Example 3:

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Initializing two tensors
const a = tf.tensor([0, 0, 0, 1]);
const b = tf.tensor([0.1, 0.8, 0.05, 0.05]);
  
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
  
// Print tensor 
accuracy.print();


Output:

Tensor
    0

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



Previous Article
Next Article

Similar Reads

Tensorflow.js tf.metrics.meanAbsolutePercentageError() 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.meanAbsolutePercentageError() function is a loss or else a metric function i.e. mean absolute percentage error which uses truth and prediction tensor inputs in orde
2 min read
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 (
1 min read
Tensorflow.js tf.metrics.meanAbsoluteError() Function
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.meanAbsoluteError() is used to calculate mean absolute error. The mean absolute error is defined as the mean of absolute difference of two tensors. Where, the mean is ap
2 min read
Tensorflow.js tf.metrics.binaryAccuracy() Function
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 b
2 min read
Tensorflow.js tf.metrics.categoricalCrossentropy() Function
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf.metrics.categoricalCrossentropy() function categor
2 min read
Tensorflow.js tf.metrics.cosineProximity() Function
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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf.metrics.cosineProximity() function is defined as:
2 min read
Tensorflow.js tf.metrics.recall() Function
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.recall() function is used to compute the recall of the predictions with respect to the labels. 'Recall' is one of the metrics in machine learning. You can read more abou
2 min read
Tensorflow.js tf.metrics.meanSquaredError() Function
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 Tensorflow tf.metrics.meanSquaredError() function is a Loss or metric function used to Computes the mean squared error between y_true and y_pred. the y_true is a truth tensor and y
2 min read
Tensorflow.js tf.metrics.sparseCategoricalAccuracy() 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.sparseCategoricalAccuracy() function is sparse categorical accuracy metric function which uses indices and logits in order to return tf.Tensor object. Syntax: tf.me
1 min read
Tensorflow.js tf.metrics.precision() 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.precision() function is used to calculate the precision of the expectancy with reference to the names. Syntax: tf.metrics.precision(yTrue, yPred) Parameters: yTrue:
2 min read