Confusing result about meter.AverageValueMeter()

My test code:
import torch as t
from torchnet import meter

loss_meter = meter.AverageValueMeter()
a = t.Tensor([10])
b = t.Tensor([10])

but I get a wrong result:
tensor([ 15.])

The code for AverageValueMeter is a bit buggy if you are using tensors.

TLDR: You can use a.item() and b.item() and the code should work.

Long version:
There are no type checks in the method, so a tensor will be handled like an int or float.
Using .item() will return the pure Python float, so the code will work as intended.
However, if you pass tensors, you can see that the code holds a reference to the passed tensor in:

self.sum += value

(value is the passed tensor).
Now if we want to see what happens if we pass another tensor to it, we can add a print statement into the top of the else branch in add():

    print(self.mean, self.mean_old, value, n)
    self.mean = self.mean_old + ...

Although we just passed the new value (torch.tensor([10.])) to the method, self.mean will return 20! The reason for this error is because self.mean was assigned to self.sum, which was inplace modified by the last tensor, which in the end modifies self.mean.

A simple fix would be to add these lines at the beginning of add:

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

Thanks for your reply. :grin: