class_docstr: Maintains the moving average (i.e., the average of the latest N records)
  of a single metric.
  
  Args:
      size (int, optional): The window size of moving average. If `None`,
          the average of all added records is maintained.
      name (str, optional): name of the recorder. Used when printing.
class_docstr_tok: ["Maintains","the","moving","average","(","i.e.",",","the","average","of","the","latest","N","records",")","of","a","single","metric",".","Args",":","size","(","int",",","optional",")",":","The","window","size","of","moving","average",".","If","`","None","`",",","the","average","of","all","added","records","is","maintained",".","name","(","str",",","optional",")",":","name","of","the","recorder",".","Used","when","printing","."]
docstr: Appends a new record.
  
  Args:
      record: A scalar; the new record to append.
      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 metric on the batch
          in order to calculate the average metric value on a whole
          dataset.
  
  Returns:
      The (moving) average after appending the record.
docstr_tok: ["Appends","a","new","record",".","Args",":","record",":","A","scalar",";","the","new","record","to","append",".","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","metric","on","the","batch","in","order","to","calculate","the","average","metric","value","on","a","whole","dataset",".","Returns",":","The","(","moving",")","average","after","appending","the","record","."]
code: def add(self, record, weight=None):
      """Appends a new record.
  
      Args:
          record: A scalar; the new record to append.
          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 metric on the batch
              in order to calculate the average metric value on a whole
              dataset.
  
      Returns:
          The (moving) average after appending the record.
      """
      w = weight if weight is not None else 1
      self._w_sum += w
      self._sum += record * w
  
      if self._size is not None:
          if len(self._q) == self._size:
              w_pop = self._w.popleft()
              self._sum -= self._q.popleft() * w_pop
              self._w_sum -= w_pop
          self._q.append(record)
          self._w.append(w)
  
      return self.avg()
  
file_path: texar/texar/tf/utils/average_recorder.py
imports: from collections import deque
label_desc: Use image node_id 2 for calling the _SingleAverageRecorder obj's underlying member method code with example usage: obj.add(record, weight) and returns: self