from tensorflow.python.framework import ops

      value matches `metric`.
  """
  default_name = _at_k_name('average_precision', k)
  with ops.name_scope(name, default_name, (predictions, labels)) as scope:
    # Calculate per-example average precision, and apply weights.
    average_precision = sparse_average_precision_at_k(
        predictions=predictions, labels=labels, k=k)
    if weights is not None:
      weights = math_ops.to_double(weights)
      average_precision = math_ops.mul(average_precision, weights)

    # Create accumulation variables and update ops for max average precision and
    # total average precision.
    with ops.name_scope(None, 'max', (average_precision,)) as max_scope:
      # `max` is the max possible precision. Since max for any row is 1.0:
      # - For the unweighted case, this is just the number of rows.
      # - For the weighted case, it's the sum of the weights broadcast across
      #   `average_precision` rows.
      max_var = contrib_variables.local_variable(
          array_ops.zeros([], dtype=dtypes.float64), name=max_scope)
      if weights is None:
        batch_max = math_ops.to_double(
            array_ops.size(average_precision, name='batch_max'))
      else:
        # TODO(ptucker): More efficient way to broadcast?
        broadcast_weights = math_ops.mul(
