How to Add, Subtract, Multiply and Divide Tensors in TensorFlow
Adding two tensors in TensorFlow adds them value by value: the first to the first, the second to the second, and so on. The same goes for subtracting, multiplying and dividing. This guide does all four on two small tensors in TensorFlow 2.22.0, and shows the one result whose type surprises people.
Syntax
<tensor a> + <tensor b>
<tensor a> - <tensor b>
<tensor a> * <tensor b>
<tensor a> / <tensor b>
| Part | What it means |
|---|---|
<tensor a>, <tensor b> | Two tensors of the same data type and the same shape, or shapes that can be broadcast |
+, -, *, / | Add, subtract, multiply or divide each pair of matching values. The functions tf.add, tf.subtract, tf.multiply and tf.divide do the same |
The tf.math.add API page describes +: "Returns x + y element-wise." Element-wise means value by value, at matching positions. The tensor guide lists what tensors support: "You can do basic math on tensors, including addition, element-wise multiplication, and matrix multiplication."
The Example
Two rows of three whole numbers, [10, 20, 30] and [1, 2, 3], combined with each operator and with tf.add.
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
a = tf.constant([10, 20, 30])
b = tf.constant([1, 2, 3])
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(tf.add(a, b))

Both tensors are int32: TensorFlow's type for Python's whole numbers.
Step 2: Run It
uv run python arithmetic.py
tf.Tensor([11 22 33], shape=(3,), dtype=int32)
tf.Tensor([ 9 18 27], shape=(3,), dtype=int32)
tf.Tensor([10 40 90], shape=(3,), dtype=int32)
tf.Tensor([10. 10. 10.], shape=(3,), dtype=float64)
tf.Tensor([11 22 33], shape=(3,), dtype=int32)

Each result pairs the values by position: 10 + 1, 20 + 2, 30 + 3. * multiplies the pairs too, so a * b is [10 40 90], not a matrix product. tf.add(a, b) gave the same as a + b. The command works unchanged in PowerShell.
a / b is the odd one out: [10. 10. 10.], as float64. The tf.math.truediv API page explains it: "This function forces Python 3 division operator semantics where all integer arguments are cast to floating types first", and "the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy)." For whole-number division, a // b gave [10 10 10] as int32.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Tensors of different lengths: [10, 20, 30] + [1, 2] | InvalidArgumentError: … Incompatible shapes: [3] vs. [2] [Op:AddV2]. There's no matching value for 30 | Give both the same shape, or shapes that broadcast |
Tensors of different types, such as uint8 and float32 | cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor, as Fixed: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor shows | Convert one with tf.cast first |
Dividing a uint8 tensor by 255.0 | TypeError: `x` and `y` must have the same dtype, got tf.uint8 != tf.float32., as Fixed: TypeError: x and y must have the same dtype, got tf.uint8 != tf.float32 shows | Cast to float32, then divide |
FAQ
Can I add a single number to a tensor?
Yes: a + 5 added 5 to every value and gave [15 25 35]. TensorFlow stretches the single number to the tensor's shape, which is called broadcasting.
Why is the division result float64 and not float32?
Because the inputs are int32. The truediv page says int32 and int64 inputs are cast to float64 first, "matching the behavior of Numpy". To get float32, cast the inputs to float32 before dividing.
Conclusion
+, -, * and / combined two tensors value by value, and tf.add and its siblings did the same. Dividing int32 tensors gave float64, and tensors of different shapes or types stopped with an error until they matched.
Additional Resources
- tf.math.add — addition, with the broadcasting rules.
- tf.math.truediv — how
/chooses its result type. - Introduction to tensors — basic math on tensors.
