Skip to main content

Fixed: NotImplementedError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Long

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

I asked PyTorch for the average of a tensor of whole numbers with mean(), and it stopped with NotImplementedError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Long. mean() only averages decimal (or complex) numbers. Converting the tensor with .float(), or passing dtype=torch.float32, fixed it.

What I Was Doing​

I was writing a guide to mean() in PyTorch 2.15.0, on Python 3.15 on an Apple M1 Pro, and checked what happens with whole numbers: a table of the numbers 1 to 6, written without decimal points:

mean_int.py
import torch

counts = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(counts.mean())
Terminal
uv run python mean_int.py

What I Expected​

The average of the six numbers, 3.5.

What I Got​

Output
Traceback (most recent call last):
File "/Users/you/source/pytorch-mean/mean_int.py", line 4, in <module>
print(counts.mean())
~~~~~~~~~~~^^
NotImplementedError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Long

The terminal after uv run python mean_int.py: a traceback pointing at counts.mean() on line 4 and ending in NotImplementedError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Long.

What I Checked​

What "Long" is. Long is PyTorch's name for whole numbers stored in 64 bits, torch.int64, also written torch.long. counts.dtype printed torch.int64: torch.tensor() gives whole numbers that data type, as How to Check the Data Type of a Tensor in PyTorch shows.

What mean() accepts. The torch.mean reference says: "Input must be floating point or complex." Its dtype argument is "the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed."

The error says the same in its own words: mean() "could not infer output dtype". It doesn't choose a data type for the average of whole numbers, and it doesn't round the average to a whole number either: you choose a decimal type.

Why It Happens​

Numbers written without a decimal point, like 1 and 2, make a tensor of whole numbers (Long). mean() works only on decimal or complex tensors, and it doesn't convert whole numbers on its own.

The Fix​

Convert the tensor to decimal numbers first, or tell mean() which data type to compute in. Both work:

mean_int_fixed.py
import torch

counts = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(counts.float().mean())
print(counts.mean(dtype=torch.float32))

The fixed mean_int_fixed.py in a macOS-style code window: the same tensor of whole numbers, then a print of counts.float().mean() and a print of counts.mean(dtype=torch.float32).

  • counts.float() returns a copy of the tensor as decimal numbers, torch.float32, and .mean() averages the copy.
  • counts.mean(dtype=torch.float32) converts the values while averaging, as the reference describes.

After the Fix​

Terminal
uv run python mean_int_fixed.py
Output
tensor(3.5000)
tensor(3.5000)

The terminal after the fix: tensor(3.5000) printed twice.

Both ways gave 3.5, the average of 1 to 6. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Converting with .int() or .long()The tensor stays whole numbers: .int() gave the same error ending in Got: Int, and .long() in Got: LongConvert to a decimal type: .float()
Calling counts.float() on its own line and averaging counts.float() returns a new tensor: after that line, counts.dtype still printed torch.int64Average the result: counts.float().mean(), or keep it in a variable
Writing data as whole numbers when you'll average itEvery average needs a conversionCreate it as decimals, torch.tensor([[1.0, 2.0, 3.0]])

Troubleshooting​

SymptomLikely causeFix
Got: Int instead of Got: LongA tensor of 32-bit whole numbers, torch.int32The same fix
Got: BoolA tensor of True/False values, such as the result of a comparison like counts > 3Convert with .float(): the average is then the share of True values. (counts > 3).float().mean() gave tensor(0.5000), as three of the six numbers are above 3

FAQ​

Why does sum() work on whole numbers when mean() doesn't?​

The sum of whole numbers is always a whole number, so sum() can return the same data type: counts.sum() printed tensor(21). An average usually isn't, so mean() needs a decimal type.

Will .float() lose precision on big numbers?​

It can, above 16,777,216 (2²⁴): up to there, float32 holds every whole number exactly. torch.tensor(16_777_217).float() became 16777216.0, while .double(), which converts to float64, kept 16777217.0. For large counts, use .double().

Conclusion​

NotImplementedError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Long came from averaging a tensor of whole numbers. counts.float().mean() and counts.mean(dtype=torch.float32) both returned tensor(3.5000).

Additional Resources​