Skip to content
DeepForgeby svx
All problems
HardNLPnlp-149

Hard Negative Mining

Problem

Mine hard negatives for a query. negatives is a list of [id, vector] pairs, and the positive is a vector.

Keep negatives whose dot score with query is strictly below the positive's dot score, rank them by descending score with alphabetical tie-breaking, and return the top n ids.

Hint

Scored at or above the positive are false negatives, so exclude them.

Test cases

case 1
in: [[1,0],[1,0],[["n1",[0.5,0]],["n2",[2,0]],["n3",[0.9,0]]],2]
expected: ["n3","n1"]
case 2
in: [[1,0],[1,0],[["a",[1,0]]],1]
expected: []
case 3
in: [[1,1],[0,1],[["a",[0,0]],["b",[0,-1]]],5]
expected: ["a","b"]
case 4
in: [[1,0],[1,0],[],1]
expected: []
case 5
in: [[1,0],[1,0],[["a",[0,1]]],0]
expected: []
Starter codePython
def hard_negative_mining(query, positive, negatives, n):
    # Your code here
    pass

5 test cases · run your code against them in the editor