Calculates true positives for recall@k and 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`. Ar
(labels,
predictions_idx,
class_id=None,
weights=None,
name=None)
| 2281 | |
| 2282 | |
| 2283 | def _sparse_true_positive_at_k(labels, |
| 2284 | predictions_idx, |
| 2285 | class_id=None, |
| 2286 | weights=None, |
| 2287 | name=None): |
| 2288 | """Calculates true positives for recall@k and precision@k. |
| 2289 | |
| 2290 | If `class_id` is specified, calculate binary true positives for `class_id` |
| 2291 | only. |
| 2292 | If `class_id` is not specified, calculate metrics for `k` predicted vs |
| 2293 | `n` label classes, where `n` is the 2nd dimension of `labels_sparse`. |
| 2294 | |
| 2295 | Args: |
| 2296 | labels: `int64` `Tensor` or `SparseTensor` with shape |
| 2297 | [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of |
| 2298 | target classes for the associated prediction. Commonly, N=1 and `labels` |
| 2299 | has shape [batch_size, num_labels]. [D1, ... DN] must match |
| 2300 | `predictions_idx`. |
| 2301 | predictions_idx: 1-D or higher `int64` `Tensor` with last dimension `k`, |
| 2302 | top `k` predicted classes. For rank `n`, the first `n-1` dimensions must |
| 2303 | match `labels`. |
| 2304 | class_id: Class for which we want binary metrics. |
| 2305 | weights: `Tensor` whose rank is either 0, or n-1, where n is the rank of |
| 2306 | `labels`. If the latter, it must be broadcastable to `labels` (i.e., all |
| 2307 | dimensions must be either `1`, or the same as the corresponding `labels` |
| 2308 | dimension). |
| 2309 | name: Name of operation. |
| 2310 | |
| 2311 | Returns: |
| 2312 | A [D1, ... DN] `Tensor` of true positive counts. |
| 2313 | """ |
| 2314 | with ops.name_scope(name, 'true_positives', |
| 2315 | (predictions_idx, labels, weights)): |
| 2316 | labels, predictions_idx = _maybe_select_class_id(labels, predictions_idx, |
| 2317 | class_id) |
| 2318 | tp = sets.set_size(sets.set_intersection(predictions_idx, labels)) |
| 2319 | tp = math_ops.cast(tp, dtypes.float64) |
| 2320 | if weights is not None: |
| 2321 | with ops.control_dependencies((weights_broadcast_ops.assert_broadcastable( |
| 2322 | weights, tp),)): |
| 2323 | weights = math_ops.cast(weights, dtypes.float64) |
| 2324 | tp = math_ops.multiply(tp, weights) |
| 2325 | return tp |
| 2326 | |
| 2327 | |
| 2328 | def _streaming_sparse_true_positive_at_k(labels, |
no test coverage detected