Friday, November 14, 2025
HomeLanguagesJavascriptTensorflow.js tf.metrics.meanAbsoluteError() Function

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 applied over feature dimensions. It takes two tensors as a parameter.

mean(abs(Prediction - True))

Syntax:

tf.metrics.meanAbsoluteError(Tensor1, Tensor2);

Parameters: 

  • Tensor1: It is the truth tensor.
  • Tensor2: It is the Prediction tensor.

Return Value: It returns the tensor of the mean absolute errors.

Example 1: In this example, we are giving two 1d tensors as a parameter, and the metrics.meanAbsoluteError function will calculate the mean absolute error and return a tensor.

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Defining the value of the tensors
const True = tf.tensor([1,2,3]);
const Prediction = tf.tensor([3,2,1]);
  
// Calculating mean absolute error
const error = tf.metrics.meanAbsoluteError(True, Prediction);
  
// Printing the tensor
error.print();


Output:

Tensor
    1.3333333730697632

Example 2: In this example, we are giving two 2d tensors as a parameter, and the metrics.meanAbsoluteError function will calculate the mean absolute error and return a tensor.

Javascript




// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Defining the value of the tensors
const True = tf.tensor([[1,2,3],[2,4,1]]);
const Prediction = tf.tensor([[3,2,1],[5,2,1]]);
  
// Calculating mean absolute error
const error = tf.metrics.meanAbsoluteError(True, Prediction);
  
// Printing the tensor
error.print();


Output:

Tensor
    [1.3333334, 1.6666667]

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

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
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS