Skip to content
Related Articles

Related Articles

Tensorflow.js tf.metrics.meanAbsoluteError() Function

Improve Article
Save Article
  • Last Updated : 25 May, 2021
Improve Article
Save Article

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!