Skip to main content

Fixed: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Adding a float32 tensor to a uint8 tensor of pixels stopped with InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:AddV2]. TensorFlow's + only adds tensors of the same type. Casting the pixels to float32 first fixed it.

What I Was Doing​

I was brightening three pixel values by adding 5.5 to each. The pixels were uint8, the type a decoded photo has, and the amount to add was a tensor of decimals:

brighten.py
import tensorflow as tf

pixels = tf.constant([10, 120, 240], dtype=tf.uint8)
boost = tf.constant([5.5, 5.5, 5.5])

brighter = pixels + boost
print(brighter)

I ran it in a uv project with TensorFlow 2.22.0:

Terminal
uv run python brighten.py

What I Expected​

[15.5 125.5 245.5]: each pixel plus 5.5.

What I Got​

Output
Traceback (most recent call last):
File "/Users/you/source/fx-addv2-dtype/brighten.py", line 6, in <module>
brighter = pixels + boost
~~~~~~~^~~~~~~
File "/Users/you/source/fx-addv2-dtype/.venv/lib/python3.14/site-packages/tensorflow/python/util/traceback_utils.py", line 167, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/you/source/fx-addv2-dtype/.venv/lib/python3.14/site-packages/tensorflow/python/framework/ops.py", line 6084, in raise_from_not_ok_status
raise core._status_to_exception(e) from None # pylint: disable=protected-access
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:AddV2] name:

The terminal after uv run python brighten.py: a traceback pointing at pixels + boost on line 6, ending in InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor, for the AddV2 operation.

The script stopped at line 6, the addition, and printed nothing.

What I Checked​

1. What each part of the message means​

AddV2 is the TensorFlow operation behind +: the traceback points at pixels + boost. Its inputs are counted from 0 ("zero-based"), so input #0 is pixels and input #1 is boost. Input #0 is uint8, so TensorFlow expected input #1 to be uint8 too, and it was a float tensor: tf.constant([5.5, 5.5, 5.5]) makes float32.

2. What the documentation says​

The tf.math.add API page lists what its two inputs may be. For the second: "A tf.Tensor. Must have the same type as x."

3. Whether a plain number does the same​

pixels + 5.5, with a Python number instead of a tensor, stopped with exactly the same error. 5.5 became a float tensor, and the types still didn't match.

Why It Happens​

+ adds only tensors of the same type, and TensorFlow doesn't convert either side to match the other. A uint8 tensor holds whole numbers from 0 to 255, so it can't take part in a sum with decimals until it's converted.

The Fix​

Cast the pixels to float32 before adding:

brighten.py
import tensorflow as tf

pixels = tf.constant([10, 120, 240], dtype=tf.uint8)
boost = tf.constant([5.5, 5.5, 5.5])

brighter = tf.cast(pixels, tf.float32) + boost
print(brighter)

tf.cast(pixels, tf.float32) makes a float32 copy of the pixels, so both inputs of + are float32.

After the Fix​

Terminal
uv run python brighten.py
Output
tf.Tensor([ 15.5 125.5 245.5], shape=(3,), dtype=float32)

The terminal after the fix: the float32 tensor 15.5, 125.5 and 245.5.

Each pixel went up by 5.5, and the result is float32. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Casting boost to uint8 insteadpixels + tf.cast(boost, tf.uint8) returned [ 15 125 245]: the cast dropped the .5 before addingCast the whole numbers to decimals, not the other way round
Adding a plain 5.5The same error: 5.5 becomes a float tensorCast the pixels first
Swapping the sides: boost + pixelsThe message turns round: cannot compute AddV2 as input #1(zero-based) was expected to be a float tensor but is a uint8 tensor [Op:AddV2]The same fix: cast the uint8 tensor

Troubleshooting​

SymptomLikely causeFix
cannot compute AddV2 … expected to be a uint8 tensor but is a float tensorA float32 tensor added to a uint8 onetf.cast(pixels, tf.float32) + boost
cannot compute Mul … or cannot compute Sub … with the same words* or - with the same mix: pixels * boost gave [Op:Mul], pixels - boost gave [Op:Sub]Cast first, as for +
x` and `y` must have the same dtype, got tf.uint8 != tf.float32/ with the same mixFixed: TypeError: x and y must have the same dtype, got tf.uint8 != tf.float32

FAQ​

What does "zero-based" mean?​

That counting starts at 0. An operation's first input is #0 and its second is #1, so in pixels + boost, input #1 is boost.

How do I turn the result back into whole pixel values?​

Round, keep it between 0 and 255, and cast: tf.cast(tf.clip_by_value(tf.round(brighter), 0, 255), tf.uint8) returned [ 16 126 246]. tf.round rounds a half to the nearest even number, so 15.5 became 16 and 125.5 became 126.

Conclusion​

cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor meant + got a uint8 tensor and a float32 one. tf.cast(pixels, tf.float32) + boost made both float32, and returned [ 15.5 125.5 245.5].

Additional Resources​

  • tf.math.add — the rule for both inputs' types.
  • tf.cast — converting a tensor's type.