Skip to main content

How to Get the Absolute Value of a Tensor with tf.abs

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
tf.abs(<tensor>)
PartWhat 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​

differences.py
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))

The differences.py script in a macOS-style code window: a forecast of 20, 25 and 18 degrees, the actual 22.5, 24 and 18, their difference as errors, then prints of errors and 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​

Terminal
uv run python differences.py
Output
tf.Tensor([-2.5 1. 0. ], shape=(3,), dtype=float32)
tf.Tensor([2.5 1. 0. ], shape=(3,), dtype=float32)

The terminal after uv run python differences.py: the errors -2.5, 1 and 0, then their absolute values 2.5, 1 and 0.

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​

MistakeWhat happensDo this instead
Taking the absolute value of the smallest int8tf.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 negativeCast to a wider type first: in int32 the result was [128 5 7]
Averaging the errors before tf.absThe negative and positive errors partly cancel out: tf.reduce_mean(errors) gave -0.5, while the forecast really missed by 1.1666666 on averageTake 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​