(features, outputs)
Get eval metrics for estimator in the pretraining stage.
Get eval metrics for estimator in the pretraining stage.
Get eval metrics for estimator in the pretraining stage .
def pretrain_eval_metrics_fn(features, outputs):
  """Get eval metrics for estimator in the pretraining stage."""
  targets_weights = features.get("targets_mask", None)
  if targets_weights is None:
    targets_weights = 1.0 - tf.cast(
        tf.equal(features["targets"], 0), tf.float32)
  else:
    targets_weights = tf.cast(targets_weights, tf.float32)

  masked_inputs_weights = features.get("masked_inputs_mask", None)

  if "logits_mlm" in outputs:
    if masked_inputs_weights is None:
      masked_inputs_weights = 1.0 - tf.cast(
          tf.equal(features["masked_inputs"], 0), tf.float32)
    else:
      masked_inputs_weights = tf.cast(masked_inputs_weights, tf.float32)

    def _create_eval_metrics(targets, weights, logits, masked_inputs,
                             weights_mlm, logits_mlm):
      """Returns a map of metric names to metric tensors."""
      metrics = _create_generative_metrics(targets, weights, logits)
      metrics_mlm = _create_generative_metrics(masked_inputs, weights_mlm,
                                               logits_mlm)
      metrics.update({k + "_mlm": v for k, v in metrics_mlm.items()})
      return metrics

    if "masked_inputs" not in features:
      raise KeyError(
          "'masked_inputs' not found in features. "
          "Please check TransformerEncoderDecoderMLModel when MLM is applied.")

    return (_create_eval_metrics, [
        features["targets"], targets_weights, outputs["logits"],
        features["masked_inputs"], masked_inputs_weights, outputs["logits_mlm"]
    ])
  else:
    return (_create_generative_metrics,
            [features["targets"], targets_weights, outputs["logits"]])
def pretrain_eval_metrics_fn ( features , outputs ) : targets_weights = features . get ( "targets_mask" , None ) if targets_weights is None : targets_weights = 1.0 - tf . cast ( tf . equal ( features [ "targets" ] , 0 ) , tf . float32 ) else : targets_weights = tf . cast ( targets_weights , tf . float32 ) masked_inputs_weights = features . get ( "masked_inputs_mask" , None ) if "logits_mlm" in outputs : if masked_inputs_weights is None : masked_inputs_weights = 1.0 - tf . cast ( tf . equal ( features [ "masked_inputs" ] , 0 ) , tf . float32 ) else : masked_inputs_weights = tf . cast ( masked_inputs_weights , tf . float32 ) def _create_eval_metrics ( targets , weights , logits , masked_inputs , weights_mlm , logits_mlm ) : """Returns a map of metric names to metric tensors.""" metrics = _create_generative_metrics ( targets , weights , logits ) metrics_mlm = _create_generative_metrics ( masked_inputs , weights_mlm , logits_mlm ) metrics . update ( { k + "_mlm" : v for k , v in metrics_mlm . items ( ) } ) return metrics if "masked_inputs" not in features : raise KeyError ( "'masked_inputs' not found in features. " "Please check TransformerEncoderDecoderMLModel when MLM is applied." ) return ( _create_eval_metrics , [ features [ "targets" ] , targets_weights , outputs [ "logits" ] , features [ "masked_inputs" ] , masked_inputs_weights , outputs [ "logits_mlm" ] ] ) else : return ( _create_generative_metrics , [ features [ "targets" ] , targets_weights , outputs [ "logits" ] ] )