MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _num_relevant

Function _num_relevant

tensorflow/python/ops/metrics_impl.py:2956–2991  ·  view source on GitHub ↗

Computes number of relevant values for each row in labels. For labels with shape [D1, ... DN, num_labels], this is the minimum of `num_labels` and `k`. Args: labels: `int64` `Tensor` or `SparseTensor` with shape [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of

(labels, k)

Source from the content-addressed store, hash-verified

2954
2955
2956def _num_relevant(labels, k):
2957 """Computes number of relevant values for each row in labels.
2958
2959 For labels with shape [D1, ... DN, num_labels], this is the minimum of
2960 `num_labels` and `k`.
2961
2962 Args:
2963 labels: `int64` `Tensor` or `SparseTensor` with shape
2964 [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of
2965 target classes for the associated prediction. Commonly, N=1 and `labels`
2966 has shape [batch_size, num_labels].
2967 k: Integer, k for @k metric.
2968
2969 Returns:
2970 Integer `Tensor` of shape [D1, ... DN], where each value is the number of
2971 relevant values for that row.
2972
2973 Raises:
2974 ValueError: if inputs have invalid dtypes or values.
2975 """
2976 if k < 1:
2977 raise ValueError('Invalid k=%s.' % k)
2978 with ops.name_scope(None, 'num_relevant', (labels,)) as scope:
2979 # For SparseTensor, calculate separate count for each row.
2980 labels = sparse_tensor.convert_to_tensor_or_sparse_tensor(labels)
2981 if isinstance(labels, sparse_tensor.SparseTensor):
2982 return math_ops.minimum(sets.set_size(labels), k, name=scope)
2983
2984 # The relevant values for each (d1, ... dN) is the minimum of k and the
2985 # number of labels along the last dimension that are non-negative.
2986 num_labels = math_ops.reduce_sum(
2987 array_ops.where_v2(math_ops.greater_equal(labels, 0),
2988 array_ops.ones_like(labels),
2989 array_ops.zeros_like(labels)),
2990 axis=-1)
2991 return math_ops.minimum(num_labels, k, name=scope)
2992
2993
2994def _sparse_average_precision_at_top_k(labels, predictions_idx):

Callers 1

Calls 4

minimumMethod · 0.80
reduce_sumMethod · 0.80
name_scopeMethod · 0.45
set_sizeMethod · 0.45

Tested by

no test coverage detected