class_docstr: Maintains the moving averages (i.e., the average of the latest N
  records) of (possibly multiple) fields.
  
  Fields are determined by the first call of :meth:`add`.
  
  Args:
      size (int, optional): The window size of moving average. If `None`,
          the average of all added records is maintained.
  
  Example:
  
      .. code-block:: python
  
          ## Use to maintain moving average of training loss
          avg_rec = AverageRecorder(size=10) # average over latest 10 records
          while training:
              loss_0, loss_1  = ...
              avg_rec.add([loss_0, loss_1])
              # avg_rec.avg() == [0.12343452, 0.567800323]
              # avg_rec.avg(0) == 0.12343452
              # avg_rec.to_str(precision=2, ) == '0.12 0.57'
  
          ## Use to maintain average of test metrics on the whole test set
          avg_rec = AverageRecorder() # average over ALL records
          while test:
              metric_0, metric_1  = ...
              avg_rec.add({'m0': metric_0, 'm1': metric_1}) # dict is allowed
          print(avg_rec.to_str(precision=4, delimiter=' , '))
          # 'm0: 0.1234 , m1: 0.5678'
          #
          # avg_rec.avg() == {'m0': 0.12343452, 'm1': 0.567800323}
          # avg_rec.avg(0) == 0.12343452
class_docstr_tok: ["Maintains","the","moving","averages","(","i.e.",",","the","average","of","the","latest","N","records",")","of","(","possibly","multiple",")","fields",".","Fields","are","determined","by","the","first","call","of",":","meth",":","`","add","`",".","Args",":","size","(","int",",","optional",")",":","The","window","size","of","moving","average",".","If","`","None","`",",","the","average","of","all","added","records","is","maintained",".","Example",":","..","code-block",":",":","python","#","#","Use","to","maintain","moving","average","of","training","loss","avg_rec","=","AverageRecorder","(","size=10",")","#","average","over","latest","10","records","while","training",":","loss_0",",","loss_1","=","...","avg_rec.add","(","[","loss_0",",","loss_1","]",")","#","avg_rec.avg","(",")","==","[","0.12343452",",","0.567800323","]","#","avg_rec.avg","(","0",")","==","0.12343452","#","avg_rec.to_str","(","precision=2",",",")","==","'","0.12","0.57'","#","#","Use","to","maintain","average","of","test","metrics","on","the","whole","test","set","avg_rec","=","AverageRecorder","(",")","#","average","over","ALL","records","while","test",":","metric_0",",","metric_1","=","...","avg_rec.add","(","{","'m0","'",":","metric_0",",","'m1","'",":","metric_1","}",")","#","dict","is","allowed","print","(","avg_rec.to_str","(","precision=4",",","delimiter=","'",",","'",")",")","#","'m0",":","0.1234",",","m1",":","0.5678'","#","#","avg_rec.avg","(",")","==","{","'m0","'",":","0.12343452",",","'m1","'",":","0.567800323","}","#","avg_rec.avg","(","0",")","==","0.12343452"]
docstr: Appends a new record.
  
  :attr:`record` can be a `list`, `dict`, or a single scalar. The
  record type is determined at the first time :meth:`add` is called.
  All subsequent calls to :meth:`add` must have the same type of
  :attr:`record`.
  
  :attr:`record` in subsequent calls to :meth:`add` can contain only
  a subset of fields than the first call to :meth:`add`.
  
  Example:
  
      .. code-block:: python
  
          recorder.add({'1': 0.2, '2': 0.2}) # 1st call to `add`
          x = recorder.add({'1': 0.4}) # 2nd call to `add`
          # x == {'1': 0.3, '2': 0.2}
  
  Args:
      record: A single scalar, a list of scalars, or a dict of scalars.
      weight (optional): A scalar, weight of the new record for
          calculating a weighted average. If `None`, weight is set to `1`.
          For example, :attr:`weight` can be set to batch size and
          :attr:`record` the average value of certain metrics on the batch
          in order to calculate the average metric values on a whole
          dataset.
  
  Returns:
      The (moving) average after appending the record, with the same
      type as :attr:`record`.
docstr_tok: ["Appends","a","new","record",".",":","attr",":","`","record","`","can","be","a","`","list","`",",","`","dict","`",",","or","a","single","scalar",".","The","record","type","is","determined","at","the","first","time",":","meth",":","`","add","`","is","called",".","All","subsequent","calls","to",":","meth",":","`","add","`","must","have","the","same","type","of",":","attr",":","`","record","`",".",":","attr",":","`","record","`","in","subsequent","calls","to",":","meth",":","`","add","`","can","contain","only","a","subset","of","fields","than","the","first","call","to",":","meth",":","`","add","`",".","Example",":","..","code-block",":",":","python","recorder.add","(","{","'","1","'",":","0.2",",","'","2","'",":","0.2","}",")","#","1st","call","to","`","add","`","x","=","recorder.add","(","{","'","1","'",":","0.4","}",")","#","2nd","call","to","`","add","`","#","x","==","{","'","1","'",":","0.3",",","'","2","'",":","0.2","}","Args",":","record",":","A","single","scalar",",","a","list","of","scalars",",","or","a","dict","of","scalars",".","weight","(","optional",")",":","A","scalar",",","weight","of","the","new","record","for","calculating","a","weighted","average",".","If","`","None","`",",","weight","is","set","to","`","1","`",".","For","example",",",":","attr",":","`","weight","`","can","be","set","to","batch","size","and",":","attr",":","`","record","`","the","average","value","of","certain","metrics","on","the","batch","in","order","to","calculate","the","average","metric","values","on","a","whole","dataset",".","Returns",":","The","(","moving",")","average","after","appending","the","record",",","with","the","same","type","as",":","attr",":","`","record","`","."]
code: def add(self, record, weight=None):
      """Appends a new record.
  
      :attr:`record` can be a `list`, `dict`, or a single scalar. The
      record type is determined at the first time :meth:`add` is called.
      All subsequent calls to :meth:`add` must have the same type of
      :attr:`record`.
  
      :attr:`record` in subsequent calls to :meth:`add` can contain only
      a subset of fields than the first call to :meth:`add`.
  
      Example:
  
          .. code-block:: python
  
              recorder.add({'1': 0.2, '2': 0.2}) # 1st call to `add`
              x = recorder.add({'1': 0.4}) # 2nd call to `add`
              # x == {'1': 0.3, '2': 0.2}
  
      Args:
          record: A single scalar, a list of scalars, or a dict of scalars.
          weight (optional): A scalar, weight of the new record for
              calculating a weighted average. If `None`, weight is set to `1`.
              For example, :attr:`weight` can be set to batch size and
              :attr:`record` the average value of certain metrics on the batch
              in order to calculate the average metric values on a whole
              dataset.
  
      Returns:
          The (moving) average after appending the record, with the same
          type as :attr:`record`.
      """
      if self._record_type is None:
          self._record_type = type(record)
      elif self._record_type != type(record):
          raise ValueError(
              "The type of `record` is not consistent. "
              "Expect type `{}`".format(self._record_type)
          )
  
      record_dict = self._to_dict(record)
      if self._recorders is None:
          self._recorders = {
              name: _SingleAverageRecorder(
                  self._size,
                  name if self._record_type == dict else None,
              )
              for name in record_dict.keys()
          }
  
      for name, val in record_dict.items():
          self._recorders[name].add(val, weight=weight)
  
      return self.avg()
  
file_path: texar/texar/tf/utils/average_recorder.py
imports: from collections import deque
label_desc: Use image node_id 3 for calling the AverageRecorder obj's underlying member method code with example usage: obj.add(record, weight) and returns: self