parameters: (variables_to_update,
                                        y_true,
                                        y_pred,
                                        thresholds,
                                        top_k=None,
                                        class_id=None,
                                        sample_weight=None)
return_statement: return control_flow_ops.group(update_ops)
docstring: Returns op to update the given confusion matrix variables.
  
    For every pair of values in y_true and y_pred:
  
    true_positive: y_true == True and y_pred > thresholds
    false_negatives: y_true == True and y_pred <= thresholds
    true_negatives: y_true == False and y_pred <= thresholds
    false_positive: y_true == False and y_pred > thresholds
  
    The results will be weighted and added together. When multiple thresholds are
    provided, we will repeat the same for every threshold.
  
    For estimation of these metrics over a stream of data, the function creates an
    `update_op` operation that updates the given variables.
  
    If `sample_weight` is `None`, weights default to 1.
    Use weights of 0 to mask values.
  
    Args:
      variables_to_update: Dictionary with 'tp', 'fn', 'tn', 'fp' as valid keys
        and corresponding variables to update as values.
      y_true: A `Tensor` whose shape matches `y_pred`. Will be cast to `bool`.
      y_pred: A floating point `Tensor` of arbitrary shape and whose values are in
        the range `[0, 1]`.
      thresholds: A float value or a python list or tuple of float thresholds in
        `[0, 1]`, or NEG_INF (used when top_k is set).
      top_k: Optional int, indicates that the positive labels should be limited to
        the top k predictions.
      class_id: Optional int, limits the prediction and labels to the class
        specified by this argument.
      sample_weight: Optional `Tensor` whose rank is either 0, or the same rank as
        `y_true`, and must be broadcastable to `y_true` (i.e., all dimensions must
        be either `1`, or the same as the corresponding `y_true` dimension).
  
    Returns:
      Update op.
  
    Raises:
      ValueError: If `y_pred` and `y_true` have mismatched shapes, or if
        `sample_weight` is not `None` and its shape doesn't match `y_pred`, or if
        `variables_to_update` contains invalid keys.
