8000 Support torch.mean for BoolTensors and other integer tensor inputs (without manual upcasting and hopefully without hidden upcasting) · Issue #45833 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content
Support torch.mean for BoolTensors and other integer tensor inputs (without manual upcasting and hopefully without hidden upcasting) #45833
@vadimkantorov

Description

@vadimkantorov

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

import torch
import numpy

print(torch.__version__)
# 1.6.0

a = numpy.array([True, False, True])
print(a.mean())
# 0.6666666666666666

b = 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().

cc @mruberry @rgommers @nairbv

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: numpyRelated to numpy support, and also numpy compatibility of our operatorsmodule: reductionsmodule: type promotionRelated to semantics of type promotiontriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0