# Calculate Mean Absolute Percentage Error using TensorFlow 2

Mean absolute percentage error (MAPE) is a loss function that is used to solve regression problems. MAPE is calculated as the average of the absolute percentage differences between the actual and predicted values.

The formula to calculate the MAPE:

• `n` - the number of data points.
• `y` - the actual value of the data point. Also known as true value.
• `ŷ` - the predicted value of the data point. This value is returned by model.

Let's say we have the following sets of numbers:

Here is example how MAPE can be calculated using these numbers:

TensorFlow 2 allows to calculate the MAPE. It can be done by using `MeanAbsolutePercentageError` class.

``````from tensorflow import keras

yActual = [4, -1.5, 5, 2]
yPredicted = [3.5, 1, 5, 3]

mapeObject = keras.losses.MeanAbsolutePercentageError()
mapeTensor = mapeObject(yActual, yPredicted)
mape = mapeTensor.numpy()

print(mape)``````

MAPE also can be calculated by using `mean_absolute_percentage_error`, `mape` or `MAPE` function.

``````mapeTensor = keras.losses.mean_absolute_percentage_error(yActual, yPredicted)
mape = mapeTensor.numpy()

mapeTensor = keras.losses.mape(yActual, yPredicted)
mape = mapeTensor.numpy()

mapeTensor = keras.losses.MAPE(yActual, yPredicted)
mape = mapeTensor.numpy()``````