How to Get the Absolute Value of a Tensor with tf.abs
When you compare two tensors, the differences come out positive and negative, and you usually only care how big they are. tf.abs drops the minus signs. This guide uses it on a weather forecast's errors in TensorFlow 2.22.0, and shows the one value it can't make positive.
Syntax
tf.abs(<tensor>)
| Part | What it means |
|---|---|
<tensor> | A tensor of whole or decimal numbers. Each value is replaced by its distance from zero |
The tf.math.abs API page says it "Computes the absolute value of a tensor": "Given a tensor of integer or floating-point values, this operation returns a tensor of the same type, where each element contains the absolute value of the corresponding element in the input." tf.abs is the same function under a shorter name.
The Example
A forecast of three days' temperatures, next to what really happened. Their difference is the forecast's error, and tf.abs turns each error into how far off the forecast was.
Prerequisites
- A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Write the Script
import tensorflow as tf
forecast = tf.constant([20.0, 25.0, 18.0])
actual = tf.constant([22.5, 24.0, 18.0])
errors = forecast - actual
print(errors)
print(tf.abs(errors))

forecast - actual subtracts day by day. A forecast that was too low gives a negative error, one that was too high a positive one.
Step 2: Run It
uv run python differences.py
tf.Tensor([-2.5 1. 0. ], shape=(3,), dtype=float32)
tf.Tensor([2.5 1. 0. ], shape=(3,), dtype=float32)

The forecast was 2.5 degrees too low on day 1, 1 degree too high on day 2 and right on day 3. tf.abs kept the sizes, 2.5, 1 and 0, and dropped the sign. The result has the same shape and type as the input. The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Taking the absolute value of the smallest int8 | tf.abs(tf.constant([-128, -5, 7], dtype=tf.int8)) returned [-128 5 7]. int8 holds -128 to 127, so 128 doesn't fit and -128 stays negative | Cast to a wider type first: in int32 the result was [128 5 7] |
Averaging the errors before tf.abs | The negative and positive errors partly cancel out: tf.reduce_mean(errors) gave -0.5, while the forecast really missed by 1.1666666 on average | Take tf.abs first, then average |
FAQ
How do I get the average error of the forecast?
Average the absolute errors: tf.reduce_mean(tf.abs(forecast - actual)) returned 1.1666666. This is the mean absolute error, a common way to score a forecast.
Does Python's own abs() work on a tensor?
Yes: abs(forecast - actual) returned the same [2.5 1. 0. ] tensor.
Conclusion
tf.abs turned the forecast's errors -2.5, 1 and 0 into 2.5, 1 and 0, keeping the shape and type. Averaged, they gave a mean absolute error of 1.1666666. The one exception is a type's smallest whole number, such as -128 in int8, which has no positive partner in that type.
Additional Resources
- tf.math.abs — the API page, including complex numbers.
- tf.math.reduce_mean — averaging the absolute values.
