Related Articles

# Tensorflow.js tf.metrics.meanAbsoluteError() Function

• Last Updated : 25 May, 2021

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

`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]```

My Personal Notes arrow_drop_up