Calculates false positives for precision@k. If `class_id` is specified, calculate binary true positives for `class_id` only. If `class_id` is not specified, calculate metrics for `k` predicted vs `n` label classes, where `n` is the 2nd dimension of `labels_sparse`. Args: labe
(labels,
predictions_idx,
class_id=None,
weights=None)
| 3320 | |
| 3321 | |
| 3322 | def _sparse_false_positive_at_k(labels, |
| 3323 | predictions_idx, |
| 3324 | class_id=None, |
| 3325 | weights=None): |
| 3326 | """Calculates false positives for precision@k. |
| 3327 | |
| 3328 | If `class_id` is specified, calculate binary true positives for `class_id` |
| 3329 | only. |
| 3330 | If `class_id` is not specified, calculate metrics for `k` predicted vs |
| 3331 | `n` label classes, where `n` is the 2nd dimension of `labels_sparse`. |
| 3332 | |
| 3333 | Args: |
| 3334 | labels: `int64` `Tensor` or `SparseTensor` with shape |
| 3335 | [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of |
| 3336 | target classes for the associated prediction. Commonly, N=1 and `labels` |
| 3337 | has shape [batch_size, num_labels]. [D1, ... DN] must match |
| 3338 | `predictions_idx`. |
| 3339 | predictions_idx: 1-D or higher `int64` `Tensor` with last dimension `k`, |
| 3340 | top `k` predicted classes. For rank `n`, the first `n-1` dimensions must |
| 3341 | match `labels`. |
| 3342 | class_id: Class for which we want binary metrics. |
| 3343 | weights: `Tensor` whose rank is either 0, or n-1, where n is the rank of |
| 3344 | `labels`. If the latter, it must be broadcastable to `labels` (i.e., all |
| 3345 | dimensions must be either `1`, or the same as the corresponding `labels` |
| 3346 | dimension). |
| 3347 | |
| 3348 | Returns: |
| 3349 | A [D1, ... DN] `Tensor` of false positive counts. |
| 3350 | """ |
| 3351 | with ops.name_scope(None, 'false_positives', |
| 3352 | (predictions_idx, labels, weights)): |
| 3353 | labels, predictions_idx = _maybe_select_class_id(labels, predictions_idx, |
| 3354 | class_id) |
| 3355 | fp = sets.set_size( |
| 3356 | sets.set_difference(predictions_idx, labels, aminusb=True)) |
| 3357 | fp = math_ops.cast(fp, dtypes.float64) |
| 3358 | if weights is not None: |
| 3359 | with ops.control_dependencies((weights_broadcast_ops.assert_broadcastable( |
| 3360 | weights, fp),)): |
| 3361 | weights = math_ops.cast(weights, dtypes.float64) |
| 3362 | fp = math_ops.multiply(fp, weights) |
| 3363 | return fp |
| 3364 | |
| 3365 | |
| 3366 | def _streaming_sparse_false_positive_at_k(labels, |
no test coverage detected