docstring_summary: Returns op to update the given confusion matrix variables.
docstring_tokens: Returns op to update the given confusion matrix variables .
function: def update_confusion_matrix_variables(variables_to_update,
                                        y_true,
                                        y_pred,
                                        thresholds,
                                        top_k=None,
                                        class_id=None,
                                        sample_weight=None):
    """Returns op to update the given confusion matrix variables.
  
    For every pair of values in y_true and y_pred:
  
    true_positive: y_true == True and y_pred > thresholds
    false_negatives: y_true == True and y_pred <= thresholds
    true_negatives: y_true == False and y_pred <= thresholds
    false_positive: y_true == False and y_pred > thresholds
  
    The results will be weighted and added together. When multiple thresholds are
    provided, we will repeat the same for every threshold.
  
    For estimation of these metrics over a stream of data, the function creates an
    `update_op` operation that updates the given variables.
  
    If `sample_weight` is `None`, weights default to 1.
    Use weights of 0 to mask values.
  
    Args:
      variables_to_update: Dictionary with 'tp', 'fn', 'tn', 'fp' as valid keys
        and corresponding variables to update as values.
      y_true: A `Tensor` whose shape matches `y_pred`. Will be cast to `bool`.
      y_pred: A floating point `Tensor` of arbitrary shape and whose values are in
        the range `[0, 1]`.
      thresholds: A float value or a python list or tuple of float thresholds in
        `[0, 1]`, or NEG_INF (used when top_k is set).
      top_k: Optional int, indicates that the positive labels should be limited to
        the top k predictions.
      class_id: Optional int, limits the prediction and labels to the class
        specified by this argument.
      sample_weight: Optional `Tensor` whose rank is either 0, or the same rank as
        `y_true`, and must be broadcastable to `y_true` (i.e., all dimensions must
        be either `1`, or the same as the corresponding `y_true` dimension).
  
    Returns:
      Update op.
  
    Raises:
      ValueError: If `y_pred` and `y_true` have mismatched shapes, or if
        `sample_weight` is not `None` and its shape doesn't match `y_pred`, or if
        `variables_to_update` contains invalid keys.
    """
    if variables_to_update is None:
      return
    y_true = math_ops.cast(y_true, dtype=dtypes.float32)
    y_pred = math_ops.cast(y_pred, dtype=dtypes.float32)
    [y_pred,
     y_true], _ = ragged_assert_compatible_and_get_flat_values([y_pred, y_true],
                                                               sample_weight)
    y_pred.shape.assert_is_compatible_with(y_true.shape)
  
    if not any(
        key for key in variables_to_update if key in list(ConfusionMatrix)):
      raise ValueError(
          'Please provide at least one valid confusion matrix '
          'variable to update. Valid variable key options are: "{}". '
          'Received: "{}"'.format(
              list(ConfusionMatrix), variables_to_update.keys()))
  
    invalid_keys = [
        key for key in variables_to_update if key not in list(ConfusionMatrix)
    ]
    if invalid_keys:
      raise ValueError(
          'Invalid keys: {}. Valid variable key options are: "{}"'.format(
              invalid_keys, list(ConfusionMatrix)))
  
    with ops.control_dependencies([
        check_ops.assert_greater_equal(
            y_pred,
            math_ops.cast(0.0, dtype=y_pred.dtype),
            message='predictions must be >= 0'),
        check_ops.assert_less_equal(
            y_pred,
            math_ops.cast(1.0, dtype=y_pred.dtype),
            message='predictions must be <= 1')
    ]):
      if sample_weight is None:
        y_pred, y_true = tf_losses_utils.squeeze_or_expand_dimensions(
            y_pred, y_true)
      else:
        y_pred, y_true, sample_weight = (
            tf_losses_utils.squeeze_or_expand_dimensions(
                y_pred, y_true, sample_weight=sample_weight))
  
    if top_k is not None:
      y_pred = _filter_top_k(y_pred, top_k)
    if class_id is not None:
      y_true = y_true[..., class_id]
      y_pred = y_pred[..., class_id]
  
    thresholds = to_list(thresholds)
    num_thresholds = len(thresholds)
    num_predictions = array_ops.size(y_pred)
  
    # Reshape predictions and labels.
    predictions_2d = array_ops.reshape(y_pred, [1, -1])
    labels_2d = array_ops.reshape(
        math_ops.cast(y_true, dtype=dtypes.bool), [1, -1])
  
    # Tile the thresholds for every prediction.
    thresh_tiled = array_ops.tile(
        array_ops.expand_dims(array_ops.constant(thresholds), 1),
        array_ops.stack([1, num_predictions]))
  
    # Tile the predictions for every threshold.
    preds_tiled = array_ops.tile(predictions_2d, [num_thresholds, 1])
  
    # Compare predictions and threshold.
    pred_is_pos = math_ops.greater(preds_tiled, thresh_tiled)
  
    # Tile labels by number of thresholds
    label_is_pos = array_ops.tile(labels_2d, [num_thresholds, 1])
  
    if sample_weight is not None:
      weights = weights_broadcast_ops.broadcast_weights(
          math_ops.cast(sample_weight, dtype=dtypes.float32), y_pred)
      weights_tiled = array_ops.tile(
          array_ops.reshape(weights, [1, -1]), [num_thresholds, 1])
    else:
      weights_tiled = None
  
    update_ops = []
  
    def weighted_assign_add(label, pred, weights, var):
      label_and_pred = math_ops.cast(
          math_ops.logical_and(label, pred), dtype=dtypes.float32)
      if weights is not None:
        label_and_pred *= weights
      return var.assign_add(math_ops.reduce_sum(label_and_pred, 1))
  
    loop_vars = {
        ConfusionMatrix.TRUE_POSITIVES: (label_is_pos, pred_is_pos),
    }
    update_tn = ConfusionMatrix.TRUE_NEGATIVES in variables_to_update
    update_fp = ConfusionMatrix.FALSE_POSITIVES in variables_to_update
    update_fn = ConfusionMatrix.FALSE_NEGATIVES in variables_to_update
  
    if update_fn or update_tn:
      pred_is_neg = math_ops.logical_not(pred_is_pos)
      loop_vars[ConfusionMatrix.FALSE_NEGATIVES] = (label_is_pos, pred_is_neg)
  
    if update_fp or update_tn:
      label_is_neg = math_ops.logical_not(label_is_pos)
      loop_vars[ConfusionMatrix.FALSE_POSITIVES] = (label_is_neg, pred_is_pos)
      if update_tn:
        loop_vars[ConfusionMatrix.TRUE_NEGATIVES] = (label_is_neg, pred_is_neg)
  
    for matrix_cond, (label, pred) in loop_vars.items():
      if matrix_cond in variables_to_update:
        update_ops.append(
            weighted_assign_add(label, pred, weights_tiled,
                                variables_to_update[matrix_cond]))
    return control_flow_ops.group(update_ops)
