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: Returns the (moving) average.
  
  Args:
      id_or_name (optional): A list of or a single element.
          Each element is the index (if the record type is `list`) or
          name (if the record type is `dict`) of the field for which
          the average is calculated. If not given, the average of all
          fields are returned.
  
  Returns:
      The average value(s). If :attr:`id_or_name` is a single element
      (not a list), then returns the average value of the corresponding
      field. Otherwise, if :attr:`id_or_name` is a list of element(s),
      then returns average value(s) in the same type as :attr:`record`
      of :meth:`add`.
docstr_tok: ["Returns","the","(","moving",")","average",".","Args",":","id_or_name","(","optional",")",":","A","list","of","or","a","single","element",".","Each","element","is","the","index","(","if","the","record","type","is","`","list","`",")","or","name","(","if","the","record","type","is","`","dict","`",")","of","the","field","for","which","the","average","is","calculated",".","If","not","given",",","the","average","of","all","fields","are","returned",".","Returns",":","The","average","value","(","s",")",".","If",":","attr",":","`","id_or_name","`","is","a","single","element","(","not","a","list",")",",","then","returns","the","average","value","of","the","corresponding","field",".","Otherwise",",","if",":","attr",":","`","id_or_name","`","is","a","list","of","element","(","s",")",",","then","returns","average","value","(","s",")","in","the","same","type","as",":","attr",":","`","record","`","of",":","meth",":","`","add","`","."]
code: def avg(self, id_or_name=None):
      """Returns the (moving) average.
  
      Args:
          id_or_name (optional): A list of or a single element.
              Each element is the index (if the record type is `list`) or
              name (if the record type is `dict`) of the field for which
              the average is calculated. If not given, the average of all
              fields are returned.
  
      Returns:
          The average value(s). If :attr:`id_or_name` is a single element
          (not a list), then returns the average value of the corresponding
          field. Otherwise, if :attr:`id_or_name` is a list of element(s),
          then returns average value(s) in the same type as :attr:`record`
          of :meth:`add`.
      """
      if self._recorders is None:
          return 0.0
  
      keys = id_or_name
      if keys is None:
          keys = list(self._recorders.keys())
  
      if not isinstance(keys, (list, tuple)):
          return self._recorders[keys].avg()
  
      avg = {key: self._recorders[key].avg() for key in keys}
      if self._record_type in {list, tuple}:
          ret_avg = []
          for k, v in avg.items():
              if k in keys:
                  ret_avg.append(v)
          return self._record_type(ret_avg)
      elif self._record_type == dict:
          return avg
      else:
          return avg[self._default_metric_name]
  
file_path: texar/texar/tf/utils/average_recorder.py
imports: from collections import deque
label_desc: Use image node_id 4 for calling the AverageRecorder obj's underlying member method code with example usage: obj.avg(id_or_name) and returns: int, self, self, avg, avg