Fixed: NotImplementedError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Long
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:
import torch
counts = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(counts.mean())
uv run python mean_int.py
What I Expected
The average of the six numbers, 3.5.
What I Got
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

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:
import torch
counts = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(counts.float().mean())
print(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
uv run python mean_int_fixed.py
tensor(3.5000)
tensor(3.5000)

Both ways gave 3.5, the average of 1 to 6. The commands work unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do 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: Long | Convert 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.int64 | Average the result: counts.float().mean(), or keep it in a variable |
| Writing data as whole numbers when you'll average it | Every average needs a conversion | Create it as decimals, torch.tensor([[1.0, 2.0, 3.0]]) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Got: Int instead of Got: Long | A tensor of 32-bit whole numbers, torch.int32 | The same fix |
Got: Bool | A tensor of True/False values, such as the result of a comparison like counts > 3 | Convert 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
- torch.mean — the input types
mean()accepts and itsdtypeargument. - Tensor attributes: torch.dtype — PyTorch's data types and their names.
