pytorch/tnt

AverageValueMeter returns incorrect results when `tensor` is passed

ptrblck opened this issue · 1 comments

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.

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!