Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AverageValueMeter returns incorrect results when tensor is passed #100

Closed
ptrblck opened this issue Jul 27, 2018 · 1 comment
Closed

AverageValueMeter returns incorrect results when tensor is passed #100

ptrblck opened this issue Jul 27, 2018 · 1 comment

Comments

@ptrblck
Copy link

ptrblck commented Jul 27, 2018

Based on this thread it seems as if the internal members of the class hold references to the tensors, thus yielding wrong results.
When the tensor value is passed by .item() the result is correct.
A simple fix would be to add this condition to add:

def add(self, value, n=1):
    if isinstance(value, torch.Tensor):
        value = value.item()
    self.val = value

I can submit a PR, if that makes sense to you.

@alexsax
Copy link
Contributor

alexsax commented Jul 29, 2018

Thanks for reporting this, and I agree that this is caused by the internal members holding references.

AverageValueMeter is spec'ed to support tensors and numpy arrays element-wise. So the correct output for the thread that you linked should be Tensor([[10.]]) (like a single-pixel image). Supporting tensors like this is desirable if, for example, you wanted to get the mean image in a dataset in order to whiten the inputs.

I think that an earlier contribution broke this tensor-supporting behavior (understandable, because I neither documented the specifications nor added tests!).

I just pushed a fix and a couple tests to fix the bug. Any documentation or other improvements would be most welcome. Thanks again for posting the issue!

@alexsax alexsax closed this as completed Jul 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants