Thursday, September 4, 2025
HomeLanguagesJavascriptTensorflow.js tf.metrics.meanAbsolutePercentageError() Function

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 order to return tf.Tensor object.

Syntax:  

tf.metrics.meanAbsolutePercentageError(yTrue, yPred)

Parameters:  

  • yTrue: It is the stated truth tensor and it can be of type tf.Tensor.
  • yPred: It is the stated prediction tensor 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 truth and prediction tensors
const y = tf.tensor2d([[0, 2], [20, 30]]);
const z = tf.tensor2d([[0, 2], [21, 34]]);
  
// Calling metrics.meanAbsolutePercentageError() 
// method
const mape = tf.metrics.meanAbsolutePercentageError(y, z);
  
// Printing output
mape.print();


Output:

Tensor
    [0, 9.166666]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling metrics.meanAbsolutePercentageError() 
// method with its parameter directly and then
// Printing output
const output = tf.metrics.meanAbsolutePercentageError(tf.tensor(
    [
      [0, 1, 0, 0],
      [0, 1, 1, 0],
      [0, 0, 0, 1],
      [1, 1, 0, 0],
      [0, 0, 1, 0]
    ]
), tf.tensor(
    [
      [0, 0, 1, 1],
      [0, 1, 1, 0],
      [0, 0, 0, 1],
      [0, 1, 0, 1],
      [1, 1, 0, 0]
    ]
)).print();


Output:

Tensor
    [500025, 0, 0, 250025, 500025]

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

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
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS