Skip to content

Latest commit

 

History

History
210 lines (158 loc) · 4.43 KB

File metadata and controls

210 lines (158 loc) · 4.43 KB

TFSimilarity.retrieval_metrics.MapAtK

Mean Average precision - mAP@K is computed as.

Inherits From: RetrievalMetric, ABC

TFSimilarity.retrieval_metrics.MapAtK(
    r: Mapping[int, int],
    name: str = map,
    k: int = 5,
    average: str = micro,
    **kwargs
) -> None

$$ mAP_i@K = \frac\sum_{j = 1}^{K} {rel_i_j}\times{P_i@j}}{R $$

Where: K is the number of neighbors in the i_th query result set. P is the rolling precision over the i_th query result set. R is the cardinality of the target class. rel is the relevance mask (indicator function) for the i_th query. i represents the i_th query. j represents the j_th ranked query result.

AP@K is biased towards the top ranked results and is a function of the rank (K), the relevancy mask (rel), and the number of indexed examples for the class (R). The denominator for the i_th query is set to the number of indexed examples (R) for the class associated with the i_th query.

For example, if the index has has 100 embedded examples (R) of class 'a', and our query returns 50 results (K) where the top 10 results are all TPs, then the AP@50 will be 0.10; however, if instead the bottom 10 ranked results are all TPs, then the AP@50 will be much lower (0.012) because we apply a penalty for the 40 FPs that come before the relevant query results.

This metric is useful when we want to ensure that the top ranked results are relevant to the query; however, it requires that we pass a mapping from the class id to the number of indexed examples for that class.

Args

r A mapping from class id to the number of examples in the index, e.g., r[4] = 10 represents 10 indexed examples from class 4.
name Name associated with the metric object, e.g., avg_precision@5
canonical_name The canonical name associated with metric, e.g., avg_precision@K
k The number of nearest neighbors over which the metric is computed.
distance_threshold The max distance below which a nearest neighbor is considered a valid match.
average 'micro' Determines the type of averaging performed over the queries.
  • 'micro': Calculates metrics globally over all queries.

Attributes

name

Methods

compute

View source

compute(
    *,
    query_labels: <a href="../../TFSimilarity/callbacks/IntTensor.md">TFSimilarity.callbacks.IntTensor```
</a>,
    match_mask: <a href="../../TFSimilarity/utils/BoolTensor.md">TFSimilarity.utils.BoolTensor```
</a>,
    **kwargs
) -> <a href="../../TFSimilarity/callbacks/FloatTensor.md">TFSimilarity.callbacks.FloatTensor```
</a>

Compute the metric

Args
query_labels A 1D array of the labels associated with the embedding queries.
match_mask A 2D mask where a 1 indicates a match between the jth query and the kth neighbor and a 0 indicates a mismatch.
**kwargs Additional compute args
Returns
A rank 0 tensor containing the metric.

get_config

View source

get_config()