Support torch.mean for BoolTensors and other integer tensor inputs (without manual upcasting and hopefully without hidden upcasting) · Issue #45833 · pytorch/pytorch · GitHub
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I suggest that it upcasts to torch.float32 automatically, like in NumPy. For sum it works and upcasts to torch.long.
Is it a duplicate? Could not easily find the previous issue :/ Or in #38349
importtorchimportnumpyprint(torch.__version__)
# 1.6.0a=numpy.array([True, False, True])
print(a.mean())
# 0.6666666666666666b=torch.tensor([True, False, True])
print(b.mean())
# File "bug2.py", line 10, in <module># print(b.mean())# RuntimeError: Can only calculate the mean of floating types. Got Bool instead.
Mean on a bool tensor is a frequent way to compute accuracy. Of course, the workaround is to cast to float() before mean().