prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  AverageValueMeter returns incorrect results when `tensor` is passed
  Based on [this thread](https://discuss.pytorch.org/t/confusing-result-about-meter-averagevaluemeter/21819) 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`:
  ```python
  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.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `torchnet/meter/averagevaluemeter.py`
  Content:
  ```
  1 import math
  2 from . import meter
  3 import numpy as np
  4 
  5 
  6 class AverageValueMeter(meter.Meter):
  7     def __init__(self):
  8         super(AverageValueMeter, self).__init__()
  9         self.reset()
  10         self.val = 0
  11 
  12     def add(self, value, n=1):
  13         self.val = value
  14         self.sum += value
  15         self.var += value * value
  16         self.n += n
  17 
  18         if self.n == 0:
  19             self.mean, self.std = np.nan, np.nan
  20         elif self.n == 1:
  21             self.mean, self.std = self.sum, np.inf
  22             self.mean_old = self.mean
  23             self.m_s = 0.0
  24         else:
  25             self.mean = self.mean_old + (value - n * self.mean_old) / float(self.n)
  26             self.m_s += (value - self.mean_old) * (value - self.mean)
  27             self.mean_old = self.mean
  28             self.std = math.sqrt(self.m_s / (self.n - 1.0))
  29 
  30     def value(self):
  31         return self.mean, self.std
  32 
  33     def reset(self):
  34         self.n = 0
  35         self.sum = 0.0
  36         self.var = 0.0
  37         self.val = 0.0
  38         self.mean = np.nan
  39         self.mean_old = 0.0
  40         self.m_s = 0.0
  41         self.std = np.nan
  42
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/torchnet/meter/averagevaluemeter.py b/torchnet/meter/averagevaluemeter.py
  --- a/torchnet/meter/averagevaluemeter.py
  +++ b/torchnet/meter/averagevaluemeter.py
  @@ -18,14 +18,15 @@
           if self.n == 0:
               self.mean, self.std = np.nan, np.nan
           elif self.n == 1:
  -            self.mean, self.std = self.sum, np.inf
  +            self.mean = 0.0 + self.sum  # This is to force a copy in torch/numpy
  +            self.std = np.inf
               self.mean_old = self.mean
               self.m_s = 0.0
           else:
               self.mean = self.mean_old + (value - n * self.mean_old) / float(self.n)
               self.m_s += (value - self.mean_old) * (value - self.mean)
               self.mean_old = self.mean
  -            self.std = math.sqrt(self.m_s / (self.n - 1.0))
  +            self.std = np.sqrt(self.m_s / (self.n - 1.0))
   
       def value(self):
           return self.mean, self.std
  
verification_info: {"golden_diff": "diff --git a/torchnet/meter/averagevaluemeter.py b/torchnet/meter/averagevaluemeter.py\n--- a/torchnet/meter/averagevaluemeter.py\n+++ b/torchnet/meter/averagevaluemeter.py\n@@ -18,14 +18,15 @@\n         if self.n == 0:\n             self.mean, self.std = np.nan, np.nan\n         elif self.n == 1:\n-            self.mean, self.std = self.sum, np.inf\n+            self.mean = 0.0 + self.sum  # This is to force a copy in torch/numpy\n+            self.std = np.inf\n             self.mean_old = self.mean\n             self.m_s = 0.0\n         else:\n             self.mean = self.mean_old + (value - n * self.mean_old) / float(self.n)\n             self.m_s += (value - self.mean_old) * (value - self.mean)\n             self.mean_old = self.mean\n-            self.std = math.sqrt(self.m_s / (self.n - 1.0))\n+            self.std = np.sqrt(self.m_s / (self.n - 1.0))\n \n     def value(self):\n         return self.mean, self.std\n", "issue": "AverageValueMeter returns incorrect results when `tensor` is passed\nBased on [this thread](https://discuss.pytorch.org/t/confusing-result-about-meter-averagevaluemeter/21819) it seems as if the internal members of the class hold references to the `tensors`, thus yielding wrong results.\r\nWhen the `tensor` value is passed by `.item()` the result is correct.\r\nA simple fix would be to add this condition to `add`:\r\n```python\r\ndef add(self, value, n=1):\r\n    if isinstance(value, torch.Tensor):\r\n        value = value.item()\r\n    self.val = value\r\n```\r\n\r\nI can submit a PR, if that makes sense to you.\n", "before_files": [{"content": "import math\nfrom . import meter\nimport numpy as np\n\n\nclass AverageValueMeter(meter.Meter):\n    def __init__(self):\n        super(AverageValueMeter, self).__init__()\n        self.reset()\n        self.val = 0\n\n    def add(self, value, n=1):\n        self.val = value\n        self.sum += value\n        self.var += value * value\n        self.n += n\n\n        if self.n == 0:\n            self.mean, self.std = np.nan, np.nan\n        elif self.n == 1:\n            self.mean, self.std = self.sum, np.inf\n            self.mean_old = self.mean\n            self.m_s = 0.0\n        else:\n            self.mean = self.mean_old + (value - n * self.mean_old) / float(self.n)\n            self.m_s += (value - self.mean_old) * (value - self.mean)\n            self.mean_old = self.mean\n            self.std = math.sqrt(self.m_s / (self.n - 1.0))\n\n    def value(self):\n        return self.mean, self.std\n\n    def reset(self):\n        self.n = 0\n        self.sum = 0.0\n        self.var = 0.0\n        self.val = 0.0\n        self.mean = np.nan\n        self.mean_old = 0.0\n        self.m_s = 0.0\n        self.std = np.nan\n", "path": "torchnet/meter/averagevaluemeter.py"}], "after_files": [{"content": "import math\nfrom . import meter\nimport numpy as np\n\n\nclass AverageValueMeter(meter.Meter):\n    def __init__(self):\n        super(AverageValueMeter, self).__init__()\n        self.reset()\n        self.val = 0\n\n    def add(self, value, n=1):\n        self.val = value\n        self.sum += value\n        self.var += value * value\n        self.n += n\n\n        if self.n == 0:\n            self.mean, self.std = np.nan, np.nan\n        elif self.n == 1:\n            self.mean = 0.0 + self.sum  # This is to force a copy in torch/numpy\n            self.std = np.inf\n            self.mean_old = self.mean\n            self.m_s = 0.0\n        else:\n            self.mean = self.mean_old + (value - n * self.mean_old) / float(self.n)\n            self.m_s += (value - self.mean_old) * (value - self.mean)\n            self.mean_old = self.mean\n            self.std = np.sqrt(self.m_s / (self.n - 1.0))\n\n    def value(self):\n        return self.mean, self.std\n\n    def reset(self):\n        self.n = 0\n        self.sum = 0.0\n        self.var = 0.0\n        self.val = 0.0\n        self.mean = np.nan\n        self.mean_old = 0.0\n        self.m_s = 0.0\n        self.std = np.nan\n", "path": "torchnet/meter/averagevaluemeter.py"}]}