GFG App
Open App
Browser
Continue

# Tensorflow.js tf.metrics.categoricalAccuracy() 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.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```

My Personal Notes arrow_drop_up