function_tokens: def update_confusion_matrix_variables ( variables_to_update , y_true , y_pred , thresholds , top_k = None , class_id = None , sample_weight = None ) : if variables_to_update is None : return y_true = math_ops . cast ( y_true , dtype = dtypes . float32 ) y_pred = math_ops . cast ( y_pred , dtype = dtypes . float32 ) [ y_pred , y_true ] , _ = ragged_assert_compatible_and_get_flat_values ( [ y_pred , y_true ] , sample_weight ) y_pred . shape . assert_is_compatible_with ( y_true . shape ) if not any ( key for key in variables_to_update if key in list ( ConfusionMatrix ) ) : raise ValueError ( 'Please provide at least one valid confusion matrix ' 'variable to update. Valid variable key options are: "{}". ' 'Received: "{}"' . format ( list ( ConfusionMatrix ) , variables_to_update . keys ( ) ) ) invalid_keys = [ key for key in variables_to_update if key not in list ( ConfusionMatrix ) ] if invalid_keys : raise ValueError ( 'Invalid keys: {}. Valid variable key options are: "{}"' . format ( invalid_keys , list ( ConfusionMatrix ) ) ) with ops . control_dependencies ( [ check_ops . assert_greater_equal ( y_pred , math_ops . cast ( 0.0 , dtype = y_pred . dtype ) , message = 'predictions must be >= 0' ) , check_ops . assert_less_equal ( y_pred , math_ops . cast ( 1.0 , dtype = y_pred . dtype ) , message = 'predictions must be <= 1' ) ] ) : if sample_weight is None : y_pred , y_true = tf_losses_utils . squeeze_or_expand_dimensions ( y_pred , y_true ) else : y_pred , y_true , sample_weight = ( tf_losses_utils . squeeze_or_expand_dimensions ( y_pred , y_true , sample_weight = sample_weight ) ) if top_k is not None : y_pred = _filter_top_k ( y_pred , top_k ) if class_id is not None : y_true = y_true [ ... , class_id ] y_pred = y_pred [ ... , class_id ] thresholds = to_list ( thresholds ) num_thresholds = len ( thresholds ) num_predictions = array_ops . size ( y_pred ) # Reshape predictions and labels. predictions_2d = array_ops . reshape ( y_pred , [ 1 , - 1 ] ) labels_2d = array_ops . reshape ( math_ops . cast ( y_true , dtype = dtypes . bool ) , [ 1 , - 1 ] ) # Tile the thresholds for every prediction. thresh_tiled = array_ops . tile ( array_ops . expand_dims ( array_ops . constant ( thresholds ) , 1 ) , array_ops . stack ( [ 1 , num_predictions ] ) ) # Tile the predictions for every threshold. preds_tiled = array_ops . tile ( predictions_2d , [ num_thresholds , 1 ] ) # Compare predictions and threshold. pred_is_pos = math_ops . greater ( preds_tiled , thresh_tiled ) # Tile labels by number of thresholds label_is_pos = array_ops . tile ( labels_2d , [ num_thresholds , 1 ] ) if sample_weight is not None : weights = weights_broadcast_ops . broadcast_weights ( math_ops . cast ( sample_weight , dtype = dtypes . float32 ) , y_pred ) weights_tiled = array_ops . tile ( array_ops . reshape ( weights , [ 1 , - 1 ] ) , [ num_thresholds , 1 ] ) else : weights_tiled = None update_ops = [ ] def weighted_assign_add ( label , pred , weights , var ) : label_and_pred = math_ops . cast ( math_ops . logical_and ( label , pred ) , dtype = dtypes . float32 ) if weights is not None : label_and_pred *= weights return var . assign_add ( math_ops . reduce_sum ( label_and_pred , 1 ) ) loop_vars = { ConfusionMatrix . TRUE_POSITIVES : ( label_is_pos , pred_is_pos ) , } update_tn = ConfusionMatrix . TRUE_NEGATIVES in variables_to_update update_fp = ConfusionMatrix . FALSE_POSITIVES in variables_to_update update_fn = ConfusionMatrix . FALSE_NEGATIVES in variables_to_update if update_fn or update_tn : pred_is_neg = math_ops . logical_not ( pred_is_pos ) loop_vars [ ConfusionMatrix . FALSE_NEGATIVES ] = ( label_is_pos , pred_is_neg ) if update_fp or update_tn : label_is_neg = math_ops . logical_not ( label_is_pos ) loop_vars [ ConfusionMatrix . FALSE_POSITIVES ] = ( label_is_neg , pred_is_pos ) if update_tn : loop_vars [ ConfusionMatrix . TRUE_NEGATIVES ] = ( label_is_neg , pred_is_neg ) for matrix_cond , ( label , pred ) in loop_vars . items ( ) : if matrix_cond in variables_to_update : update_ops . append ( weighted_assign_add ( label , pred , weights_tiled , variables_to_update [ matrix_cond ] ) ) return control_flow_ops . group ( update_ops )