def document_frequency_to_idf(document_frequency: tf.Tensor,
                              corpus_size: Union[int, tf.Tensor],
                              smooth: bool = True,
                              add_baseline: bool = True) -> tf.Tensor:
  """Computes inverse document frequency given document frequency.

  The inverse document frequency of a term, by default, is calculated as
  1 + log ((corpus size + 1) / (document frequency + 1)), where document
  frequency is the number of documents that contain this term.

  Args:
    document_frequency: A tensor storing the document frequency of each term.
    corpus_size: An int or int scalar tensor representing the size of the entire
      dataset, i.e., number of examples.
    smooth: A bool indicating if the inverse document frequency should be
      smoothed. If True, which is the default, then the idf is calculated as 1 +
      log((corpus size + 1) / (document frequency of term + 1)). Otherwise, the
      idf is 1 + log((corpus size) / (document frequency of term)), which could
      result in a division by zero error.
    add_baseline: A bool indicating if the inverse document frequency should be
      added with a constant baseline 1.0. If True, which is the default, then
      the idf is calculated as 1 + log(*). Otherwise, the idf is log(*) without
      the constant 1 baseline. Keeping the baseline reduces the discrepancy in
      idf between commonly seen terms and rare terms.

  Returns:
    A tensor of the inverse document frequency of input document frequency.
  """
  baseline = 1.0 if add_baseline else 0.0
  if smooth:
    return (
        tf.math.log(
            (tf.cast(corpus_size, dtype=tf.float32) + 1.0)
            / (1.0 + tf.cast(document_frequency, dtype=tf.float32))
        )
        + baseline
    )
  else:
    return (
        tf.math.log(
            tf.cast(corpus_size, dtype=tf.float32)
            / (tf.cast(document_frequency, dtype=tf.float32))
        )
        + baseline
    )
Computes inverse document frequency given document frequency.

  The inverse document frequency of a term, by default, is calculated as
  1 + log ((corpus size + 1) / (document frequency + 1)), where document
  frequency is the number of documents that contain this term.

  Args:
    document_frequency: A tensor storing the document frequency of each term.
    corpus_size: An int or int scalar tensor representing the size of the entire
      dataset, i.e., number of examples.
    smooth: A bool indicating if the inverse document frequency should be
      smoothed. If True, which is the default, then the idf is calculated as 1 +
      log((corpus size + 1) / (document frequency of term + 1)). Otherwise, the
      idf is 1 + log((corpus size) / (document frequency of term)), which could
      result in a division by zero error.
    add_baseline: A bool indicating if the inverse document frequency should be
      added with a constant baseline 1.0. If True, which is the default, then
      the idf is calculated as 1 + log(*). Otherwise, the idf is log(*) without
      the constant 1 baseline. Keeping the baseline reduces the discrepancy in
      idf between commonly seen terms and rare terms.

  Returns:
    A tensor of the inverse document frequency of input document frequency.
  
tensorflow_transform/tf_utils.py