A batch holds queries and their positive documents, and optionally one mined hard negative per query. Every document in the batch that is not query 's positive is a negative for it. Implement the loss with a temperature and with a mask for pairs that must not count as negatives.
similarity_logits(Q, P, N=None, tau=0.05)
Q, P have shape (B, d); N is (B, d) or None. Build the candidate matrix [P; N] (shape (B, d) or (2B, d)), compute cosine similarities between every query and every candidate, divide by tau, and return the (B, B) or (B, 2B) array rounded to 4 decimals.duplicate_mask(query_ids, positive_ids)
(B, B) array, True at [i, j] when i != j and either query_ids[i] == query_ids[j] or positive_ids[i] == positive_ids[j]: candidate j is a false negative for query i.infonce(Q, P, N=None, tau=0.05, mask=None)
mask is a (B, B) boolean array as above; masked entries are removed from the denominator by setting their logit to . Hard-negative columns are never masked. Return a float rounded to 4 decimals.positive_probabilities(Q, P, N=None, tau=0.05, mask=None)
(B,) array rounded to 4 decimals.Qn @ Cn.T.-inf entries become zero weight.